Class: SoberSwag::Reporting::Output::Partitioned

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

Overview

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:

ResolutionOutput = SoberSwag::Reporting::Output.new(
  proc { |x| x.is_a?(Transfer) },
  TransferOutput,
  RefundOutput
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#view, #views

Methods included from Interface

#call!, #described, #enum, #in_range, #list, #nilable, #partitioned, #referenced, #reporting?, #serialize, #via_map

Constructor Details

#initialize(partition, true_output, false_output) ⇒ Partitioned

Returns a new instance of Partitioned.

Parameters:

  • partition (#call)

    block that returns true or false for the input type

  • true_output (Interface)

    serializer to use if block is true

  • false_output (Interface)

    serializer to use if block is false



25
26
27
28
29
# 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

Instance Attribute Details

#false_outputInterface (readonly)

Returns:



41
42
43
# File 'lib/sober_swag/reporting/output/partitioned.rb', line 41

def false_output
  @false_output
end

#partition#call (readonly)

Returns partitioning block.

Returns:

  • (#call)

    partitioning block



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

def partition
  @partition
end

#true_outputInterface (readonly)

Returns:



37
38
39
# File 'lib/sober_swag/reporting/output/partitioned.rb', line 37

def true_output
  @true_output
end

Instance Method Details

#call(item) ⇒ Object



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

def call(item)
  serializer_for(item).call(item)
end

#serialize_report(item) ⇒ Object



47
48
49
# File 'lib/sober_swag/reporting/output/partitioned.rb', line 47

def serialize_report(item)
  serializer_for(item).serialize_report(item)
end

#serializer_for(item) ⇒ Interface (private)

Returns:



67
68
69
70
71
72
73
# 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

#swagger_schemaObject



51
52
53
54
55
56
57
58
59
60
61
# 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