Module: OrderedTree::InstanceMethods::List
- Included in:
- OrderedTree::InstanceMethods
- Defined in:
- lib/ordered_tree/instance_methods/list.rb
Instance Method Summary collapse
-
#move_above(sibling = nil) ⇒ Object
moves the item above sibling in the list defaults to the top of the list.
-
#move_higher ⇒ Object
swap with the node above self.
-
#move_lower ⇒ Object
swap with the node below self.
-
#move_to_bottom ⇒ Object
move to the bottom of the list.
-
#move_to_top ⇒ Object
move to the top of the list.
-
#position_in_list ⇒ Object
returns object’s position in the list the list will either be parent.children, or self.class.roots.
-
#self_and_siblings(reload = false) ⇒ Object
returns an array of the object’s siblings, including itself.
-
#siblings(reload = false) ⇒ Object
returns an array of the object’s siblings, excluding itself.
Instance Method Details
#move_above(sibling = nil) ⇒ Object
moves the item above sibling in the list
defaults to the top of the list
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ordered_tree/instance_methods/list.rb', line 35 def move_above(sibling = nil) if sibling return if (!self_and_siblings(true).include?(sibling) || (sibling == self)) if sibling.position_in_list > position_in_list move_to(sibling.position_in_list - 1) else move_to(sibling.position_in_list) end else move_to_top end end |
#move_higher ⇒ Object
swap with the node above self
55 56 57 58 |
# File 'lib/ordered_tree/instance_methods/list.rb', line 55 def move_higher return if position_in_list == 1 move_to(position_in_list - 1) end |
#move_lower ⇒ Object
swap with the node below self
61 62 63 64 |
# File 'lib/ordered_tree/instance_methods/list.rb', line 61 def move_lower return if self == self_and_siblings(true).last move_to(position_in_list + 1) end |
#move_to_bottom ⇒ Object
move to the bottom of the list
67 68 69 70 |
# File 'lib/ordered_tree/instance_methods/list.rb', line 67 def move_to_bottom return if self == self_and_siblings(true).last move_to(self_and_siblings.last.position_in_list) end |
#move_to_top ⇒ Object
move to the top of the list
49 50 51 52 |
# File 'lib/ordered_tree/instance_methods/list.rb', line 49 def move_to_top return if position_in_list == 1 move_to(1) end |
#position_in_list ⇒ Object
returns object’s position in the list
the list will either be parent.children,
or self.class.roots
i.e. self.position
27 28 29 |
# File 'lib/ordered_tree/instance_methods/list.rb', line 27 def position_in_list self[order_column] end |
#self_and_siblings(reload = false) ⇒ Object
returns an array of the object’s siblings, including itself
return is cached
use self_and_siblings(true) to force a reload
10 11 12 |
# File 'lib/ordered_tree/instance_methods/list.rb', line 10 def self_and_siblings(reload = false) parent(reload) ? parent.children(reload) : self.class.roots(scope_condition) end |
#siblings(reload = false) ⇒ Object
returns an array of the object’s siblings, excluding itself
return is cached
use siblings(true) to force a reload
18 19 20 |
# File 'lib/ordered_tree/instance_methods/list.rb', line 18 def siblings(reload = false) self_and_siblings(reload) - [self] end |