class NxtSchema::Validators::Attribute

Attributes

expectation[R]
method[R]

Public Class Methods

new(method, expectation) click to toggle source
# File lib/nxt_schema/validators/attribute.rb, line 4
def initialize(method, expectation)
  @method = method
  @expectation = expectation
end

Public Instance Methods

build() click to toggle source

Query any attribute on a value with validator(:attribute, :size, ->(s) { s < 7 })

# File lib/nxt_schema/validators/attribute.rb, line 14
def build
  lambda do |node, value|
    raise ArgumentError, "#{value} does not respond to query: #{method}" unless value.respond_to?(method)

    if expectation.call(value.send(method))
      true
    else
      node.add_error(
        translate_error(
          node.locale,
          attribute: value,
          attribute_name: method,
          value: value.send(method)
        )
      )
    end
  end
end