Class: Yema::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/yema/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, *rules) ⇒ Validator

Returns a new instance of Validator.



6
7
8
# File 'lib/yema/validator.rb', line 6

def initialize(resource, *rules)
  @resource, @rules = resource, rules
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'lib/yema/validator.rb', line 4

def resource
  @resource
end

#rulesObject (readonly)

Returns the value of attribute rules.



4
5
6
# File 'lib/yema/validator.rb', line 4

def rules
  @rules
end

Instance Method Details

#errorsObject



14
15
16
17
18
19
20
21
# File 'lib/yema/validator.rb', line 14

def errors
  errors = []
  rules.each do |rule|
    value = resource.public_send(rule.attribute_name)
    errors << Error.new(rule, resource) unless rule.matches?(value)
  end
  errors.to_set
end

#valid?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/yema/validator.rb', line 10

def valid?
  errors.empty?
end