Class: SoberSwag::Reporting::Output::Viewed

Inherits:
Base
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • views (Hash<Symbol,Interface>)

    a map of view key to view. Note: this map must include the base view.

Raises:

  • (ArgumentError)


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_mapObject (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.

Parameters:

  • input (Object)

    object to serialize

  • view (Symbol) (defaults to: :base)

    which view to use. If view is not valid, an exception will be thrown

Returns:

  • (Object, String, Array, Numeric)

    JSON-serializable object. Suitable for use with #to_json.

Raises:

  • (KeyError)

    if view is not valid



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_schemaObject



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

#viewsSet<Symbol>

Returns all of the views applicable.

Returns:

  • (Set<Symbol>)

    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

#with_view(name, val) ⇒ Viewed

Add (or override) the possible views.

Returns:

  • (Viewed)

    a new view map, with one more view.



56
57
58
# File 'lib/sober_swag/reporting/output/viewed.rb', line 56

def with_view(name, val)
  Viewed.new(views.merge(name => val))
end