Sha256: 5abf7157392f5d6a13e43b6a73871a9ca6587fed38f326868eaefb1bd1a893f0

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

module Dry
  module Types
    COERCIBLE = {
      string: String,
      int: 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
    }.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("object", Definition[::Object].new(::Object))
  end
end

require 'dry/types/coercions'
require 'dry/types/form'
require 'dry/types/json'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-types-0.9.4 lib/dry/types/core.rb