Class: Sirens::VirtualTreeModel

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/models/virtual_tree_model.rb

Defined Under Namespace

Classes: TreeChanged, TreeNode

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(roots: [], get_children_block:) ⇒ VirtualTreeModel

Initializing



23
24
25
26
27
28
29
# File 'lib/models/virtual_tree_model.rb', line 23

def initialize(roots: [], get_children_block:)
    super()

    @get_children_block = get_children_block

    @roots = roots.collect{ |item| new_tree_node_on(item) }
end

Class Method Details

.on(root_items) ⇒ Object



16
17
18
# File 'lib/models/virtual_tree_model.rb', line 16

def on(root_items)
    self.new(root_items)
end

.with(root_item) ⇒ Object



8
9
10
# File 'lib/models/virtual_tree_model.rb', line 8

def with(root_item)
    self.on([root_item])
end

.with_all(root_items) ⇒ Object



12
13
14
# File 'lib/models/virtual_tree_model.rb', line 12

def with_all(root_items)
    self.on(root_items.clone)
end

Instance Method Details

#children_at(path:) ⇒ Object



67
68
69
# File 'lib/models/virtual_tree_model.rb', line 67

def children_at(path:)
    node_at(path: path).children.collect{ |node| node.value }
end

#item_at(path:) ⇒ Object



63
64
65
# File 'lib/models/virtual_tree_model.rb', line 63

def item_at(path:)
    node_at(path: path).value
end

#new_tree_node_on(item) ⇒ Object



31
32
33
# File 'lib/models/virtual_tree_model.rb', line 31

def new_tree_node_on(item)
    TreeNode.new(value: item, get_children_block: @get_children_block)
end

#node_at(path:) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/models/virtual_tree_model.rb', line 53

def node_at(path:)
    node = @roots[path.first]

    path[1..-1].each do |child_index|
        node = node.child_at(index: child_index)
    end

    node
end

#objects_hierarchy_at(path:) ⇒ Object

Given a path returns an array with the objects on each tree level corresponding to each index in the path.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/models/virtual_tree_model.rb', line 91

def objects_hierarchy_at(path:)
    return [] if path.nil?

    nodes = @roots

    path.inject([]) { |hierarchy, index|
        node = nodes[index]

        hierarchy << node.value

        nodes = node.children

        hierarchy
    }
end

#path_of(objects_hierarchy) ⇒ Object

Given a hierarchy of objects in the tree, returns an array with the path indices.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/models/virtual_tree_model.rb', line 74

def path_of(objects_hierarchy)
    objects = @roots

    objects_hierarchy.inject([]) { |path, each_object|
        index = objects.index { |node| node.value == each_object }

        path << index

        objects = objects[index].children

        path
    }
end

#rootsObject

Accessing



37
38
39
# File 'lib/models/virtual_tree_model.rb', line 37

def roots()
    @roots.collect{ |node| node.value }
end

#set_roots(new_roots) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/models/virtual_tree_model.rb', line 41

def set_roots(new_roots)
    old_roots = roots

    @roots = new_roots.collect{ |item| new_tree_node_on(item) }

    changed

    notify_observers(
        TreeChanged.new(new_roots: new_roots, old_roots: old_roots)
    )
end