Class: LIVR::Rules::Meta::ListOfDifferentObjects

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

Instance Method Summary collapse

Constructor Details

#initialize(selector_field, livrs) ⇒ ListOfDifferentObjects

Returns a new instance of ListOfDifferentObjects.


125
126
127
128
129
130
131
# File 'lib/livr/rules/meta.rb', line 125

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(objects, user_data, field_results) ⇒ Object


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/livr/rules/meta.rb', line 133

def call(objects, user_data, field_results)
  return if is_no_value(objects)
  return 'FORMAT_ERROR' unless objects.is_a?(Array)

  results = []
  errors = []
  has_errors = false

  objects.each do |object|
    if !object.is_a?(Hash) || !object[@selector_field] || !@validators[ object[@selector_field] ]
      errors << 'FORMAT_ERROR'
      next
    end

    validator = @validators[ object[@selector_field] ]
    result = validator.validate(object)

    if result
      results << result
      errors << nil
    else
      has_errors = true
      errors << validator.get_errors
      results << nil
    end
  end

  if has_errors
    return errors
  else
    field_results << results
    return
  end
end