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
@return [Interface]
@return [#call] partitioning block
@return [Interface]
Public Class Methods
@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
# File lib/sober_swag/reporting/output/partitioned.rb, line 43 def call(item) serializer_for(item).call(item) end
# File lib/sober_swag/reporting/output/partitioned.rb, line 47 def serialize_report(item) serializer_for(item).serialize_report(item) end
# 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
@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