Sha256: 0af351fee87d6338747a9b5279faa2e087267cb9196399e0f5b2512cd90cf2ab

Contents?: true

Size: 764 Bytes

Versions: 4

Compression:

Stored size: 764 Bytes

Contents

module NxtSchema
  module Validators
    class Query < Validator
      def initialize(method)
        @method = method
      end

      register_as :query
      attr_reader :method

      # Query a boolean method on you value => node(:test, :String).validate(:query, :good_enough?)
      # => Would be valid if value.good_enough? is truthy

      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nxt_schema-1.0.2 lib/nxt_schema/validators/query.rb
nxt_schema-0.1.2 lib/nxt_schema/validators/query.rb
nxt_schema-0.1.1 lib/nxt_schema/validators/query.rb
nxt_schema-0.1.0 lib/nxt_schema/validators/query.rb