module SoberSwag::OutputObject::FieldSyntax
Syntax for definitions that can add fields.
Public Instance Methods
Defines a new field. @see SoberSwag::OutputObject::Field#initialize @param name [Symbol] name of this field @param serializer [SoberSwag::Serializer::Base] serializer to use for this field. @param from [Symbol] method name to extract this field from, for convenience. @param block [Proc] optional way to extract this field.
# File lib/sober_swag/output_object/field_syntax.rb, line 13 def field(name, serializer, from: nil, &block) add_field!(Field.new(name, serializer, from: from, &block)) end
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.
@param other [#fields] a field container, like a {SoberSwag::OutputObject} or something @param except [Array<Symbol>] optionally exclude a field from the output object being merged @return [void]
# File lib/sober_swag/output_object/field_syntax.rb, line 42 def merge(other, except: []) other.fields.each do |field| add_field!(field) unless except.include?(field.name) end end
Similar to field
, but adds multiple at once. Named multi
because fields was already taken.
@param names [Array<Symbol>] the field names to add. @param serializer [SoberSwag::Serializer::Base] the serializer to use for all fields.
# File lib/sober_swag/output_object/field_syntax.rb, line 23 def multi(names, serializer) names.each { |name| field(name, serializer) } end
Given a symbol to this, we will use a primitive name @param name [Symbol] symbol to look up. @return [SoberSwag::Serializer::Base] serializer to use.
# File lib/sober_swag/output_object/field_syntax.rb, line 31 def primitive(name) SoberSwag::Serializer.primitive(SoberSwag::Types.const_get(name)) end