Class: LDAP::Server::Trie

Inherits:
Object
  • Object
show all
Defined in:
lib/ldap/server/trie.rb

Defined Under Namespace

Classes: NodeNotFoundError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, value = nil) {|_self| ... } ⇒ Trie

Create a new Trie. Use with a block

Yields:

  • (_self)

Yield Parameters:



15
16
17
18
19
20
21
# File 'lib/ldap/server/trie.rb', line 15

def initialize(parent = nil, value = nil)
  @parent = parent
  @value = value
  @children = Hash.new

  yield self if block_given?
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



12
13
14
# File 'lib/ldap/server/trie.rb', line 12

def children
  @children
end

#parentObject

Returns the value of attribute parent.



12
13
14
# File 'lib/ldap/server/trie.rb', line 12

def parent
  @parent
end

#valueObject

Returns the value of attribute value.



12
13
14
# File 'lib/ldap/server/trie.rb', line 12

def value
  @value
end

Instance Method Details

#<<(dn) ⇒ Object

Insert a path (empty node)



24
25
26
# File 'lib/ldap/server/trie.rb', line 24

def <<(dn)
  insert(dn)
end

#insert(dn, value = nil) ⇒ Object

Insert a node with a value



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ldap/server/trie.rb', line 29

def insert(dn, value = nil)
  dn = LDAP::Server::DN.new(dn || '') if not dn.is_a? LDAP::Server::DN
  dn.reverse_each do |component|
    @children[component] = Trie.new(self) if @children[component].nil?
    dn.dname.pop
    if dn.any?
      @children[component].insert dn, value
    else
      @children[component].value = value
    end
  end
end

#lookup(dn) ⇒ Object

Looks up a node and returns its value or raises LDAP::Server::Trie::NodeNotFoundError if it’s not in the tree

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ldap/server/trie.rb', line 44

def lookup(dn)
  dn = LDAP::Server::DN.new(dn || '') if not dn.is_a? LDAP::Server::DN
  return @value if dn.dname.empty?
  component = dn.dname.pop
  @children.each do |key, value|
    if key.keys.first == component.keys.first
      if key.values.first.start_with?(':') or key.values.first == component.values.first
        return value.lookup dn
      end
    end
  end
  raise NodeNotFoundError
end

#match(dn, path = '') ⇒ Object

Looks up a node and returns its value or the (non-nil) value of the nearest ancestor.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ldap/server/trie.rb', line 60

def match(dn, path = '')
  dn = LDAP::Server::DN.new(dn || '') if not dn.is_a? LDAP::Server::DN
  return path, @value if dn.dname.empty?
  component = dn.dname.pop
  @children.each do |key, value|
    if key.keys.first == component.keys.first
      if key.values.first.start_with?(':') or key.values.first == component.values.first
        path.prepend ',' unless path.empty?
        path.prepend "#{LDAP::Server::DN.join key}"
        new_path, new_value = value.match dn, path
        if new_value
          return new_path, new_value
        else
          return (@value ? path : nil), @value
        end
      end
    end
  end
  return path, @value
end


81
82
83
84
85
86
87
88
89
# File 'lib/ldap/server/trie.rb', line 81

def print_tree(prefix = '')
  if @value
    p "#{prefix}{{#{@value}}}"
  end
  @children.each do |key, value|
      p "#{prefix}#{key.keys.first} => #{key.values.first}"
    @children[key].print_tree("#{prefix}  ")
  end
end