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

Constructor Details

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

Returns a new instance of PortRange.



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

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

Instance Method Details

#coerce(value) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/logstash/settings.rb', line 367

def coerce(value)
  case value
  when ::Range
    value
  when ::Integer
    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:



363
364
365
# File 'lib/logstash/settings.rb', line 363

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

#validate(value) ⇒ Object



386
387
388
389
390
# File 'lib/logstash/settings.rb', line 386

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