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
-
#field(name, serializer, from: nil, &block) ⇒ Object
Defines a new field.
-
#merge(other, except: []) ⇒ void
Merge in anything that has a list of fields, and use it.
-
#multi(names, serializer) ⇒ Object
Similar to #field, but adds multiple at once.
-
#primitive(name) ⇒ SoberSwag::Serializer::Base
Given a symbol to this, we will use a primitive name.
Instance Method Details
#field(name, serializer, from: nil, &block) ⇒ Object
Defines a new field.
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.
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.
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
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 |