Class: Sirens::TreeChoiceModel

Inherits:
Object
  • Object
show all
Defined in:
lib/models/tree_choice_model.rb

Instance Method Summary collapse

Constructor Details

#initialize(selection: nil, roots: [], get_children_block:) ⇒ TreeChoiceModel

Initializing



6
7
8
9
10
11
12
13
14
# File 'lib/models/tree_choice_model.rb', line 6

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

    @selection = ValueModel.on(selection)
    @tree = VirtualTreeModel.on(
        roots: roots,
        get_children_block: get_children_block
    )
end

Instance Method Details

#children_at(path:) ⇒ Object

Returns an array with the children of the item in the tree at the given path.



78
79
80
# File 'lib/models/tree_choice_model.rb', line 78

def children_at(path:)
    @tree.children_at(path: path)
end

#has_selectionObject

Asking



98
99
100
# File 'lib/models/tree_choice_model.rb', line 98

def has_selection()
    ! @selection.value.nil? && ! @selection.value.empty?
end

#item_at(path:) ⇒ Object

Returns the item in the tree at the given path. The path is an array of Integers, each Integer being the index in each level of the tree. Example:

[1, 0, 3] returns the item taken from the second root, its first child and its fourth child.


71
72
73
# File 'lib/models/tree_choice_model.rb', line 71

def item_at(path:)
    @tree.item_at(path: path)
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.



92
93
94
# File 'lib/models/tree_choice_model.rb', line 92

def objects_hierarchy_at(path:)
    @tree.objects_hierarchy_at(path: path)
end

#path_of(objects_hierarchy) ⇒ Object

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



85
86
87
# File 'lib/models/tree_choice_model.rb', line 85

def path_of(objects_hierarchy)
    @tree.path_of(objects_hierarchy)
end

#selectionObject

Returns the selection model, not the selected item. This model can be observed for changes. This model value is an array with the objects path from the tree root object to the selected item.



23
24
25
# File 'lib/models/tree_choice_model.rb', line 23

def selection()
    @selection
end

#set_selection(objects_hierarchy) ⇒ Object

Sets the item currently selected. The objects_hierarchy is an array with the objects path from the tree root object to the selected item. The selection model triggers an announcement that its value changed.



33
34
35
# File 'lib/models/tree_choice_model.rb', line 33

def set_selection(objects_hierarchy)
    @selection.set_value(objects_hierarchy)
end

#set_selection_from_path(path:) ⇒ Object

Sets the item currently selected. The objects_hierarchy is an array with the objects path from the tree root object to the selected item. The selection model triggers an announcement that its value changed.



43
44
45
46
47
# File 'lib/models/tree_choice_model.rb', line 43

def set_selection_from_path(path:)
    objects_hierarchies = objects_hierarchy_at(path: path)

    set_selection(objects_hierarchies)
end

#set_tree_roots(items) ⇒ Object

Sets the tree model roots. Announces that the tree changed.



61
62
63
# File 'lib/models/tree_choice_model.rb', line 61

def set_tree_roots(items)
    @tree.set_roots(items)
end

#treeObject

Returns the tree model. The tree model holds the tree items and announces changes on it.



53
54
55
# File 'lib/models/tree_choice_model.rb', line 53

def tree()
    @tree
end