class SoberSwag::Reporting::Input::List

Class to parse an array, where each element has the same type.

Called List to avoid name conflicts.

Attributes

element[R]

@return [Base] the parser for elements

Public Class Methods

new(element) click to toggle source

@param element [Base] the parser for elements

# File lib/sober_swag/reporting/input/list.rb, line 11
def initialize(element)
  @element = element
end

Public Instance Methods

call(value) click to toggle source
# File lib/sober_swag/reporting/input/list.rb, line 19
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
swagger_schema() click to toggle source
# File lib/sober_swag/reporting/input/list.rb, line 36
def swagger_schema
  schema, found = element.swagger_schema

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