Module: ActiveLdap::Acts::Tree

Defined in:
lib/active_ldap/acts/tree.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/active_ldap/acts/tree.rb', line 4

def self.included(base)
  base.class_eval do
    extend(ClassMethods)
    association_accessor(:children) do |target|
      Association::Children.new(target, {})
    end
  end
end

Instance Method Details

#ancestorsObject

Returns list of ancestors, starting from parent until root.

subchild1.ancestors # => [child1, root]


22
23
24
25
26
# File 'lib/active_ldap/acts/tree.rb', line 22

def ancestors
  node, nodes = self, []
  nodes << node = node.parent while node.parent
  nodes
end

#parentObject



49
50
51
52
53
54
55
# File 'lib/active_ldap/acts/tree.rb', line 49

def parent
  if base == self.class.base
    nil
  else
    find(:first, :base => base, :scope => :base)
  end
end

#parent=(entry) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_ldap/acts/tree.rb', line 57

def parent=(entry)
  if entry.is_a?(String) or entry.is_a?(DN)
    base = entry
  elsif entry.respond_to?(:dn)
    base = entry.dn
    if entry.respond_to?(:clear_association_cache)
      entry.clear_association_cache
    end
  else
    message = _("parent must be an entry or parent DN: %s") % entry.inspect
    raise ArgumentError, message
  end
	destroy unless new_entry?
  self.dn = "#{dn_attribute}=#{id},#{base}"
  save
end

#rootObject

Returns the root node of the tree.



29
30
31
32
33
# File 'lib/active_ldap/acts/tree.rb', line 29

def root
  node = self
  node = node.parent while node.parent
  node
end

#self_and_siblingsObject

Returns all siblings and a reference to the current node.

subchild1.self_and_siblings # => [subchild1, subchild2]


45
46
47
# File 'lib/active_ldap/acts/tree.rb', line 45

def self_and_siblings
  parent ? parent.children : [self]
end

#siblingsObject

Returns all siblings of the current node.

subchild1.siblings # => [subchild2]


38
39
40
# File 'lib/active_ldap/acts/tree.rb', line 38

def siblings
  self_and_siblings - [self]
end