Class: LogStash::Setting::Numeric
- Inherits:
-
Coercible
- Object
- LogStash::Setting
- Coercible
- LogStash::Setting::Numeric
- Defined in:
- lib/logstash/settings.rb
Instance Attribute Summary
Attributes inherited from LogStash::Setting
Instance Method Summary collapse
- #coerce(v) ⇒ Object
-
#initialize(name, default = nil, strict = true) ⇒ Numeric
constructor
A new instance of Numeric.
Methods inherited from Coercible
Methods inherited from LogStash::Setting
#==, #reset, #set, #set?, #strict?, #to_hash, #validate_value, #value
Constructor Details
#initialize(name, default = nil, strict = true) ⇒ Numeric
Returns a new instance of Numeric.
295 296 297 |
# File 'lib/logstash/settings.rb', line 295 def initialize(name, default=nil, strict=true) super(name, ::Numeric, default, strict) end |
Instance Method Details
#coerce(v) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/logstash/settings.rb', line 299 def coerce(v) return v if v.is_a?(::Numeric) # I hate these "exceptions as control flow" idioms # but Ruby's `"a".to_i => 0` makes it hard to do anything else. coerced_value = (Integer(v) rescue nil) || (Float(v) rescue nil) if coerced_value.nil? raise ArgumentError.new("Failed to coerce value to Numeric. Received #{v} (#{v.class})") else coerced_value end end |