Module: Dry::Types

Extended by:
Configurable, Core::Extensions
Includes:
Core::Constants
Defined in:
lib/dry/types.rb,
lib/dry/types/sum.rb,
lib/dry/types/core.rb,
lib/dry/types/enum.rb,
lib/dry/types/form.rb,
lib/dry/types/hash.rb,
lib/dry/types/json.rb,
lib/dry/types/safe.rb,
lib/dry/types/array.rb,
lib/dry/types/errors.rb,
lib/dry/types/result.rb,
lib/dry/types/builder.rb,
lib/dry/types/default.rb,
lib/dry/types/options.rb,
lib/dry/types/version.rb,
lib/dry/types/compiler.rb,
lib/dry/types/coercions.rb,
lib/dry/types/container.rb,
lib/dry/types/decorator.rb,
lib/dry/types/definition.rb,
lib/dry/types/constrained.rb,
lib/dry/types/constraints.rb,
lib/dry/types/constructor.rb,
lib/dry/types/hash/schema.rb,
lib/dry/types/array/member.rb,
lib/dry/types/coercions/form.rb,
lib/dry/types/coercions/json.rb,
lib/dry/types/extensions/maybe.rb,
lib/dry/types/constrained/coercible.rb

Overview

Returns:

  • (Module)

Defined Under Namespace

Modules: Builder, Coercions, Decorator, Options Classes: Array, Compiler, Constrained, ConstraintError, Constructor, Container, Default, Definition, Enum, Hash, Maybe, MissingKeyError, Result, Safe, SchemaError, Sum, UnknownKeysError

Constant Summary collapse

TYPE_SPEC_REGEX =
%r[(.+)<(.+)>].freeze
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
SchemaKeyError =
Class.new(KeyError)
VERSION =
'0.9.4'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#namespaceContainer{String => Definition} (readonly)

Returns:



7
# File 'lib/dry/types/errors.rb', line 7

setting :namespace, self

Class Method Details

.[](name) ⇒ Definition

Parameters:

  • name (String)

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dry/types.rb', line 72

def self.[](name)
  type_map.fetch_or_store(name) do
    case name
    when String
      result = name.match(TYPE_SPEC_REGEX)

      if result
        type_id, member_id = result[1..2]
        container[type_id].member(self[member_id])
      else
        container[name]
      end
    when Class
      type_name = identifier(name)

      if container.key?(type_name)
        self[type_name]
      else
        name
      end
    end
  end
end

.containerContainer{String => Definition}

Returns:



49
50
51
# File 'lib/dry/types.rb', line 49

def self.container
  @container ||= Container.new
end

.define_constants(namespace, identifiers) ⇒ <Definition>

Parameters:

Returns:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dry/types.rb', line 99

def self.define_constants(namespace, identifiers)
  names = identifiers.map do |id|
    parts = id.split('.')
    [Inflecto.camelize(parts.pop), parts.map(&Inflecto.method(:camelize))]
  end

  names.map do |(klass, parts)|
    mod = parts.reduce(namespace) do |a, e|
      a.constants.include?(e.to_sym) ? a.const_get(e) : a.const_set(e, Module.new)
    end

    mod.const_set(klass, self[identifier((parts + [klass]).join('::'))])
  end
end

.finalizeObject

Deprecated.

Include module instead



40
41
42
43
44
45
46
# File 'lib/dry/types.rb', line 40

def self.finalize
  warn 'Dry::Types.finalize and configuring namespace is deprecated. Just'\
   ' do `include Dry::Types.module` in places where you want to have access'\
   ' to built-in types'

  define_constants(config.namespace, type_keys)
end

.identifier(klass) ⇒ String

Parameters:

  • klass (#to_s)

Returns:

  • (String)


116
117
118
# File 'lib/dry/types.rb', line 116

def self.identifier(klass)
  Inflecto.underscore(klass).tr('/', '.')
end

.moduleObject



33
34
35
36
37
# File 'lib/dry/types.rb', line 33

def self.module
  namespace = Module.new
  define_constants(namespace, type_keys)
  namespace
end

.register(name, type = nil, &block) ⇒ Container{String => Definition}

Parameters:

  • name (String)
  • type (Definition) (defaults to: nil)
  • block (#call)

Returns:



57
58
59
# File 'lib/dry/types.rb', line 57

def self.register(name, type = nil, &block)
  container.register(name, type || block.call)
end

.register_class(klass, meth = :new) ⇒ Container{String => Definition}

Registers given +klass+ in #container using +meth+ constructor

Parameters:

  • klass (Class)
  • meth (Symbol) (defaults to: :new)

Returns:



65
66
67
68
# File 'lib/dry/types.rb', line 65

def self.register_class(klass, meth = :new)
  type = Definition.new(klass).constructor(klass.method(meth))
  container.register(identifier(klass), type)
end

.Rule(options) ⇒ Dry::Logic::Rule

Parameters:

Returns:

  • (Dry::Logic::Rule)


9
10
11
12
13
# File 'lib/dry/types/constraints.rb', line 9

def self.Rule(options)
  rule_compiler.(
    options.map { |key, val| Logic::Rule::Predicate.new(Logic::Predicates[:"#{key}?"]).curry(val).to_ast }
  ).reduce(:and)
end

.rule_compilerDry::Logic::RuleCompiler

Returns:

  • (Dry::Logic::RuleCompiler)


16
17
18
# File 'lib/dry/types/constraints.rb', line 16

def self.rule_compiler
  @rule_compiler ||= Logic::RuleCompiler.new(Logic::Predicates)
end

.type_keys<String>

List of type keys defined in container

Returns:

  • (<String>)


127
128
129
# File 'lib/dry/types.rb', line 127

def self.type_keys
  container._container.keys
end

.type_mapConcurrent::Map

Returns:

  • (Concurrent::Map)


121
122
123
# File 'lib/dry/types.rb', line 121

def self.type_map
  @type_map ||= Concurrent::Map.new
end