Class: SoberSwag::Reporting::Output::Viewed
- Defined in:
- lib/sober_swag/reporting/output/viewed.rb
Overview
Augment outputs with the ability to select views. This models a 'oneOf' relationship, where the choice picked is controlled by the 'view' parameter.
This is "optional choice," in the sense that you must provide a default :base
key.
This key will be used in almost all cases.
Instance Attribute Summary collapse
-
#view_map ⇒ Object
readonly
Returns the value of attribute view_map.
Instance Method Summary collapse
-
#call(input, view: :base) ⇒ Object, ...
Serialize out an object.
-
#initialize(views) ⇒ Viewed
constructor
A new instance of Viewed.
- #serialize_report(input) ⇒ Object
- #swagger_schema ⇒ Object
-
#view(view) ⇒ Object
Get a view with a particular key.
-
#views ⇒ Set<Symbol>
All of the views applicable.
-
#with_view(name, val) ⇒ Viewed
Add (or override) the possible views.
Methods included from Interface
#call!, #described, #enum, #in_range, #list, #nilable, #partitioned, #referenced, #reporting?, #serialize, #via_map
Constructor Details
#initialize(views) ⇒ Viewed
Returns a new instance of Viewed.
14 15 16 17 18 |
# File 'lib/sober_swag/reporting/output/viewed.rb', line 14 def initialize(views) @view_map = views raise ArgumentError, 'views must have a base key' unless views.key?(:base) end |
Instance Attribute Details
#view_map ⇒ Object (readonly)
Returns the value of attribute view_map.
20 21 22 |
# File 'lib/sober_swag/reporting/output/viewed.rb', line 20 def view_map @view_map end |
Instance Method Details
#call(input, view: :base) ⇒ Object, ...
Serialize out an object. If the view key is not provided, use the base view.
32 33 34 |
# File 'lib/sober_swag/reporting/output/viewed.rb', line 32 def call(input, view: :base) view(view).call(input) end |
#serialize_report(input) ⇒ Object
36 37 38 |
# File 'lib/sober_swag/reporting/output/viewed.rb', line 36 def serialize_report(input) view(:base).call(input) end |
#swagger_schema ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/sober_swag/reporting/output/viewed.rb', line 60 def swagger_schema found = {} possibles = view_map.values.flat_map do |v| view_item, view_found = v.swagger_schema found.merge!(view_found) view_item[:oneOf] || [view_item] end [{ oneOf: possibles }, found] end |
#view(view) ⇒ Object
Get a view with a particular key.
42 43 44 |
# File 'lib/sober_swag/reporting/output/viewed.rb', line 42 def view(view) view_map.fetch(view) end |
#views ⇒ Set<Symbol>
Returns all of the views applicable.
48 49 50 |
# File 'lib/sober_swag/reporting/output/viewed.rb', line 48 def views view_map.keys.to_set end |