Class: Lib::DHCP::Options

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/lib/dhcp/options.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Options

Returns a new instance of Options.



18
19
20
21
# File 'lib/lib/dhcp/options.rb', line 18

def initialize(*options)
  @options = []
  options.each { |option| self.add option }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/lib/dhcp/options.rb', line 12

def options
  @options
end

Class Method Details

.unpack(packet) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lib/dhcp/options.rb', line 65

def self.unpack(packet)
  size = packet.size
  offset = 0
  options = self.new
  while offset < size
    payload = packet.unpack("@#{offset}a*").first
    option = Lib::DHCP::Option.unpack(payload)
    options << option
    break if option.oid == Lib::DHCP::Option::END_OPTION
    offset += (option.len.to_i + 2)
  end
  options
end

Instance Method Details

#[]=(index, option) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/lib/dhcp/options.rb', line 39

def []=(index, option)
  option = create_option(option)
  if include?(option) and @options[index].oid != option.oid
    @options[index] = option if option.oid == 0
  else
    @options[index] = create_option(option)
  end

end

#add(option) ⇒ Object Also known as: <<



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lib/dhcp/options.rb', line 27

def add(option)
  option = begin
             JSON.parse(option)
           rescue JSON::ParserError
             option
           rescue TypeError
             option
           end
  option = create_option(option)
  @options << option unless self.include? option
end

#del(oid) ⇒ Object



59
60
61
# File 'lib/lib/dhcp/options.rb', line 59

def del(oid)
  @options.delete_if{|option| option.oid.to_i == oid.to_i}
end

#include?(option) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/lib/dhcp/options.rb', line 49

def include?(option)
  return ! select(option.oid).empty? if option.is_a?(Option)
  return ! select(option).empty? if option.is_a?(Integer)
  false
end

#packObject



23
24
25
# File 'lib/lib/dhcp/options.rb', line 23

def pack
  self.map{|option| option.pack}.join('')
end

#select(oid) ⇒ Object



55
56
57
# File 'lib/lib/dhcp/options.rb', line 55

def select(oid)
  @options.select {|option| option.oid.to_i == oid.to_i}
end