Sha256: 4c6c8c41ec651ec13b55e8cb9a7c9e5449e1a437b5a7e11ae829b1d3433de78e

Contents?: true

Size: 784 Bytes

Versions: 4

Compression:

Stored size: 784 Bytes

Contents

# Represents a plain old boolean type. TBD: can be nil?
#
require_relative '../exceptions'

module Attributor

  class Boolean
    include Type

    def self.valid_type?(value)
      value == true || value == false
    end

    def self.example(context=nil, options: {})
      [true, false].sample
    end

    def self.load(value,context=Attributor::DEFAULT_ROOT_CONTEXT, **options)
      return nil if value.nil?

      raise CoercionError, context: context, from: value.class, to: self, value: value  if value.is_a?(::Float)
      return false if [ false, 'false', 'FALSE', '0', 0, 'f', 'F' ].include?(value)
      return true if [ true, 'true', 'TRUE', '1', 1, 't', 'T' ].include?(value)
      raise CoercionError, context: context, from: value.class, to: self
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
attributor-2.6.0 lib/attributor/types/boolean.rb
attributor-2.5.0 lib/attributor/types/boolean.rb
attributor-2.4.0 lib/attributor/types/boolean.rb
attributor-2.3.0 lib/attributor/types/boolean.rb