Class: SoberSwag::Reporting::Output::MergeObjects
- 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
-
#child ⇒ Interface
readonly
Second object to merge.
-
#parent ⇒ Interface
readonly
First object to merge.
Instance Method Summary collapse
-
#call(input) ⇒ Object
Serialize with the parent first, then merge in the child.
-
#initialize(parent, child) ⇒ MergeObjects
constructor
A new instance of MergeObjects.
- #merge_results(par, chi) ⇒ Object private
- #serialize_report(value) ⇒ Object
-
#swagger_schema ⇒ Object
Swagger schema.
-
#view(view) ⇒ Object
Passes on view to the child object.
-
#views ⇒ Object
Child views.
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.
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
#child ⇒ Interface (readonly)
Returns second object to merge.
32 33 34 |
# File 'lib/sober_swag/reporting/output/merge_objects.rb', line 32 def child @child end |
#parent ⇒ Interface (readonly)
Returns first object to merge.
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_schema ⇒ Object
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 |
#views ⇒ Object
Child views.
45 46 47 |
# File 'lib/sober_swag/reporting/output/merge_objects.rb', line 45 def views child.views end |