Sha256: 857d73993ae9ed4e1c459e8e2614fa0c4e00b133de981d8362e8b8333b0f4b98

Contents?: true

Size: 1.04 KB

Versions: 7

Compression:

Stored size: 1.04 KB

Contents

module SoberSwag
  module Reporting
    module Output
      ##
      # Output with a particular pattern.
      class Pattern < Base
        def initialize(output, pattern)
          @output = output
          @pattern = pattern
        end

        ##
        # @return [Interface]
        attr_reader :output

        ##
        # @return [Regexp]
        attr_reader :pattern

        def call(input)
          output.call(input)
        end

        def serialize_report(value)
          base = output.serialize_report(value)

          return base if base.is_a?(Report::Error)

          if pattern.match?(base)
            base
          else
            Report::Value.new(['did not match pattern'])
          end
        end

        def swagger_schema
          schema, defs = output.swagger_schema

          merged =
            if schema.key?(:$ref)
              { oneOf: [schema] }
            else
              schema
            end.merge(pattern: pattern.to_s.gsub('?-mix:', ''))
          [merged, defs]
        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/output/pattern.rb
sober_swag-0.25.1 lib/sober_swag/reporting/output/pattern.rb
sober_swag-0.25.0 lib/sober_swag/reporting/output/pattern.rb
sober_swag-0.24.1 lib/sober_swag/reporting/output/pattern.rb
sober_swag-0.24.0 lib/sober_swag/reporting/output/pattern.rb
sober_swag-0.23.0 lib/sober_swag/reporting/output/pattern.rb
sober_swag-0.22.0 lib/sober_swag/reporting/output/pattern.rb