Module: Dry::Data

Extended by:
Configurable
Defined in:
lib/dry/data.rb,
lib/dry/data/enum.rb,
lib/dry/data/safe.rb,
lib/dry/data/type.rb,
lib/dry/data/types.rb,
lib/dry/data/value.rb,
lib/dry/data/struct.rb,
lib/dry/data/default.rb,
lib/dry/data/version.rb,
lib/dry/data/compiler.rb,
lib/dry/data/optional.rb,
lib/dry/data/sum_type.rb,
lib/dry/data/container.rb,
lib/dry/data/decorator.rb,
lib/dry/data/type/hash.rb,
lib/dry/data/type/array.rb,
lib/dry/data/types/form.rb,
lib/dry/data/constrained.rb,
lib/dry/data/constraints.rb,
lib/dry/data/type_builder.rb,
lib/dry/data/coercions/form.rb

Defined Under Namespace

Modules: Coercions, Decorator, Predicates, TypeBuilder Classes: Compiler, Constrained, Container, Default, Enum, Optional, Safe, SchemaError, SchemaKeyError, Struct, SumType, Type, Value

Constant Summary collapse

StructError =
Class.new(TypeError)
ConstraintError =
Class.new(TypeError)
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
VERSION =
'0.5.1'.freeze

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dry/data.rb', line 59

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

        type =
          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

    type
  end
end

.containerObject



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

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

.define_constants(namespace, identifiers) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dry/data.rb', line 81

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



40
41
42
# File 'lib/dry/data.rb', line 40

def self.finalize
  define_constants(config.namespace, container._container.keys)
end

.identifier(klass) ⇒ Object



96
97
98
# File 'lib/dry/data.rb', line 96

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

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



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

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

.register_class(klass) ⇒ Object



52
53
54
55
56
57
# File 'lib/dry/data.rb', line 52

def self.register_class(klass)
  container.register(
    Inflecto.underscore(klass).gsub('/', '.'),
    Type.new(klass.method(:new), primitive: klass)
  )
end

.Rule(primitive, options) ⇒ Object



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

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

.rule_compilerObject



22
23
24
# File 'lib/dry/data/constraints.rb', line 22

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

.type_mapObject



100
101
102
# File 'lib/dry/data.rb', line 100

def self.type_map
  @type_map ||= ThreadSafe::Cache.new
end