Class: LIVR::Rules::Meta::ListOf

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

Instance Method Summary collapse

Constructor Details

#initialize(*rules) ⇒ ListOf

Returns a new instance of ListOf.


53
54
55
56
# File 'lib/livr/rules/meta.rb', line 53

def initialize(*rules)
  livr = { field: rules.flatten }
  @validator = Validator.new(livr).prepare
end

Instance Method Details

#call(values, user_data, field_results) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/livr/rules/meta.rb', line 58

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

  results = []
  errors = []
  has_errors = false

  values.each do |value|
    result = @validator.validate(field: value)

    if result
      results << result[:field]
      errors << nil
    else
      has_errors = true
      errors << @validator.get_errors[:field]
      results << nil
    end
  end

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