class Sirens::TreeChoiceModel

Public Class Methods

new(selection: nil, roots: [], get_children_block:) click to toggle source

Initializing

Calls superclass method
# 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

Public Instance Methods

children_at(path:) click to toggle source

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

# File lib/models/tree_choice_model.rb, line 78
def children_at(path:)
    @tree.children_at(path: path)
end
has_selection() click to toggle source

Asking

# File lib/models/tree_choice_model.rb, line 98
def has_selection()
    ! @selection.value.nil? && ! @selection.value.empty?
end
item_at(path:) click to toggle source

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.
# File lib/models/tree_choice_model.rb, line 71
def item_at(path:)
    @tree.item_at(path: path)
end
objects_hierarchy_at(path:) click to toggle source

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

# 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) click to toggle source

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

# File lib/models/tree_choice_model.rb, line 85
def path_of(objects_hierarchy)
    @tree.path_of(objects_hierarchy)
end
selection() click to toggle source

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.

# File lib/models/tree_choice_model.rb, line 23
def selection()
    @selection
end
set_selection(objects_hierarchy) click to toggle source

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.

# 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:) click to toggle source

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.

# 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) click to toggle source

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

# File lib/models/tree_choice_model.rb, line 61
def set_tree_roots(items)
    @tree.set_roots(items)
end
tree() click to toggle source

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

# File lib/models/tree_choice_model.rb, line 53
def tree()
    @tree
end