Class: LogStash::Setting::Numeric

Inherits:
Coercible show all
Defined in:
lib/logstash/settings.rb

Instance Attribute Summary

Attributes inherited from LogStash::Setting

#default, #name

Instance Method Summary collapse

Methods inherited from Coercible

#set

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