Sha256: 642edcdb75daae0cc3f8bcf0a174d27150512984c98c45337d2722e50777c530

Contents?: true

Size: 1.15 KB

Versions: 7

Compression:

Stored size: 1.15 KB

Contents

module SoberSwag
  module Reporting
    module Input
      ##
      # Parses either one input, or another.
      # Left-biased.
      class Either < Base
        ##
        # @param lhs [Base] an input we will try first
        # @param rhs [Base] an input we will try second
        def initialize(lhs, rhs)
          @lhs = lhs
          @rhs = rhs
        end

        ##
        # @return [Base] parser for LHS
        attr_reader :lhs
        ##
        # @return [Base] parser for RHS
        attr_reader :rhs

        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

        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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sober_swag-0.25.2 lib/sober_swag/reporting/input/either.rb
sober_swag-0.25.1 lib/sober_swag/reporting/input/either.rb
sober_swag-0.25.0 lib/sober_swag/reporting/input/either.rb
sober_swag-0.24.1 lib/sober_swag/reporting/input/either.rb
sober_swag-0.24.0 lib/sober_swag/reporting/input/either.rb
sober_swag-0.23.0 lib/sober_swag/reporting/input/either.rb
sober_swag-0.22.0 lib/sober_swag/reporting/input/either.rb