Class: Dry::Struct::StructBuilder

Inherits:
Compiler
  • Object
show all
Defined in:
lib/dry/struct/struct_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Compiler

#visit_struct

Constructor Details

#initialize(struct) ⇒ StructBuilder

Returns a new instance of StructBuilder.



9
10
11
12
# File 'lib/dry/struct/struct_builder.rb', line 9

def initialize(struct)
  super(Types)
  @struct = struct
end

Instance Attribute Details

#structObject (readonly)

Returns the value of attribute struct.



7
8
9
# File 'lib/dry/struct/struct_builder.rb', line 7

def struct
  @struct
end

Instance Method Details

#call(attr_name, type) { ... } ⇒ Object

Parameters:

  • attr_name (Symbol|String)

    the name of the nested type

  • type (Dry::Struct, Dry::Types::Type::Array, Undefined)

    the superclass of the nested struct

Yields:

  • the body of the nested struct



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
# File 'lib/dry/struct/struct_builder.rb', line 18

def call(attr_name, type, &block)
  const_name = const_name(type, attr_name)
  check_name(const_name)

  builder = self
  parent = parent(type)

  new_type = ::Class.new(Undefined.default(parent, struct.abstract_class)) do
    if Undefined.equal?(parent)
      schema builder.struct.schema.clear
    end

    class_exec(&block)
  end

  struct.const_set(const_name, new_type)

  if array?(type)
    type.of(new_type)
  elsif optional?(type)
    new_type.optional
  else
    new_type
  end
end