module Dry::Data

Constants

ALL_PRIMITIVES
COERCIBLE
ConstraintError
NON_COERCIBLE
StructError
TYPE_SPEC_REGEX
VERSION

Public Class Methods

Rule(primitive, options) click to toggle source
# 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
[](name) click to toggle source
# 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
container() click to toggle source
# File lib/dry/data.rb, line 44
def self.container
  @container ||= Container.new
end
define_constants(namespace, identifiers) click to toggle source
# 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
finalize() click to toggle source
# File lib/dry/data.rb, line 40
def self.finalize
  define_constants(config.namespace, container._container.keys)
end
identifier(klass) click to toggle source
# File lib/dry/data.rb, line 96
def self.identifier(klass)
  Inflecto.underscore(klass).gsub('/', '.')
end
register(name, type = nil, &block) click to toggle source
# File lib/dry/data.rb, line 48
def self.register(name, type = nil, &block)
  container.register(name, type || block.call)
end
register_class(klass) click to toggle source
# 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_compiler() click to toggle source
# File lib/dry/data/constraints.rb, line 22
def self.rule_compiler
  @rule_compiler ||= Logic::RuleCompiler.new(Data::Predicates)
end
type_map() click to toggle source
# File lib/dry/data.rb, line 100
def self.type_map
  @type_map ||= ThreadSafe::Cache.new
end