Sha256: 2aa9b689a3325351d4688788b1b648b13f5f08e2e2d8d80f610f6fed535f4ba3

Contents?: true

Size: 973 Bytes

Versions: 4

Compression:

Stored size: 973 Bytes

Contents

module Koine
  module Attributes
    module Adapter
      class Boolean < Base
        DEFAULT_TRUE_VALUES = [1, '1', true, 'true'].freeze
        DEFAULT_FALSE_VALUES = [0, '0', false, 'false'].freeze

        def initialize(
          true_values: DEFAULT_TRUE_VALUES.dup,
          false_values: DEFAULT_FALSE_VALUES.dup
        )
          @true_values = true_values
          @false_values = false_values
        end

        def append_true_value(value)
          true_values << value
          self
        end

        def append_false_value(value)
          false_values << value
          self
        end

        private

        attr_reader :true_values
        attr_reader :false_values

        def coerce_not_nil(value)
          return true if true_values.include?(value)
          return false if false_values.include?(value)
          raise InvalidAttributeError.new("Invalid argument '#{value}'", attribute_name)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
koine-attributes-1.2.3 lib/koine/attributes/adapter/boolean.rb
koine-attributes-1.2.2 lib/koine/attributes/adapter/boolean.rb
koine-attributes-1.2.1 lib/koine/attributes/adapter/boolean.rb
koine-attributes-1.2.0 lib/koine/attributes/adapter/boolean.rb