Class: SoberSwag::Reporting::Input::List

Inherits:
Base
  • Object
show all
Defined in:
lib/sober_swag/reporting/input/list.rb

Overview

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

Called List to avoid name conflicts.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Interface

#add_schema_key, #call!, #described, #enum, #format, #in_range, #list, #mapped, #modify_schema, #multiple_of, #optional, #or, #referenced, #swagger_path_schema, #swagger_query_schema, #|

Constructor Details

#initialize(element) ⇒ List

Returns a new instance of List.

Parameters:

  • element (Base)

    the parser for elements



17
18
19
# File 'lib/sober_swag/reporting/input/list.rb', line 17

def initialize(element)
  @element = element
end

Instance Attribute Details

#elementBase (readonly)

Returns the parser for elements.

Returns:

  • (Base)

    the parser for elements



23
24
25
# File 'lib/sober_swag/reporting/input/list.rb', line 23

def element
  @element
end

Class Method Details

.of(element) ⇒ Object

See Also:

  • #new


11
12
13
# File 'lib/sober_swag/reporting/input/list.rb', line 11

def self.of(element)
  initialize(element)
end

Instance Method Details

#call(value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sober_swag/reporting/input/list.rb', line 25

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_schemaObject



42
43
44
45
46
# File 'lib/sober_swag/reporting/input/list.rb', line 42

def swagger_schema
  schema, found = element.swagger_schema

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