Module: SoberSwag::Reporting::Output::Interface

Included in:
Base, Struct
Defined in:
lib/sober_swag/reporting/output/interface.rb

Overview

Interface methods for all outputs.

Instance Method Summary collapse

Instance Method Details

#call!(item) ⇒ Object

Raises:



7
8
9
10
11
12
13
# File 'lib/sober_swag/reporting/output/interface.rb', line 7

def call!(item)
  res = serialize_report(item)

  raise Report::Error.new(res) if res.is_a?(Report::Base) # rubocop:disable Style/RaiseArgs

  res
end

#described(description) ⇒ Object



90
91
92
# File 'lib/sober_swag/reporting/output/interface.rb', line 90

def described(description)
  Described.new(self, description)
end

#enum(*cases) ⇒ SoberSwag::Reporting::Output::Enum



35
36
37
# File 'lib/sober_swag/reporting/output/interface.rb', line 35

def enum(*cases)
  Enum.new(self, cases)
end

#in_range(range) ⇒ SoberSwag::Reporting::Output::InRange

Constrained values: must be within the given range.

Returns:

Raises:

  • (ArgumentError)


46
47
48
49
50
# File 'lib/sober_swag/reporting/output/interface.rb', line 46

def in_range(range)
  raise ArgumentError, 'need a range' unless range.is_a?(Range)

  InRange.new(self, range)
end

#listObject Also known as: array



52
53
54
# File 'lib/sober_swag/reporting/output/interface.rb', line 52

def list
  List.new(self)
end

#nilableObject



82
83
84
85
86
87
88
# File 'lib/sober_swag/reporting/output/interface.rb', line 82

def nilable
  Partitioned.new(
    :nil?.to_proc,
    Null.new,
    self
  )
end

#partitioned(other, &block) ⇒ Interface

Partition this serializer into two potentials. If the block given returns false, we will use other as the serializer. Otherwise, we will use self.

This might be useful to serialize a sum type:

ResolutionOutput = TransferOutput.partitioned(RefundOutput) { |to_serialize| to_serialize.is_a?(Transfer)

Parameters:

  • other (Interface)

    serializer to use if the block returns false

Yield Returns:

  • (true, false)

    false if we should use the other serializer

Returns:

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
80
# File 'lib/sober_swag/reporting/output/interface.rb', line 72

def partitioned(other, &block)
  raise ArgumentError, 'need a block' if block.nil?

  Partitioned.new(
    block,
    self,
    other
  )
end

#referenced(name) ⇒ Object



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

def referenced(name)
  Referenced.new(self, name)
end

#reporting?Boolean

Show off that this is a reporting output.

Returns:

  • (Boolean)


17
18
19
# File 'lib/sober_swag/reporting/output/interface.rb', line 17

def reporting?
  true
end

#serialize(item) ⇒ Object

Delegates to #call



23
24
25
# File 'lib/sober_swag/reporting/output/interface.rb', line 23

def serialize(item)
  call(item)
end

#via_map(&block) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
# File 'lib/sober_swag/reporting/output/interface.rb', line 27

def via_map(&block)
  raise ArgumentError, 'block argument required' unless block

  ViaMap.new(self, block)
end