Class: LIVR::AliasedRule

Inherits:
Object
  • Object
show all
Defined in:
lib/livr/aliased_rule.rb

Instance Method Summary collapse

Constructor Details

#initialize(_alias) ⇒ AliasedRule

Returns a new instance of AliasedRule.


4
5
6
7
8
9
10
# File 'lib/livr/aliased_rule.rb', line 4

def initialize(_alias)
  raise "Alias name required" unless _alias["name"]
  raise "Alias rules required" unless _alias["rules"].present?

  @alias = _alias
  @validator = Validator.new(value: Array.wrap(@alias["rules"]))
end

Instance Method Details

#call(value, user_data, field_results) ⇒ Object


16
17
18
19
20
21
22
23
24
# File 'lib/livr/aliased_rule.rb', line 16

def call(value, user_data, field_results)
  result = @validator.validate({ value: value })
  if result
    field_results << result[:value]
    return
  else
    return @alias.fetch("error", @validator.get_errors)
  end
end

#new(*args) ⇒ Object


12
13
14
# File 'lib/livr/aliased_rule.rb', line 12

def new(*args)
  self
end