class SoberSwag::Reporting::Output::Viewed

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.

Attributes

view_map[R]

Public Class Methods

new(views) click to toggle source

@param views [Hash<Symbol,Interface>] a map of view key to view.

Note: this map *must* include the base view.
# 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

Public Instance Methods

call(input, view: :base) click to toggle source

Serialize out an object. If the view key is not provided, use the base view.

@param input [Object] object to serialize @param view [Symbol] which view to use.

If view is not valid, an exception will be thrown

@raise [KeyError] if view is not valid @return [Object,String,Array,Numeric] JSON-serializable object.

Suitable for use with #to_json.
# File lib/sober_swag/reporting/output/viewed.rb, line 32
def call(input, view: :base)
  view(view).call(input)
end
serialize_report(input) click to toggle source
# File lib/sober_swag/reporting/output/viewed.rb, line 36
def serialize_report(input)
  view(:base).call(input)
end
swagger_schema() click to toggle source
# 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) click to toggle source

Get a view with a particular key.

# File lib/sober_swag/reporting/output/viewed.rb, line 42
def view(view)
  view_map.fetch(view)
end
views() click to toggle source

@return [Set<Symbol>] all of the views applicable.

# File lib/sober_swag/reporting/output/viewed.rb, line 48
def views
  view_map.keys.to_set
end
with_view(name, val) click to toggle source

Add (or override) the possible views.

@return [Viewed] a new view map, with one more view.

# File lib/sober_swag/reporting/output/viewed.rb, line 56
def with_view(name, val)
  Viewed.new(views.merge(name => val))
end