Class: Dry::Schema::Macros::Value Private

Inherits:
DSL
  • Object
show all
Defined in:
lib/dry/schema/macros/value.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A macro used for specifying predicates to be applied to values from a hash

Direct Known Subclasses

Filled, Schema

Instance Attribute Summary

Attributes inherited from DSL

#chain, #predicate_inferrer, #primitive_inferrer

Instance Method Summary collapse

Methods inherited from DSL

#array, #custom_type?, #each, #filled, #hash, #maybe, #schema, #type, #value

Methods inherited from Core

#new, #operation, #path, #to_ast, #to_rule

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



115
116
117
118
119
120
121
# File 'lib/dry/schema/macros/value.rb', line 115

def method_missing(meth, *args, &block)
  if meth.to_s.end_with?(QUESTION_MARK)
    trace.__send__(meth, *args, &block)
  else
    super
  end
end

Instance Method Details

#array_type?(type) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


74
75
76
# File 'lib/dry/schema/macros/value.rb', line 74

def array_type?(type)
  primitive_inferrer[type].eql?([::Array])
end

#build_array_type(array_type, member) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dry/schema/macros/value.rb', line 89

def build_array_type(array_type, member)
  if array_type.respond_to?(:of)
    array_type.of(member)
  else
    raise ArgumentError, <<~ERROR.split("\n").join(" ")
      Cannot define schema for a nominal array type.
      Array types must be instances of Dry::Types::Array,
      usually constructed with Types::Constructor(Array) { ... } or
      Dry::Types['array'].constructor { ... }
    ERROR
  end
end

#call(*args, **opts, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dry/schema/macros/value.rb', line 15

def call(*args, **opts, &block)
  types, predicates = args.partition { |arg| arg.is_a?(Dry::Types::Type) }

  constructor = types.select { |type| type.is_a?(Dry::Types::Constructor) }.reduce(:>>)
  schema = predicates.detect { |predicate| predicate.is_a?(Processor) }

  schema_dsl.set_type(name, constructor) if constructor

  type_spec = opts[:type_spec]

  if schema
    current_type = schema_dsl.types[name]

    updated_type =
      if array_type?(current_type)
        build_array_type(current_type, schema.strict_type_schema)
      else
        schema.strict_type_schema
      end

    import_steps(schema)

    if !custom_type? || array_type?(current_type) || hash_type?(current_type)
      type(updated_type)
    elsif maybe_type?(current_type)
      type(updated_type.optional)
    end
  end

  trace_opts = opts.reject { |key, _| %i[type_spec type_rule].include?(key) }

  if (type_rule = opts[:type_rule])
    trace.append(type_rule).evaluate(*predicates, **trace_opts)
    trace.append(new(chain: false).instance_exec(&block)) if block
  else
    trace.evaluate(*predicates, **trace_opts)

    if block && type_spec.equal?(:hash)
      hash(&block)
    elsif type_spec.is_a?(::Dry::Types::Type) && hash_type?(type_spec)
      hash(type_spec)
    elsif block
      trace.append(new(chain: false).instance_exec(&block))
    end
  end

  if trace.captures.empty?
    raise ArgumentError, "wrong number of arguments (given 0, expected at least 1)"
  end

  each(type_spec.type.member) if type_spec.respond_to?(:member)

  self
end

#hash_type?(type) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


79
80
81
# File 'lib/dry/schema/macros/value.rb', line 79

def hash_type?(type)
  primitive_inferrer[type].eql?([::Hash])
end

#import_steps(schema) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



103
104
105
# File 'lib/dry/schema/macros/value.rb', line 103

def import_steps(schema)
  schema_dsl.steps.import_callbacks(Path[[*path, name]], schema.steps)
end

#maybe_type?(type) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


84
85
86
# File 'lib/dry/schema/macros/value.rb', line 84

def maybe_type?(type)
  type.meta[:maybe].equal?(true)
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


108
109
110
# File 'lib/dry/schema/macros/value.rb', line 108

def respond_to_missing?(meth, include_private = false)
  super || meth.to_s.end_with?(QUESTION_MARK)
end