Module: ChronicTree::Travesal

Included in:
ChronicTree
Defined in:
lib/chronic_tree/travesal.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject



38
39
40
41
42
43
# File 'lib/chronic_tree/travesal.rb', line 38

def ancestors
  init_tree_args_when_nil
  ancestors_relation(current_time_at, current_scope_name).map do |el|
    el.parent.as_tree(current_time_at, current_scope_name)
  end
end

#childrenObject



3
4
5
6
7
8
# File 'lib/chronic_tree/travesal.rb', line 3

def children
  init_tree_args_when_nil
  children_relation(current_time_at, current_scope_name).map do |el|
    el.child.as_tree(current_time_at, current_scope_name)
  end
end

#descendantsObject



22
23
24
25
26
27
28
29
# File 'lib/chronic_tree/travesal.rb', line 22

def descendants
  init_tree_args_when_nil
  descendants_relation(current_time_at, current_scope_name).inject([]) do |result, el|
    result[el.distance - 1] ||= []
    result[el.distance - 1] << el.child.as_tree(current_time_at, current_scope_name)
    result
  end
end

#existed_in_tree?(time_at = Time.now, scope_name = 'default') ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/chronic_tree/travesal.rb', line 54

def existed_in_tree?(time_at = Time.now, scope_name = 'default')
  existed_relation(time_at, scope_name).any?
end

#flat_descendantsObject



31
32
33
34
35
36
# File 'lib/chronic_tree/travesal.rb', line 31

def flat_descendants
  init_tree_args_when_nil
  descendants_relation(current_time_at, current_scope_name).map do |el|
    el.child.as_tree(current_time_at, current_scope_name)
  end
end

#parentObject Also known as: parent?



10
11
12
13
14
# File 'lib/chronic_tree/travesal.rb', line 10

def parent
  init_tree_args_when_nil
  child_element = parent_relation(current_time_at, current_scope_name).first
  child_element.parent.as_tree(current_time_at, current_scope_name) if child_element
end

#rootObject



16
17
18
19
20
# File 'lib/chronic_tree/travesal.rb', line 16

def root
  init_tree_args_when_nil
  child_element = existed_relation(current_time_at, current_scope_name).first
  child_element.root.as_tree(current_time_at, current_scope_name) if child_element
end

#tree_empty?(time_at = Time.now, scope_name = 'default') ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/chronic_tree/travesal.rb', line 47

def tree_empty?(time_at = Time.now, scope_name = 'default')
  ChronicTree::ActiveRecord::Element.
    where(scope_name: scope_name).
    at(time_at).
    empty?
end