class SoberSwag::Reporting::Input::Either

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

Attributes

lhs[R]

@return [Base] parser for LHS

rhs[R]

@return [Base] parser for RHS

Public Class Methods

new(lhs, rhs) click to toggle source

@param lhs [Base] an input we will try first @param rhs [Base] an input we will try second

# File lib/sober_swag/reporting/input/either.rb, line 11
def initialize(lhs, rhs)
  @lhs = lhs
  @rhs = rhs
end

Public Instance Methods

call(value) click to toggle source
# 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
swagger_schema() click to toggle source
# 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

Private Instance Methods

defs_for(schema) click to toggle source
# File lib/sober_swag/reporting/input/either.rb, line 45
def defs_for(schema)
  schema[:oneOf] || [schema]
end