Class: SoberSwag::Reporting::Input::Dictionary

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

Overview

Dictionary types: string keys, something else as a value.

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(value_input) ⇒ Dictionary

Returns a new instance of Dictionary.



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

def initialize(value_input)
  @value_input = value_input
end

Instance Attribute Details

#value_inputObject (readonly)

Returns the value of attribute value_input.



15
16
17
# File 'lib/sober_swag/reporting/input/dictionary.rb', line 15

def value_input
  @value_input
end

Class Method Details

.of(input_type) ⇒ Object



7
8
9
# File 'lib/sober_swag/reporting/input/dictionary.rb', line 7

def self.of(input_type)
  new(input_type)
end

Instance Method Details

#call(value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sober_swag/reporting/input/dictionary.rb', line 17

def call(value)
  return Report::Base.new(['was not an object']) unless value.is_a?(Hash)

  bad, good = value.map { |k, v|
    [k, value_input.call(v)]
  }.compact.partition { |(_, v)| v.is_a?(Report::Base) }

  return Report::Object.new(bad.to_h) if bad.any?

  good.to_h
end

#swagger_schemaObject



29
30
31
32
33
# File 'lib/sober_swag/reporting/input/dictionary.rb', line 29

def swagger_schema
  schema, found = value_input.swagger_schema

  [{ type: :object, additionalProperties: schema }, found]
end