Class: SoberSwag::Reporting::Output::Object

Inherits:
Base
  • Object
show all
Defined in:
lib/sober_swag/reporting/output/object.rb,
lib/sober_swag/reporting/output/object/property.rb

Overview

Serialize out a JSON object.

Defined Under Namespace

Classes: Property

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#view, #views

Methods included from Interface

#call!, #described, #enum, #in_range, #list, #nilable, #partitioned, #referenced, #reporting?, #serialize, #via_map

Constructor Details

#initialize(properties) ⇒ Object

Returns a new instance of Object.

Parameters:

  • properties (Hash<Symbol,Property>)

    the properties to serialize



11
12
13
# File 'lib/sober_swag/reporting/output/object.rb', line 11

def initialize(properties)
  @properties = properties
end

Instance Attribute Details

#propertiesObject (readonly)

Parameters:



17
18
19
# File 'lib/sober_swag/reporting/output/object.rb', line 17

def properties
  @properties
end

Instance Method Details

#call(item) ⇒ Object



19
20
21
22
23
# File 'lib/sober_swag/reporting/output/object.rb', line 19

def call(item)
  properties.each.with_object({}) do |(k, v), hash|
    hash[k] = v.output.call(item)
  end
end

#serialize_report(item) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/sober_swag/reporting/output/object.rb', line 25

def serialize_report(item)
  bad, good = properties.map { |k, prop|
    [k, prop.output.serialize_report(item)]
  }.partition { |(_, v)| v.is_a?(Report::Base) }

  return Report::Object.new(bad.to_h) if bad.any?

  good.to_h
end

#swagger_schemaObject

rubocop:disable Metrics/MethodLength



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sober_swag/reporting/output/object.rb', line 35

def swagger_schema # rubocop:disable Metrics/MethodLength
  props, found = properties.each.with_object([{}, {}]) do |(k, v), (field, f)|
    prop_type, prop_found = v.property_schema
    field[k] = prop_type
    f.merge!(prop_found)
  end

  [
    {
      type: 'object',
      properties: props,
      required: properties.keys
    },
    found
  ]
end