Sha256: 29127bbac6dbba7cec0953d4af2234592cb3aec20cfece9b48d301bff2259aed

Contents?: true

Size: 1005 Bytes

Versions: 1

Compression:

Stored size: 1005 Bytes

Contents

# frozen_string_literal: true

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

1 entries across 1 versions & 1 rubygems

Version Path
koine-attributes-1.2.4 lib/koine/attributes/adapter/boolean.rb