Class: Attributor::Integer
- Inherits:
-
Object
- Object
- Attributor::Integer
show all
- Includes:
- Numeric
- Defined in:
- lib/attributor/types/integer.rb
Constant Summary
collapse
- EXAMPLE_RANGE =
1000
Class Method Summary
collapse
Methods included from Type
get_memoized_collection_class, set_memoized_collection_class
Class Method Details
.example(_context = nil, options: {}) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/attributor/types/integer.rb', line 13
def self.example(_context = nil, options: {})
validate_options(options)
if options[:min].nil? && options[:max].nil?
min = 0
max = EXAMPLE_RANGE
elsif options[:min].nil?
max = options[:max]
min = max - EXAMPLE_RANGE
elsif options[:max].nil?
min = options[:min]
max = min + EXAMPLE_RANGE
else
min = options[:min]
max = options[:max]
end
rand(max - min + 1) + min
end
|
.json_schema_type ⇒ Object
59
60
61
|
# File 'lib/attributor/types/integer.rb', line 59
def self.json_schema_type
:integer
end
|
.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object
.native_type ⇒ Object
9
10
11
|
# File 'lib/attributor/types/integer.rb', line 9
def self.native_type
::Integer
end
|
.validate_options(options) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/attributor/types/integer.rb', line 41
def self.validate_options(options)
if options.key?(:min) && options.key?(:max)
raise AttributorException, "Invalid range: [#{options[:min].inspect}, #{options[:max].inspect}]" if !options[:min].is_a?(::Integer) || !options[:max].is_a?(::Integer)
raise AttributorException, "Invalid range: [#{options[:min].inspect}, #{options[:max].inspect}]" if options[:max] < options[:min]
elsif !options.key?(:min) && options.key?(:max)
raise AttributorException, "Invalid range: [, #{options[:max].inspect}]" unless options[:max].is_a?(::Integer)
elsif options.key?(:min) && !options.key?(:max)
raise AttributorException, "Invalid range: [#{options[:min].inspect},]" unless options[:min].is_a?(::Integer)
end
true
end
|