class SoberSwag::Reporting::Output::Partitioned

Partition output into one of two possible cases. We use a block to decide if we should use the first or the second. If the block returns a truthy value, we use the first output. If it returns a falsy value, we use the second.

This is useful to serialize sum types, or types where it can be EITHER one thing OR another. IE, if I can resolve a dispute by EITHER transfering money OR refunding a customer, I can do this:

“`ruby ResolutionOutput = SoberSwag::Reporting::Output.new(

proc { |x| x.is_a?(Transfer) },
TransferOutput,
RefundOutput

) “`

Attributes

false_output[R]

@return [Interface]

partition[R]

@return [#call] partitioning block

true_output[R]

@return [Interface]

Public Class Methods

new(partition, true_output, false_output) click to toggle source

@param partition [#call] block that returns true or false for the input type @param true_output [Interface] serializer to use if block is true @param false_output [Interface] serializer to use if block is false

# File lib/sober_swag/reporting/output/partitioned.rb, line 25
def initialize(partition, true_output, false_output)
  @partition = partition
  @true_output = true_output
  @false_output = false_output
end

Public Instance Methods

call(item) click to toggle source
# File lib/sober_swag/reporting/output/partitioned.rb, line 43
def call(item)
  serializer_for(item).call(item)
end
serialize_report(item) click to toggle source
# File lib/sober_swag/reporting/output/partitioned.rb, line 47
def serialize_report(item)
  serializer_for(item).serialize_report(item)
end
swagger_schema() click to toggle source
# File lib/sober_swag/reporting/output/partitioned.rb, line 51
def swagger_schema
  true_schema, true_found = true_output.swagger_schema
  false_schema, false_found = false_output.swagger_schema

  [
    {
      oneOf: (true_schema[:oneOf] || [true_schema]) + (false_schema[:oneOf] || [false_schema])
    },
    true_found.merge(false_found)
  ]
end

Private Instance Methods

serializer_for(item) click to toggle source

@return [Interface]

# File lib/sober_swag/reporting/output/partitioned.rb, line 67
def serializer_for(item)
  if partition.call(item)
    true_output
  else
    false_output
  end
end