Class: LIVR::Rules::Meta::VariableObject

Inherits:
LIVR::Rule
  • Object
show all
Defined in:
lib/livr/rules/meta.rb

Instance Method Summary collapse

Constructor Details

#initialize(selector_field, livrs) ⇒ VariableObject

Returns a new instance of VariableObject.


26
27
28
29
30
31
32
# File 'lib/livr/rules/meta.rb', line 26

def initialize(selector_field, livrs)
  @selector_field = selector_field
  @validators = {}
  livrs.each do |selector_value, livr|
    @validators[selector_value] = Validator.new(livr).prepare
  end
end

Instance Method Details

#call(object, user_data, field_results) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/livr/rules/meta.rb', line 34

def call(object, user_data, field_results)
  return if is_no_value(object)
  if ( !object.is_a?(Hash) || !object[@selector_field] || !@validators[ object[@selector_field] ] )
    return 'FORMAT_ERROR'
  end

  validator = @validators.fetch(object.fetch(@selector_field))
  result = validator.validate(object)

  if result
    field_results << result
    return
  else
    return validator.get_errors
  end
end