Class: SoberSwag::Serializer::FieldList

Inherits:
Base
  • Object
show all
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

Instance Method Summary collapse

Methods inherited from Base

#array, #identifier, #meta, #optional, #serializer, #via_map

Constructor Details

#initialize(field_list) ⇒ FieldList

Create a new field-list serializer.

Parameters:



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_listArray<SoberSwag::OutputObject::Field> (readonly)

Returns the list of fields to use.

Returns:



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_typeObject



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.

Returns:

  • (Boolean)


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.

Parameters:

  • object (Object)

    object to serialize

  • options (Hash) (defaults to: {})

    arbitrary options

Returns:

  • (Hash)

    serialized object.



29
30
31
32
33
34
35
# File 'lib/sober_swag/serializer/field_list.rb', line 29

def serialize(object, options = {})
  {}.tap do |hash|
    field_list.each do |field|
      hash[field.name] = field.serializer.serialize(object, options)
    end
  end
end

#struct_classObject (private)



80
81
82
# File 'lib/sober_swag/serializer/field_list.rb', line 80

def struct_class
  @struct_class ||= Class.new(SoberSwag::InputObject)
end

#typeDry::Struct

Construct a Dry::Struct from the fields given. This Struct will be swagger-able.

Returns:

  • (Dry::Struct)


41
42
43
# File 'lib/sober_swag/serializer/field_list.rb', line 41

def type
  @type ||= make_struct_type!
end