Class: SoberSwag::Reporting::Output::MergeObjects

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

Overview

Represents object that are marged with allOf in swagger.

These have to be objects, due to how allOf works. This expresses a subtyping relationship.

Note: non-careful use of this can generate impossible objects, IE, objects where a certain field has to be both a string and an integer or something. Subtyping is dangerous and should be used with care!

This class is used in the implementation of Struct, in order to model the inheritence relationship structs have.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interface

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

Constructor Details

#initialize(parent, child) ⇒ MergeObjects

Returns a new instance of MergeObjects.

Parameters:

  • parent (Interface)

    parent interface to use. Should certainly be some sort of object, or a reference to it.

  • child (Interface)

    child interface to use. Should certainly be some sort of object, or a reference to it.



22
23
24
25
# File 'lib/sober_swag/reporting/output/merge_objects.rb', line 22

def initialize(parent, child)
  @parent = parent
  @child = child
end

Instance Attribute Details

#childInterface (readonly)

Returns second object to merge.

Returns:



32
33
34
# File 'lib/sober_swag/reporting/output/merge_objects.rb', line 32

def child
  @child
end

#parentInterface (readonly)

Returns first object to merge.

Returns:



29
30
31
# File 'lib/sober_swag/reporting/output/merge_objects.rb', line 29

def parent
  @parent
end

Instance Method Details

#call(input) ⇒ Object

Serialize with the parent first, then merge in the child. This does mean that parent keys override child keys.

If parent or child does not serialize some sort of object, this will result in an error.



39
40
41
# File 'lib/sober_swag/reporting/output/merge_objects.rb', line 39

def call(input)
  parent.call(input).merge(child.call(input))
end

#merge_results(par, chi) ⇒ Object (private)



87
88
89
90
91
92
93
# File 'lib/sober_swag/reporting/output/merge_objects.rb', line 87

def merge_results(par, chi)
  return Report::MergedObject.new(par, chi) if [par, chi].all? { |c| c.is_a?(Report::Base) }
  return par if par.is_a?(Report::Base)
  return chi if chi.is_a?(Report::Base)

  par.to_h.merge(chi.to_h)
end

#serialize_report(value) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sober_swag/reporting/output/merge_objects.rb', line 55

def serialize_report(value)
  parent_attrs = parent.serialize_report(value)

  return parent_attrs if parent_attrs.is_a?(Report::Value)

  child_attrs = child.serialize_report(value)

  return child_attrs if child_attrs.is_a?(Report::Value)

  merge_results(parent_attrs, child_attrs)
end

#swagger_schemaObject

Swagger schema.

This will collapse 'allOf' keys, so a chain of parent methods will be



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sober_swag/reporting/output/merge_objects.rb', line 71

def swagger_schema # rubocop:disable Metrics/MethodLength
  found = {}
  mapped = [parent, child].flat_map do |i|
    schema, item_found = i.swagger_schema
    found.merge!(item_found)
    if schema.key?(:allOf)
      schema[:allOf]
    else
      [schema]
    end
  end
  [{ allOf: mapped }, found]
end

#view(view) ⇒ Object

Passes on view to the child object.



51
52
53
# File 'lib/sober_swag/reporting/output/merge_objects.rb', line 51

def view(view)
  MergeObjects.new(parent, child.view(view))
end

#viewsObject

Child views.



45
46
47
# File 'lib/sober_swag/reporting/output/merge_objects.rb', line 45

def views
  child.views
end