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.



401
402
403
# File 'lib/logstash/settings.rb', line 401

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

Instance Method Details

#coerce(value) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/logstash/settings.rb', line 409

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:



405
406
407
# File 'lib/logstash/settings.rb', line 405

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

#validate(value) ⇒ Object



428
429
430
431
432
# File 'lib/logstash/settings.rb', line 428

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