Class: SoberSwag::Serializer::FieldList
- Defined in:
- lib/sober_swag/serializer/field_list.rb
Overview
Extracts a JSON hash from a list of OutputObject::Field structs.
Instance Attribute Summary collapse
-
#field_list ⇒ Array<SoberSwag::OutputObject::Field>
readonly
The list of fields to use.
Instance Method Summary collapse
- #finalize_lazy_type! ⇒ Object
-
#initialize(field_list) ⇒ FieldList
constructor
Create a new field-list serializer.
- #lazy_type ⇒ Object
-
#lazy_type? ⇒ Boolean
These types are always constructed lazily.
-
#make_struct_type! ⇒ Object
private
rubocop:disable Metrics/MethodLength.
-
#primitive(symbol) ⇒ Object
Alias to make writing primitive stuff much easier.
-
#serialize(object, options = {}) ⇒ Hash
Serialize an object to a JSON hash by using each field in the list.
- #struct_class ⇒ Object private
-
#type ⇒ Dry::Struct
Construct a Dry::Struct from the fields given.
Methods inherited from Base
#array, #identifier, #meta, #optional, #serializer, #via_map
Constructor Details
#initialize(field_list) ⇒ FieldList
Create a new field-list serializer.
10 11 12 |
# File 'lib/sober_swag/serializer/field_list.rb', line 10 def initialize(field_list) @field_list = field_list end |
Instance Attribute Details
#field_list ⇒ Array<SoberSwag::OutputObject::Field> (readonly)
Returns the list of fields to use.
16 17 18 |
# File 'lib/sober_swag/serializer/field_list.rb', line 16 def field_list @field_list end |
Instance Method Details
#finalize_lazy_type! ⇒ Object
55 56 57 |
# File 'lib/sober_swag/serializer/field_list.rb', line 55 def finalize_lazy_type! make_struct_type! end |
#lazy_type ⇒ Object
51 52 53 |
# File 'lib/sober_swag/serializer/field_list.rb', line 51 def lazy_type struct_class end |
#lazy_type? ⇒ Boolean
These types are always constructed lazily.
47 48 49 |
# File 'lib/sober_swag/serializer/field_list.rb', line 47 def lazy_type? true end |
#make_struct_type! ⇒ Object (private)
rubocop:disable Metrics/MethodLength
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/sober_swag/serializer/field_list.rb', line 61 def make_struct_type! # rubocop:disable Metrics/MethodLength # mutual recursion makes this really, really annoying. return struct_class if @made_struct_type f = field_list s = identifier struct_class.instance_eval do identifier(s) f.each do |field| attribute field.name, field.serializer.lazy_type end end @made_struct_type = true field_list.map(&:serializer).each(&:finalize_lazy_type!) struct_class end |
#primitive(symbol) ⇒ Object
Alias to make writing primitive stuff much easier
20 21 22 |
# File 'lib/sober_swag/serializer/field_list.rb', line 20 def primitive(symbol) SoberSwag::Serializer.Primitive(SoberSwag::Types.const_get(symbol)) end |
#serialize(object, options = {}) ⇒ Hash
Serialize an object to a JSON hash by using each field in the list.
29 30 31 32 33 34 35 |
# File 'lib/sober_swag/serializer/field_list.rb', line 29 def serialize(object, = {}) {}.tap do |hash| field_list.each do |field| hash[field.name] = field.serializer.serialize(object, ) end end end |
#struct_class ⇒ Object (private)
80 81 82 |
# File 'lib/sober_swag/serializer/field_list.rb', line 80 def struct_class @struct_class ||= Class.new(SoberSwag::InputObject) end |
#type ⇒ Dry::Struct
Construct a Dry::Struct from the fields given. This Struct will be swagger-able.
41 42 43 |
# File 'lib/sober_swag/serializer/field_list.rb', line 41 def type @type ||= make_struct_type! end |