Module: SoberSwag::OutputObject::FieldSyntax

Included in:
Definition, View
Defined in:
lib/sober_swag/output_object/field_syntax.rb

Overview

Syntax for definitions that can add fields.

Instance Method Summary collapse

Instance Method Details

#field(name, serializer, from: nil, &block) ⇒ Object

Defines a new field.

Parameters:

  • name (Symbol)

    name of this field

  • serializer (SoberSwag::Serializer::Base)

    serializer to use for this field.

  • from (Symbol) (defaults to: nil)

    method name to extract this field from, for convenience.

  • block (Proc)

    optional way to extract this field.

Raises:

  • (ArgumentError)

See Also:



13
14
15
16
17
# File 'lib/sober_swag/output_object/field_syntax.rb', line 13

def field(name, serializer, from: nil, &block)
  raise ArgumentError, "do not mix reporting and non-reporting outputs (at key #{name})" if serializer.is_a?(SoberSwag::Reporting::Output::Interface)

  add_field!(Field.new(name, serializer, from: from, &block))
end

#merge(other, except: []) ⇒ void

This method returns an undefined value.

Merge in anything that has a list of fields, and use it. Note that merging in a full output object will not also merge in views, just fields defined on the base.

Parameters:

  • other (#fields)

    a field container, like a SoberSwag::OutputObject or something

  • except (Array<Symbol>) (defaults to: [])

    optionally exclude a field from the output object being merged



44
45
46
47
48
# File 'lib/sober_swag/output_object/field_syntax.rb', line 44

def merge(other, except: [])
  other.fields.each do |field|
    add_field!(field) unless except.include?(field.name)
  end
end

#multi(names, serializer) ⇒ Object

Similar to #field, but adds multiple at once. Named #multi because #fields was already taken.

Parameters:



25
26
27
# File 'lib/sober_swag/output_object/field_syntax.rb', line 25

def multi(names, serializer)
  names.each { |name| field(name, serializer) }
end

#primitive(name) ⇒ SoberSwag::Serializer::Base

Given a symbol to this, we will use a primitive name

Parameters:

  • name (Symbol)

    symbol to look up.

Returns:



33
34
35
# File 'lib/sober_swag/output_object/field_syntax.rb', line 33

def primitive(name)
  SoberSwag::Serializer.primitive(SoberSwag::Types.const_get(name))
end