Class: SoberSwag::Reporting::Input::Either

Inherits:
Base
  • Object
show all
Defined in:
lib/sober_swag/reporting/input/either.rb

Overview

Parses either one input, or another. Left-biased.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interface

#add_schema_key, #call!, #described, #enum, #format, #in_range, #list, #mapped, #modify_schema, #multiple_of, #optional, #or, #referenced, #swagger_path_schema, #swagger_query_schema, #|

Constructor Details

#initialize(lhs, rhs) ⇒ Either

Returns a new instance of Either.

Parameters:

  • lhs (Base)

    an input we will try first

  • rhs (Base)

    an input we will try second



11
12
13
14
# File 'lib/sober_swag/reporting/input/either.rb', line 11

def initialize(lhs, rhs)
  @lhs = lhs
  @rhs = rhs
end

Instance Attribute Details

#lhsBase (readonly)

Returns parser for LHS.

Returns:

  • (Base)

    parser for LHS



18
19
20
# File 'lib/sober_swag/reporting/input/either.rb', line 18

def lhs
  @lhs
end

#rhsBase (readonly)

Returns parser for RHS.

Returns:

  • (Base)

    parser for RHS



21
22
23
# File 'lib/sober_swag/reporting/input/either.rb', line 21

def rhs
  @rhs
end

Instance Method Details

#call(value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sober_swag/reporting/input/either.rb', line 23

def call(value)
  maybe_lhs = lhs.call(value)

  return maybe_lhs unless maybe_lhs.is_a?(Report::Base)

  maybe_rhs = rhs.call(value)

  return maybe_rhs unless maybe_rhs.is_a?(Report::Base)

  Report::Either.new(maybe_lhs, maybe_rhs)
end

#defs_for(schema) ⇒ Object (private)



45
46
47
# File 'lib/sober_swag/reporting/input/either.rb', line 45

def defs_for(schema)
  schema[:oneOf] || [schema]
end

#swagger_schemaObject



35
36
37
38
39
40
41
# File 'lib/sober_swag/reporting/input/either.rb', line 35

def swagger_schema
  lhs_val, lhs_set = lhs.swagger_schema
  rhs_val, rhs_set = rhs.swagger_schema

  val = { oneOf: defs_for(lhs_val) + defs_for(rhs_val) }
  [val, lhs_set.merge(rhs_set)]
end