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

Methods included from Util::Loggable

included, #logger, #slow_logger

Constructor Details

#initialize(name, default = nil, strict = true) ⇒ Numeric

Returns a new instance of Numeric.



268
269
270
# File 'lib/logstash/settings.rb', line 268

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

Instance Method Details

#coerce(v) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/logstash/settings.rb', line 272

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