Class: Proxy::DHCP::Infoblox::RangeRegularExpressionGenerator::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_proxy_dhcp_infoblox/network_address_range_regex_generator.rb

Direct Known Subclasses

Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, children = []) ⇒ Node

Returns a new instance of Node.



8
9
10
11
# File 'lib/smart_proxy_dhcp_infoblox/network_address_range_regex_generator.rb', line 8

def initialize(value, children = [])
  @value = value
  @children = children
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



6
7
8
# File 'lib/smart_proxy_dhcp_infoblox/network_address_range_regex_generator.rb', line 6

def children
  @children
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/smart_proxy_dhcp_infoblox/network_address_range_regex_generator.rb', line 6

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/smart_proxy_dhcp_infoblox/network_address_range_regex_generator.rb', line 19

def <=>(other)
  return -1 if value.to_s == '0?'
  return 1 if other.value.to_s == '0?'
  return 0 if value == other.value
  return -1 if value < other.value
  1
end

#add_child(a_node) ⇒ Object



27
28
29
30
31
# File 'lib/smart_proxy_dhcp_infoblox/network_address_range_regex_generator.rb', line 27

def add_child(a_node)
  children.push(a_node)
  children.sort!
  a_node
end

#add_children(values) ⇒ Object



13
14
15
16
17
# File 'lib/smart_proxy_dhcp_infoblox/network_address_range_regex_generator.rb', line 13

def add_children(values)
  return if values.empty?
  node = (found = children.find {|n| n.value == values.first}).nil? ? add_child(Node.new(values.first)) : found
  node.add_children(values[1..-1])
end

#as_regexObject



44
45
46
# File 'lib/smart_proxy_dhcp_infoblox/network_address_range_regex_generator.rb', line 44

def as_regex
  children.empty? ? [value.to_s] : children.map {|c| c.as_regex.map {|r| value.to_s + r}}.flatten
end

#group_childrenObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/smart_proxy_dhcp_infoblox/network_address_range_regex_generator.rb', line 33

def group_children
  children.each {|n| n.group_children}
  return if children.size < 2
  @children = children[1..-1].inject([MergedNode.new(children.first)]) do |grouped, to_group|
    current = MergedNode.new(to_group)
    found = grouped.find {|g| ((g.value != ['0?'] && current.value != ['0?']) || (current.value == ['0?'] && g.value == ['0?'])) && (g.children == current.children)}
    found.nil? ? grouped.push(current) : found.merge(current)
    grouped
  end
end