Module: Bewildr::ControlTypeAdditions::TreeAdditions

Defined in:
lib/bewildr/control_type_additions/tree_additions.rb

Instance Method Summary collapse

Instance Method Details

#contains_node?(path) ⇒ Boolean

Returns true if the tree contains the item described by the input, false if it doesn’t

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
# File 'lib/bewildr/control_type_additions/tree_additions.rb', line 51

def contains_node?(path)
  begin
    node(path)
    return true
  rescue ElementDoesntExist => e
    return false
  end
end

#node(path) ⇒ Object

Returns the tree node described by the input, eg:

node(["parent node", "child node"])


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bewildr/control_type_additions/tree_additions.rb', line 25

def node(path)
  current_nodes = root_nodes
  matching_node = nil
  path.each_with_index do |target_node, index|
    case current_nodes
    when Array
      matching_node = current_nodes.find {|node| node.name == target_node} #TODO: make this work with regexes as well as strings...
      raise Bewildr::ElementDoesntExist if matching_node.nil?
    when Bewildr::Element
      if current_nodes.name == target_node #TODO: make this work with regexes as well as strings...
        matching_node = current_nodes
      else
        raise Bewildr::ElementDoesntExist
      end
    end
    raise Bewildr::ElementDoesntExist if matching_node.nil?
    if path.size != index + 1
      matching_node.expand
      load_all_items_hack
      current_nodes = matching_node.child_nodes
    end
  end
  return matching_node
end

#root_nodeObject

Returns the root node



13
14
15
# File 'lib/bewildr/control_type_additions/tree_additions.rb', line 13

def root_node
  root_nodes.first
end

#root_nodesObject

Returns an array containing the tree’s root nodes



7
8
9
10
# File 'lib/bewildr/control_type_additions/tree_additions.rb', line 7

def root_nodes
  prepare_element
  get(:type => :tree_item, :scope => :children)
end

#select_node(path) ⇒ Object

Selects the tree node described by the input, eg:

select_node(["parent node", "child node"])


19
20
21
# File 'lib/bewildr/control_type_additions/tree_additions.rb', line 19

def select_node(path)
  node(path).select
end