Sha256: 26c890a359a4b5428a09d8449f9b1826cee34939749e9e8239caec71d2197581

Contents?: true

Size: 727 Bytes

Versions: 5

Compression:

Stored size: 727 Bytes

Contents

# frozen_string_literal: true

module InferModel
  class Parsers::Boolean
    extend Callable
    extend Dry::Initializer

    param :value
    option :allow_blank, default: -> { true }

    TRUTHY_VALUES_LOWERCASE = %w[true t x y j + * 1].freeze
    FALSEY_VALUES_LOWERCASE = %w[false f n - 0].freeze

    def call
      raise Parsers::Error, "value was blank which is not allowed" if value.nil? && !allow_blank
      return if value.nil?
      return false if value.empty?
      return false if FALSEY_VALUES_LOWERCASE.any? { |lie| value.casecmp(lie).zero? }
      return true if TRUTHY_VALUES_LOWERCASE.any? { |truth| value.casecmp(truth).zero? }

      raise Parsers::Error, "'#{value}' is not a Boolean"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
infer_model-0.1.5 lib/infer_model/parsers/boolean.rb
infer_model-0.1.4 lib/infer_model/parsers/boolean.rb
infer_model-0.1.2 lib/infer_model/parsers/boolean.rb
infer_model-0.1.1 lib/infer_model/parsers/boolean.rb
infer_model-0.1.0 lib/infer_model/parsers/boolean.rb