class Pheromone::Validators::OptionsValidator

Constants

ACCEPTED_EVENT_TYPES
ALLOWED_DISPATCH_METHODS

Public Class Methods

new(message_options) click to toggle source
# File lib/pheromone/validators/options_validator.rb, line 9
def initialize(message_options)
  @errors = {}
  @message_options = message_options
end

Public Instance Methods

validate() click to toggle source
# File lib/pheromone/validators/options_validator.rb, line 14
def validate
  validate_message_options
  return @errors if @errors.present?
  validate_topic
  validate_event_types
  validate_message_attributes
  validate_dispatch_method
  @errors
end

Private Instance Methods

add_error_message(key, value) click to toggle source
# File lib/pheromone/validators/options_validator.rb, line 68
def add_error_message(key, value)
  @errors.merge!(key => value)
end
validate_dispatch_method() click to toggle source
# File lib/pheromone/validators/options_validator.rb, line 60
def validate_dispatch_method
  dispatch_methods = @message_options.map{ |options| options[:dispatch_method] }
  return if dispatch_methods.all? do |method|
    method.nil? || ALLOWED_DISPATCH_METHODS.include?(method)
  end
  add_error_message(:dispatch_method, 'Invalid dispatch method')
end
validate_event_types() click to toggle source

:reek: FeatureEnvy

# File lib/pheromone/validators/options_validator.rb, line 37
def validate_event_types
  return if @message_options.all? do |options|
    event_types = options[:event_types]
    next true unless event_types
    event_types.present? &&
      event_types.is_a?(Array) &&
      (event_types - ACCEPTED_EVENT_TYPES).empty?
  end

  add_error_message(
    :event_types,
    "Event types must be a non-empty array with types #{ACCEPTED_EVENT_TYPES.join(',')}"
  )
end
validate_message_attributes() click to toggle source
# File lib/pheromone/validators/options_validator.rb, line 52
def validate_message_attributes
  return if @message_options.all? do |options|
    options[:serializer].present? || options[:message].present?
  end

  add_error_message(:message_attributes, 'Either serializer or message should be specified')
end
validate_message_options() click to toggle source
# File lib/pheromone/validators/options_validator.rb, line 26
def validate_message_options
  return if @message_options.is_a?(Array)
  add_error_message(:message_options, 'Message options should be an array')
end
validate_topic() click to toggle source
# File lib/pheromone/validators/options_validator.rb, line 31
def validate_topic
  return if @message_options.all? { |options| options[:topic].present? }
  add_error_message(:topic, 'Topic name missing')
end