Module: Dry::Mutations::DSL::Types

Defined in:
lib/dry/mutations/dsl/types.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Nested

Instance Method Summary collapse

Instance Method Details

#array(name, **params, &cb) ⇒ Object

FIXME: array of anonymous objects



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dry/mutations/dsl/types.rb', line 36

def array name, **params, &cb
  current = @current # closure scope

  nested =  begin
              Nested.!(current, &cb)
            rescue Errors::AnonymousTypeDetected => err
              Utils.Type err.type
            end

  name.nil? ? schema { each(nested) } : schema { __send__(current, name).each(nested) }

  case
  when params[:discard_empty] then schema.discarded!(name)
  when Nested === self then # do nothing
  else define_method(name) { @inputs[name] }
  end
end

#duck(name, **params) ⇒ Object

custom types



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

def duck name, **params
  # <<- CLOSURE_SCOPE
  current = @current
  params = @environs.merge params if @environs
  filled_or_maybe = optionality(params)
  # CLOSURE_SCOPE

  schema do
    __send__(current, name).__send__(filled_or_maybe, duck?: [*params[:methods]])
  end

  define_helper_methods name
end

#hash(name, **params, &cb) ⇒ Object

FIXME: errors in double+ nested hashes are not nested! dry-rb glitch?



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dry/mutations/dsl/types.rb', line 23

def hash name, **params, &cb
  current = @current # closure scope

  schema { __send__(current, name).schema(Nested.!(current, &cb)) }

  case
  when params[:discard_empty] then schema.discarded!(name)
  when Nested === self then # do nothing
  else define_method(name) { Utils::Hash(@inputs[name]) }
  end
end

#model(name, **params) ⇒ Object

possible params: ‘class: nil, builder: nil, new_records: false`



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dry/mutations/dsl/types.rb', line 73

def model name, **params
  # <<- CLOSURE_SCOPE
  current = @current # closure scope
  params = @environs.merge params if @environs
  filled_or_maybe = optionality(params)
  params[:class] ||= name.to_s.gsub(/(?:\A|_)./) { |m| m[-1].upcase }
  # CLOSURE_SCOPE

  schema do
    __send__(current, name).__send__(filled_or_maybe, model?: params[:class])
  end

  define_helper_methods name
end