Class: FluentValidation::AttributeRule

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent_validation/attribute_rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&expression) ⇒ AttributeRule



10
11
12
13
# File 'lib/fluent_validation/attribute_rule.rb', line 10

def initialize(&expression)
  @expression = expression
  @validators = Array.new
end

Instance Attribute Details

#attribute_nameObject

Returns the value of attribute attribute_name.



8
9
10
# File 'lib/fluent_validation/attribute_rule.rb', line 8

def attribute_name
  @attribute_name
end

#conditionObject

Returns the value of attribute condition.



8
9
10
# File 'lib/fluent_validation/attribute_rule.rb', line 8

def condition
  @condition
end

#error_codeObject

Returns the value of attribute error_code.



8
9
10
# File 'lib/fluent_validation/attribute_rule.rb', line 8

def error_code
  @error_code
end

#expressionObject (readonly)

Returns the value of attribute expression.



7
8
9
# File 'lib/fluent_validation/attribute_rule.rb', line 7

def expression
  @expression
end

#validatorsObject (readonly)

Returns the value of attribute validators.



7
8
9
# File 'lib/fluent_validation/attribute_rule.rb', line 7

def validators
  @validators
end

Instance Method Details

#add_validator(validator) ⇒ Object



15
16
17
# File 'lib/fluent_validation/attribute_rule.rb', line 15

def add_validator(validator)
  @validators << validator
end

#validate(object) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fluent_validation/attribute_rule.rb', line 19

def validate(object)
  validation_failures = Array.new
  if @condition.nil? || @condition.call(object)
    value = expression.call object
    validator_context = Validators::AttributeValidatorContext.new @attribute_name, value, @error_code
    @validators.each do |validator|
      result = validator.validate validator_context
      validation_failures.concat result
    end
  end
  validation_failures
end