Sha256: ba73415d0c8b0ca1da702d8b869cf7dc5013a42bbc8c71d2b588972df2f5a036

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module SoberSwag
  module Reporting
    module Input
      ##
      # Class to parse an array, where each element has the same type.
      #
      # Called List to avoid name conflicts.
      class List < Base
        ##
        # @see #new
        def self.of(element)
          initialize(element)
        end

        ##
        # @param element [Base] the parser for elements
        def initialize(element)
          @element = element
        end

        ##
        # @return [Base] the parser for elements
        attr_reader :element

        def call(value)
          return Report::Value.new(['was not an array']) unless value.is_a?(Array)

          # obtain a hash of indexes => errors
          errs = {}
          # yes, side effects in a map are evil, but we avoid traversal twice
          mapped = value.map.with_index do |item, idx|
            element.call(item).tap { |e| errs[idx] = e if e.is_a?(Report::Base) }
          end

          if errs.any?
            Report::List.new(errs)
          else
            mapped
          end
        end

        def swagger_schema
          schema, found = element.swagger_schema

          [{ type: 'array', items: schema }, found]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sober_swag-0.25.2 lib/sober_swag/reporting/input/list.rb