Class: Validate::Validator
- Inherits:
-
Object
- Object
- Validate::Validator
- Defined in:
- lib/validate/validator.rb
Overview
Actual implementation
Instance Method Summary collapse
-
#initialize(validations) ⇒ Validator
constructor
A new instance of Validator.
- #validates?(context) ⇒ Boolean
Constructor Details
#initialize(validations) ⇒ Validator
Returns a new instance of Validator.
8 9 10 |
# File 'lib/validate/validator.rb', line 8 def initialize(validations) @validations = validations end |
Instance Method Details
#validates?(context) ⇒ Boolean
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/validate/validator.rb', line 12 def validates?(context) bool = @validations .map do |v| # destructure fields v[:fields].map {|f| v.merge(fields: f) } end.flatten(1) .select do |v| # `:when` is a special case, this gets processed right away and # filtered out... when_opt = (v[:opts] || {})[:when] # :is_set is short for checking if then field is set when_opt = -> { self.to_hash.include?(v[:fields]) } if when_opt == :is_set !when_opt.is_a?(Proc) || context.instance_exec(&when_opt) end .map do |v| # lastly, execute validation validator = if v[:validations] Validator.new(v[:validations]) end ValidationMethods.send(v[:name], context.to_hash, v[:fields], v[:opts], validator) end .reduce {|a,b| a && b } # return the result as a boolean bool.nil? ? true : bool end |