Module: Dry::Types

Extended by:
Configurable
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/maybe.rb,
lib/dry/types/value.rb,
lib/dry/types/errors.rb,
lib/dry/types/result.rb,
lib/dry/types/struct.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/constrained/coercible.rb

Defined Under Namespace

Modules: Builder, Coercions, Decorator, Options, Predicates, Result Classes: Array, Compiler, Constrained, Constructor, Container, Default, Definition, Enum, Hash, Maybe, RepeatedAttributeError, Safe, SchemaError, SchemaKeyError, Struct, Sum, Value

Constant Summary collapse

Undefined =
Object.new.freeze
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
StructError =
Class.new(TypeError)
ConstraintError =
Class.new(TypeError) do
  attr_reader :result

  def initialize(result)
    @result = result
    if result.is_a?(String)
      super
    else
      super("#{result.input.inspect} violates constraints (#{failure_message})")
    end
  end

  def input
    result.input
  end

  def failure_message
    if result.respond_to?(:rule)
      rule = result.rule
      "#{rule.predicate.id}(#{rule.predicate.args.map(&:inspect).join(', ')}) failed"
    else
      result.inspect
    end
  end
end
VERSION =
'0.7.2'.freeze

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dry/types.rb', line 57

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
      self[identifier(name)]
    end
  end
end

.containerObject



44
45
46
# File 'lib/dry/types.rb', line 44

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

.define_constants(namespace, identifiers) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dry/types.rb', line 75

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



36
37
38
39
40
41
42
# File 'lib/dry/types.rb', line 36

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) ⇒ Object



90
91
92
# File 'lib/dry/types.rb', line 90

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

.moduleObject



30
31
32
33
34
# File 'lib/dry/types.rb', line 30

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

.register(name, type = nil, &block) ⇒ Object



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

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

.register_class(klass, meth = :new) ⇒ Object



52
53
54
55
# File 'lib/dry/types.rb', line 52

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

.Rule(options) ⇒ Object



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

def self.Rule(options)
  rule_compiler.(
    options.map { |key, val| [:val, [:predicate, [:"#{key}?", [val]]]] }
  ).reduce(:and)
end

.rule_compilerObject



20
21
22
# File 'lib/dry/types/constraints.rb', line 20

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

.type_keysObject



98
99
100
# File 'lib/dry/types.rb', line 98

def self.type_keys
  container._container.keys
end

.type_mapObject



94
95
96
# File 'lib/dry/types.rb', line 94

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