Class: SoberSwag::Reporting::Output::InRange

Inherits:
Base
  • Object
show all
Defined in:
lib/sober_swag/reporting/output/in_range.rb

Overview

Specify that an output will be within a certain range. This gets translated to minimum and maximum keys in swagger.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#view, #views

Methods included from Interface

#call!, #described, #enum, #in_range, #list, #nilable, #partitioned, #referenced, #reporting?, #serialize, #via_map

Constructor Details

#initialize(output, range) ⇒ InRange

Returns a new instance of InRange.



8
9
10
11
# File 'lib/sober_swag/reporting/output/in_range.rb', line 8

def initialize(output, range)
  @output = output
  @range = range
end

Instance Attribute Details

#outputInterface (readonly)

Returns:



15
16
17
# File 'lib/sober_swag/reporting/output/in_range.rb', line 15

def output
  @output
end

#rangeRange (readonly)

Returns:

  • (Range)


19
20
21
# File 'lib/sober_swag/reporting/output/in_range.rb', line 19

def range
  @range
end

Instance Method Details

#call(value) ⇒ Object



21
22
23
# File 'lib/sober_swag/reporting/output/in_range.rb', line 21

def call(value)
  output.call(value)
end

#maximum_portionObject



48
49
50
51
52
53
54
# File 'lib/sober_swag/reporting/output/in_range.rb', line 48

def maximum_portion
  return {} unless range.end

  res = { maximum: range.end }
  res[:exclusiveMaximum] = true if range.exclude_end?
  res
end

#minimum_portionObject



56
57
58
59
60
# File 'lib/sober_swag/reporting/output/in_range.rb', line 56

def minimum_portion
  return {} unless range.begin

  { minimum: range.begin }
end

#serialize_report(value) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/sober_swag/reporting/output/in_range.rb', line 25

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

  return rep if rep.is_a?(Report::Base)

  return Report::Value.new(['was not in minimum/maximum range']) unless range.member?(rep)

  rep
end

#swagger_schemaObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sober_swag/reporting/output/in_range.rb', line 35

def swagger_schema
  schema, found = output.swagger_schema

  merged =
    if schema.key?(:$ref)
      { allOf: [schema] }
    else
      schema
    end.merge(maximum_portion).merge(minimum_portion)

  [merged, found]
end