Sha256: 63691b06f9b37f1e5cabf76dd6eda6e836603f500eaa933144deba751522024a

Contents?: true

Size: 837 Bytes

Versions: 2

Compression:

Stored size: 837 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.new(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.new(context: context, from: value.class, to: self)
    end

    def self.family
      'boolean'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
attributor-5.4 lib/attributor/types/boolean.rb
attributor-5.3 lib/attributor/types/boolean.rb