Class: IPTables::Primitives

Inherits:
Object
  • Object
show all
Defined in:
lib/iptables/primitives.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(primitives_hash) ⇒ Primitives

Returns a new instance of Primitives.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/iptables/primitives.rb', line 6

def initialize(primitives_hash)
  @children = {}
  raise "expected Hash" unless primitives_hash.is_a? Hash
  primitives_hash.each{ |name, info|
    child = nil
    case info
    when Array, String
      child = info
    when Hash
      child = Primitives.new(info)
    else
      raise "unknown primitive type: #{name}"
    end

    self.instance_variable_set "@#{name}", child
    self.class.class_eval do
      define_method(name) { child }
    end
    @children[name] = child
  }
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/iptables/primitives.rb', line 5

def children
  @children
end

Instance Method Details

#has_primitive?(identifier) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/iptables/primitives.rb', line 42

def has_primitive?(identifier)
  begin
    self.substitute(identifier)
    return true
  rescue
    return false
  end
end

#substitute(identifier) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/iptables/primitives.rb', line 28

def substitute(identifier)
  components = identifier.split(/\./)
  the_first = components.first
  the_rest = components[1 .. -1].join('.')
  raise "failed to substitute unknown primitive: #{the_first}" unless @children.has_key? the_first
  case @children[the_first]
  when Primitives
    raise "failed to substitute partial primitive: #{the_first}" if the_rest.empty?
    return @children[the_first].substitute(the_rest)
  else
    return @children[the_first]
  end
end