Class: SoberSwag::Reporting::Input::InRange
- Inherits:
-
Base
- Object
- Base
- SoberSwag::Reporting::Input::InRange
show all
- Defined in:
- lib/sober_swag/reporting/input/in_range.rb
Overview
Specify that an item must be within a given range in ruby.
This gets translated to minimum
and maximum
keys in swagger.
This works with endless ranges (Ruby 2.6+) and beginless ranges (Ruby 2.7+)
Instance Attribute 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(input, range) ⇒ InRange
Returns a new instance of InRange.
10
11
12
13
|
# File 'lib/sober_swag/reporting/input/in_range.rb', line 10
def initialize(input, range)
@input = input
@range = range
end
|
Instance Attribute Details
17
18
19
|
# File 'lib/sober_swag/reporting/input/in_range.rb', line 17
def input
@input
end
|
#range ⇒ Range
21
22
23
|
# File 'lib/sober_swag/reporting/input/in_range.rb', line 21
def range
@range
end
|
Instance Method Details
#call(value) ⇒ Range
25
26
27
28
29
30
31
32
|
# File 'lib/sober_swag/reporting/input/in_range.rb', line 25
def call(value)
res = input.call(value)
return res if res.is_a?(Report::Base)
return Report::Value.new(['was not in minimum/maximum range']) unless range.member?(res)
res
end
|
#maximum_portion ⇒ Object
47
48
49
50
51
|
# File 'lib/sober_swag/reporting/input/in_range.rb', line 47
def maximum_portion
return {} unless range.end
{ maximum: range.end, exclusiveMaximum: range.exclude_end? }
end
|
#minimum_portion ⇒ Object
53
54
55
56
57
|
# File 'lib/sober_swag/reporting/input/in_range.rb', line 53
def minimum_portion
return {} unless range.begin
{ minimum: range.begin, exclusiveMinimum: false }
end
|
#swagger_schema ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/sober_swag/reporting/input/in_range.rb', line 34
def swagger_schema
schema, found = input.swagger_schema
merged =
if schema.key?(:$ref)
{ allOf: [schema] }
else
schema
end.merge(maximum_portion).merge(minimum_portion)
[merged, found]
end
|