Class: Trane::FieldBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/trane/field_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFieldBuilder

Returns a new instance of FieldBuilder.



7
8
9
# File 'lib/trane/field_builder.rb', line 7

def initialize
  @fields = []
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/trane/field_builder.rb', line 5

def fields
  @fields
end

Instance Method Details

#field(name, type: nil, extra: false, format: nil, of: nil, enum: nil) { ... } ⇒ Object

Define a field in the current context.

Parameters:

  • name (Symbol)

    field name

  • type (Symbol, nil) (defaults to: nil)

    field type keyword — mandatory unless of: or a block is provided

  • extra (Boolean) (defaults to: false)

    whether this is an optional extra field

  • format (Symbol, nil) (defaults to: nil)

    format hint (e.g., :iso8601)

  • of (Symbol, nil) (defaults to: nil)

    element type for arrays (infers type: :array)

  • enum (Array, nil) (defaults to: nil)

    set of allowed values; only valid for scalar primitive types

Yields:

  • optional block for inline nested fields (infers type: :object)



20
21
22
23
24
25
26
# File 'lib/trane/field_builder.rb', line 20

def field(name, type: nil, extra: false, format: nil, of: nil, enum: nil, &block)
  @fields << _build_field_node(
    name: name, type: type, extra: extra, format: format,
    of: of, enum: enum, child_builder_class: FieldBuilder,
    required: nil, &block
  )
end