Sha256: eaf69693609e4395617e46aa0428efb3c832fc6451bd4d45c0dd2cf3820e33a4

Contents?: true

Size: 892 Bytes

Versions: 12

Compression:

Stored size: 892 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

    def self.json_schema_type
      :boolean
    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
attributor-8.0 lib/attributor/types/boolean.rb
attributor-7.1 lib/attributor/types/boolean.rb
attributor-7.0 lib/attributor/types/boolean.rb
attributor-6.5 lib/attributor/types/boolean.rb
attributor-6.4 lib/attributor/types/boolean.rb
attributor-6.3 lib/attributor/types/boolean.rb
attributor-6.2 lib/attributor/types/boolean.rb
attributor-6.1 lib/attributor/types/boolean.rb
attributor-6.0 lib/attributor/types/boolean.rb
attributor-5.7 lib/attributor/types/boolean.rb
attributor-5.6 lib/attributor/types/boolean.rb
attributor-5.5 lib/attributor/types/boolean.rb