Sha256: 2f8689bbe9dd2c0b80baae8bdbe87b59f188d098aac768929ad159d1ea99ac91

Contents?: true

Size: 777 Bytes

Versions: 2

Compression:

Stored size: 777 Bytes

Contents

# -*- encoding: utf-8 -*-

require 'aequitas/rule'

module Aequitas
  class Rule
    # TODO: update this to inherit from Rule::Within::Set
    class Acceptance < Rule

      equalize_on *(superclass.equalizer.keys + [:accept])

      DEFAULT_ACCEPTED_VALUES = [ '1', 1, 'true', true, 't' ]

      attr_reader :accept

      def initialize(attribute_name, options = {})
        super

        @accept = Array(options.fetch(:accept, DEFAULT_ACCEPTED_VALUES))

        skip_condition.default_to_allowing_nil!
      end

      def valid?(resource)
        value = attribute_value(resource)

        skip?(value) || accept.include?(value)
      end

      def violation_type(resource)
        :accepted
      end

    end # class Acceptance
  end # class Rule
end # module Aequitas

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aequitas-0.0.2 lib/aequitas/rule/acceptance.rb
aequitas-0.0.1 lib/aequitas/rule/acceptance.rb