Class: LogStash::Setting::PortRange

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

Constant Summary collapse

PORT_SEPARATOR =
"-"

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) ⇒ PortRange

Returns a new instance of PortRange.



332
333
334
# File 'lib/logstash/settings.rb', line 332

def initialize(name, default=nil, strict=true)
  super(name, ::Range, default, strict=true) { |value| valid?(value) }
end

Instance Method Details

#coerce(value) ⇒ Object



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

def coerce(value)
  case value
  when ::Range
    value
  when ::Fixnum
    value..value
  when ::String
    first, last = value.split(PORT_SEPARATOR)
    last = first if last.nil?
    begin
      (Integer(first))..(Integer(last))
    rescue ArgumentError # Trap and reraise a more human error
      raise ArgumentError.new("Could not coerce #{value} into a port range")
    end
  else
    raise ArgumentError.new("Could not coerce #{value} into a port range")
  end
end

#valid?(range) ⇒ Boolean

Returns:



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

def valid?(range)
  Port::VALID_PORT_RANGE.first <= range.first && Port::VALID_PORT_RANGE.last >= range.last
end

#validate(value) ⇒ Object



359
360
361
362
363
# File 'lib/logstash/settings.rb', line 359

def validate(value)
  unless valid?(value)
    raise ArgumentError.new("Invalid value \"#{value}, valid options are within the range of #{Port::VALID_PORT_RANGE.first}-#{Port::VALID_PORT_RANGE.last}")
  end
end