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.



337
338
339
# File 'lib/logstash/settings.rb', line 337

def initialize(name, default=nil, strict=true)
  super(name, ::Numeric, default, strict)
end

Instance Method Details

#coerce(v) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/logstash/settings.rb', line 341

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