Class: SoberSwag::OutputObject::Definition
- Inherits:
-
Object
- Object
- SoberSwag::OutputObject::Definition
- 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
- #fields ⇒ Array<SoberSwag::OutputObject::Field> readonly
- #views ⇒ Array<SoberSwag::OutputObject::View> readonly
Instance Method Summary collapse
-
#add_field!(field) ⇒ Object
Adds a new field to the fields array.
-
#find_view(name) ⇒ SoberSwag::OutputObject::View
private
Get the already-defined view with a specific name.
- #identifier(arg = nil) ⇒ Object
-
#initialize ⇒ Definition
constructor
A new instance of Definition.
-
#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.
-
#view(name, inherits: nil, &block) ⇒ nil
Define a new view, with the view DSL.
Methods included from FieldSyntax
#field, #merge, #multi, #primitive
Constructor Details
#initialize ⇒ Definition
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
#fields ⇒ Array<SoberSwag::OutputObject::Field> (readonly)
14 15 16 |
# File 'lib/sober_swag/output_object/definition.rb', line 14 def fields @fields end |
#views ⇒ Array<SoberSwag::OutputObject::View> (readonly)
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
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.
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 |
#identifier ⇒ String #identifier(arg) ⇒ String
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
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
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 |