class SoberSwag::Reporting::Output::MergeObjects

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 {SoberSwag::Reporting::Output::Struct}, in order to model the inheritence relationship structs have.

Attributes

child[R]

@return [Interface] second object to merge

parent[R]

@return [Interface] first object to merge

Public Class Methods

new(parent, child) click to toggle source

@param parent [Interface] parent interface to use.

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

@param child [Interface] child interface to use.

Should certainly be some sort of object, or a reference to it.
# File lib/sober_swag/reporting/output/merge_objects.rb, line 22
def initialize(parent, child)
  @parent = parent
  @child = child
end

Public Instance Methods

call(input) click to toggle source

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.

# File lib/sober_swag/reporting/output/merge_objects.rb, line 39
def call(input)
  parent.call(input).merge(child.call(input))
end
serialize_report(value) click to toggle source
# 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_schema() click to toggle source

Swagger schema.

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

# 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) click to toggle source

Passes on view to the *child object*.

# File lib/sober_swag/reporting/output/merge_objects.rb, line 51
def view(view)
  MergeObjects.new(parent, child.view(view))
end
views() click to toggle source

Child views.

# File lib/sober_swag/reporting/output/merge_objects.rb, line 45
def views
  child.views
end

Private Instance Methods

merge_results(par, chi) click to toggle source
# 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