Class: SoberSwag::Reporting::Output::ViaMap

Inherits:
Base
  • Object
show all
Defined in:
lib/sober_swag/reporting/output/via_map.rb

Overview

Apply a mapping function before calling a base output.

Note that this is applied before the base output. This is different than Input::Mapped, which does the reverse. IE, this class does call block -> pass result to base output, while the other does call serializer -> pass result to block.

If you want to get really nerdy, this is contravariant to Mapped.

This lets you do things like making an output that serializes to strings via to_s:

ToSTextOutput = SoberSwag::Reporting::Output::ViaMap.new(
  SoberSwag::Reporting::Output.text,
  proc { |arg| arg.to_s }
)

class Person
  def to_s
    'Person'
  end
end

ToSTextOutput.call(Person.new) # => 'Person'

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(output, mapper) ⇒ ViaMap

Returns a new instance of ViaMap.



32
33
34
35
# File 'lib/sober_swag/reporting/output/via_map.rb', line 32

def initialize(output, mapper)
  @output = output
  @mapper = mapper
end

Instance Attribute Details

#mapper#call (readonly)

Returns mapping function.

Returns:

  • (#call)

    mapping function



43
44
45
# File 'lib/sober_swag/reporting/output/via_map.rb', line 43

def mapper
  @mapper
end

#outputInterface (readonly)

Returns base output.

Returns:



39
40
41
# File 'lib/sober_swag/reporting/output/via_map.rb', line 39

def output
  @output
end

Instance Method Details

#call(input) ⇒ Object



45
46
47
# File 'lib/sober_swag/reporting/output/via_map.rb', line 45

def call(input)
  output.call(mapper.call(input))
end

#serialize_report(input) ⇒ Object



49
50
51
# File 'lib/sober_swag/reporting/output/via_map.rb', line 49

def serialize_report(input)
  output.serialize_report(mapper.call(input))
end

#swagger_schemaObject



61
62
63
# File 'lib/sober_swag/reporting/output/via_map.rb', line 61

def swagger_schema
  output.swagger_schema
end

#view(view) ⇒ Object



53
54
55
# File 'lib/sober_swag/reporting/output/via_map.rb', line 53

def view(view)
  ViaMap.new(output.view(view), mapper)
end

#viewsObject



57
58
59
# File 'lib/sober_swag/reporting/output/via_map.rb', line 57

def views
  output.views
end