Class: DomainPrefix::Tree

Inherits:
Hash
  • Object
show all
Defined in:
lib/domain_prefix/tree.rb

Instance Method Summary collapse

Instance Method Details

#follow(path) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/domain_prefix/tree.rb', line 26

def follow(path)
  path = path.to_s.split(SEPARATOR) unless (path.is_a?(Array))
  path = path.reverse

  index = traverse(path)

  index and index <= path.length and path[0, index].reverse
end

#insert(path) ⇒ Object

Instance Methods =====================================================



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

def insert(path)
  components = path.sub(/^!/, '').split(DomainPrefix::SEPARATOR).reverse

  leaves = components.inject([ self ]) do |trees, part|
    [ part, SimpleIDN.to_unicode(part), SimpleIDN.to_ascii(part) ].uniq.flat_map do |l|
      trees.collect do |tree|
        tree[l] ||= self.class.new
      end
    end
  end

  required = path.match(/^[\!]/) ? 0 : 1

  leaves.each do |leaf|
    leaf[:required] = required
  end
  
  self
end