Class: Reaction::ValidatorBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/reaction/validator_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validator, options = {}) ⇒ ValidatorBuilder

Returns a new instance of ValidatorBuilder.



6
7
8
9
# File 'lib/reaction/validator_builder.rb', line 6

def initialize(validator, options = {})
  @validator = class_for_validator!(validator)
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/reaction/validator_builder.rb', line 4

def options
  @options
end

#validatorObject

Returns the value of attribute validator.



3
4
5
# File 'lib/reaction/validator_builder.rb', line 3

def validator
  @validator
end

Instance Method Details

#build(action) ⇒ Object



11
12
13
# File 'lib/reaction/validator_builder.rb', line 11

def build(action)
  validator.new(action, options)
end

#class_for_validator(validator) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/reaction/validator_builder.rb', line 15

def class_for_validator(validator)
  return validator if validator.is_a?(Class)
  name = validator.to_s.split('_').map(&:capitalize).join
  Module.const_get("#{name}Validator")
rescue NameError
  nil
end

#class_for_validator!(validator) ⇒ Object



23
24
25
# File 'lib/reaction/validator_builder.rb', line 23

def class_for_validator!(validator)
  class_for_validator(validator) or raise ArgumentError.new("Unknown validator: #{validator}")
end