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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dry/mutations/dsl/types.rb', line 38

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

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

  case
  when !params[:class].nil? # cb.nil?
    type = Utils.Type(params.delete(:class))
    schema { Utils.smart_send(__send__(current, name), *(type ? [:each, type] : [:maybe])) }
  when name.nil? then schema { each(nested) } # FIXME: CAN IT BE NIL?!
  else schema { __send__(current, name).each(nested) }
  end

  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



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dry/mutations/dsl/types.rb', line 66

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 = nil, **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
34
35
# File 'lib/dry/mutations/dsl/types.rb', line 23

def hash name = nil, **params, &cb
  return super() if name.nil? # HACK: for method name collision

  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`



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

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