Class: SoberSwag::OutputObject::Definition

Inherits:
Object
  • Object
show all
Includes:
FieldSyntax
Defined in:
lib/sober_swag/output_object/definition.rb

Overview

Container to define a single output object. This is the DSL used in the base of define.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FieldSyntax

#field, #merge, #multi, #primitive

Constructor Details

#initializeDefinition

Returns a new instance of Definition.



7
8
9
10
# File 'lib/sober_swag/output_object/definition.rb', line 7

def initialize
  @fields = []
  @views = []
end

Instance Attribute Details

#fieldsArray<SoberSwag::OutputObject::Field> (readonly)

Returns:



14
15
16
# File 'lib/sober_swag/output_object/definition.rb', line 14

def fields
  @fields
end

#viewsArray<SoberSwag::OutputObject::View> (readonly)

Returns:



18
19
20
# File 'lib/sober_swag/output_object/definition.rb', line 18

def views
  @views
end

Instance Method Details

#add_field!(field) ⇒ Object

Adds a new field to the fields array

Parameters:



51
52
53
# File 'lib/sober_swag/output_object/definition.rb', line 51

def add_field!(field)
  @fields << field
end

#find_view(name) ⇒ SoberSwag::OutputObject::View (private)

Get the already-defined view with a specific name.

Parameters:

  • name (Symbol)

    name of view to look up

Returns:

Raises:

  • (ArgumentError)

    if no view with that name found



97
98
99
# File 'lib/sober_swag/output_object/definition.rb', line 97

def find_view(name)
  @views.find { |view| view.name == name } || (raise ArgumentError, "no view #{name.inspect} defined!")
end

#identifierString #identifier(arg) ⇒ String

Overloads:

  • #identifierString

    Get the identifier of this output object.

    Returns:

    • (String)

      the identifier

  • #identifier(arg) ⇒ String

    Set the identifier of this output object.

    Parameters:

    • arg (String)

      the external identifier to use for this OutputObject

    Returns:

    • (String)

      arg



84
85
86
87
# File 'lib/sober_swag/output_object/definition.rb', line 84

def identifier(arg = nil)
  @identifier = arg if arg
  @identifier
end

#type_key(str, field_name: :type) ⇒ Object

Adds a "type key", which is basically a field that will be serialized out to a constant value.

This is useful if you have multiple types you may want to serialize out, and want consumers of your API to distinguish between them.

By default this will have the key "type" but you can set it with the keyword arg.

  type_key('MyObject')
  # is equivalent to
  field :type, SoberSwag::Serializer::Primitive.new(SoberSwag::Types::String.enum('MyObject')) do |_, _|
  'MyObject'
  end

Parameters:

  • str (String, Symbol)

    the value to serialize (will be converted to a string)



38
39
40
41
42
43
44
45
46
# File 'lib/sober_swag/output_object/definition.rb', line 38

def type_key(str, field_name: :type)
  str = str.to_s
  field(
    field_name,
    SoberSwag::Serializer::Primitive.new(
      SoberSwag::Types::String.enum(str)
    )
  ) { |_, _| str }
end

#view(name, inherits: nil, &block) ⇒ nil

Define a new view, with the view DSL

Parameters:

  • name (Symbol)

    the name of the view

  • inherits (Symbol) (defaults to: nil)

    the name of another view this view will "inherit" from

Returns:

  • (nil)

    nothing interesting.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sober_swag/output_object/definition.rb', line 62

def view(name, inherits: nil, &block)
  initial_fields =
    if inherits.nil? || inherits == :base
      fields
    else
      find_view(inherits).fields
    end
  view = View.define(name, initial_fields, &block)

  view.identifier("#{@identifier}.#{name.to_s.classify}") if identifier

  @views << view
end