Class: NxtSchema::Validators::Query

Inherits:
Validator
  • Object
show all
Defined in:
lib/nxt_schema/validators/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Validator

register_as, #translate_error

Constructor Details

#initialize(method) ⇒ Query

Returns a new instance of Query.



4
5
6
# File 'lib/nxt_schema/validators/query.rb', line 4

def initialize(method)
  @method = method
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



9
10
11
# File 'lib/nxt_schema/validators/query.rb', line 9

def method
  @method
end

Instance Method Details

#buildObject

Query a boolean method on you value => node(:test, :String).validate(:query, :good_enough?)

> Would be valid if value.good_enough? is truthy



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/nxt_schema/validators/query.rb', line 14

def build
  lambda do |node, value|
    raise ArgumentError, "#{value} does not respond to query: #{method}" unless value.respond_to?(method)

    if value.send(method)
      true
    else
      message = translate_error(node.locale, value: value, actual: value.send(method), query: method)
      node.add_error(message)
    end
  end
end