Sha256: b683187f8cd254e38d55b667d900fe3f3157c594a237ab81410086648b0cf290

Contents?: true

Size: 1.83 KB

Versions: 7

Compression:

Stored size: 1.83 KB

Contents

require 'dry/types/any'

module Dry
  module Types
    COERCIBLE = {
      string: String,
      integer: Integer,
      float: Float,
      decimal: BigDecimal,
      array: ::Array,
      hash: ::Hash
    }.freeze

    NON_COERCIBLE = {
      nil: NilClass,
      symbol: Symbol,
      class: Class,
      true: TrueClass,
      false: FalseClass,
      date: Date,
      date_time: DateTime,
      time: Time,
      range: Range
    }.freeze

    ALL_PRIMITIVES = COERCIBLE.merge(NON_COERCIBLE).freeze

    NON_NIL = ALL_PRIMITIVES.reject { |name, _| name == :nil }.freeze

    # Register built-in types that are non-coercible through kernel methods
    ALL_PRIMITIVES.each do |name, primitive|
      register(name.to_s, Definition[primitive].new(primitive))
    end

    # Register strict built-in types that are non-coercible through kernel methods
    ALL_PRIMITIVES.each do |name, primitive|
      register("strict.#{name}", self[name.to_s].constrained(type: primitive))
    end

    # Register built-in primitive types with kernel coercion methods
    COERCIBLE.each do |name, primitive|
      register("coercible.#{name}", self[name.to_s].constructor(Kernel.method(primitive.name)))
    end

    # Register non-coercible optional types
    NON_NIL.each_key do |name|
      register("optional.strict.#{name}", self["strict.#{name}"].optional)
    end

    # Register coercible optional types
    COERCIBLE.each_key do |name|
      register("optional.coercible.#{name}", self["coercible.#{name}"].optional)
    end

    # Register :bool since it's common and not a built-in Ruby type :(
    register("bool", self["true"] | self["false"])
    register("strict.bool", self["strict.true"] | self["strict.false"])

    register("any", Any)
    register("object", self['any'])
  end
end

require 'dry/types/coercions'
require 'dry/types/params'
require 'dry/types/json'

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dry-types-0.14.1 lib/dry/types/core.rb
dry-types-0.14.0 lib/dry/types/core.rb
dry-types-0.13.4 lib/dry/types/core.rb
dry-types-0.13.3 lib/dry/types/core.rb
dry-types-0.13.2 lib/dry/types/core.rb
dry-types-0.13.1 lib/dry/types/core.rb
dry-types-0.13.0 lib/dry/types/core.rb