Class: Attributor::Integer
- Inherits:
-
Object
- Object
- Attributor::Integer
- Includes:
- Type
- Defined in:
- lib/attributor/types/integer.rb
Constant Summary collapse
- EXAMPLE_RANGE =
1000.freeze
Class Method Summary collapse
- .example(context = nil, options: {}) ⇒ Object
- .load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object
- .native_type ⇒ Object
- .validate_options(options) ⇒ Object
Methods included from Type
Class Method Details
.example(context = nil, options: {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/attributor/types/integer.rb', line 15 def self.example(context=nil, options: {}) () # Set default values if [:min].nil? && [:max].nil? min = 0 max = EXAMPLE_RANGE elsif [:min].nil? max = [:max] min = max - EXAMPLE_RANGE elsif [:max].nil? min = [:min] max = min + EXAMPLE_RANGE else min = [:min] max = [:max] end # Generate random number on interval [min,max] rand(max-min+1) + min end |
.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object
37 38 39 40 41 |
# File 'lib/attributor/types/integer.rb', line 37 def self.load(value, context=Attributor::DEFAULT_ROOT_CONTEXT, **) Integer(value) rescue TypeError super end |
.native_type ⇒ Object
10 11 12 |
# File 'lib/attributor/types/integer.rb', line 10 def self.native_type return ::Integer end |
.validate_options(options) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/attributor/types/integer.rb', line 43 def self.() if .has_key?(:min) && .has_key?(:max) # Both :max and :min must be integers raise AttributorException.new("Invalid range: [#{[:min].inspect}, #{[:max].inspect}]") if ![:min].is_a?(::Integer) || ![:max].is_a?(::Integer) # :max cannot be less than :min raise AttributorException.new("Invalid range: [#{[:min].inspect}, #{[:max].inspect}]") if [:max] < [:min] elsif !.has_key?(:min) && .has_key?(:max) # :max must be an integer raise AttributorException.new("Invalid range: [, #{[:max].inspect}]") if ![:max].is_a?(::Integer) elsif .has_key?(:min) && !.has_key?(:max) # :min must be an integer raise AttributorException.new("Invalid range: [#{[:min].inspect},]") if ![:min].is_a?(::Integer) else # Neither :min nor :max were given, noop end true end |