Module: Hyalite::InstanceHandles

Defined in:
lib/hyalite/instance_handles.rb

Constant Summary collapse

SEPARATOR =
'.'

Class Method Summary collapse

Class Method Details

.create_root_idObject



12
13
14
# File 'lib/hyalite/instance_handles.rb', line 12

def create_root_id
  root_id_string(root_index);
end

.is_ancestor_id_of(ancestor_id, descendant_id) ⇒ Object



61
62
63
# File 'lib/hyalite/instance_handles.rb', line 61

def is_ancestor_id_of(ancestor_id, descendant_id)
  descendant_id.index(ancestor_id) == 0 && is_boundary(descendant_id, ancestor_id.length)
end

.is_boundary(id, index) ⇒ Object



65
66
67
# File 'lib/hyalite/instance_handles.rb', line 65

def is_boundary(id, index)
  id[index] == SEPARATOR || index == id.length
end

.next_descendant_id(ancestor_id, destination_id) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/hyalite/instance_handles.rb', line 69

def next_descendant_id(ancestor_id, destination_id)
  return ancestor_id if ancestor_id == destination_id

  start = ancestor_id.length + SEPARATOR.length
  last = destination_id.index(SEPARATOR, start) || destination_id.length
  destination_id[0,last]
end

.parent_id(id) ⇒ Object



57
58
59
# File 'lib/hyalite/instance_handles.rb', line 57

def parent_id(id)
  id.empty? ? '' : id[0, id.rindex(SEPARATOR)]
end

.root_id_from_node_id(id) ⇒ Object



16
17
18
19
20
21
# File 'lib/hyalite/instance_handles.rb', line 16

def root_id_from_node_id(id)
  if id && id.start_with?(SEPARATOR)
    index = id.index(SEPARATOR, 1)
    index ? id[0...index] : id
  end
end

.root_id_string(index) ⇒ Object



8
9
10
# File 'lib/hyalite/instance_handles.rb', line 8

def root_id_string(index)
  SEPARATOR + index.to_s(36)
end

.root_indexObject



23
24
25
26
27
# File 'lib/hyalite/instance_handles.rb', line 23

def root_index
  index = @root_index
  @root_index += 1
  index
end

.traverse_ancestors(target_id, &cb) ⇒ Object



34
35
36
# File 'lib/hyalite/instance_handles.rb', line 34

def traverse_ancestors(target_id, &cb)
  traverse_parent_path('', target_id, true, false, &cb)
end

.traverse_parent_path(start, stop, skip_first, skip_last, &cb) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hyalite/instance_handles.rb', line 38

def traverse_parent_path(start, stop, skip_first, skip_last, &cb)
  start = start || ''
  stop = stop || ''
  traverse_up = is_ancestor_id_of(stop, start)

  id = start
  loop do
    unless (skip_first && id == start) || (skip_last && id == stop)
      ret = yield(id, traverse_up)
    end

    if ret == false || id == stop
      break
    end

    id = traverse_up ? parent_id(id) : next_descendant_id(id, stop)
  end
end

.traverse_two_phase(target_id, &cb) ⇒ Object



29
30
31
32
# File 'lib/hyalite/instance_handles.rb', line 29

def traverse_two_phase(target_id, &cb)
  traverse_parent_path('', target_id, true, false, &cb)
  traverse_parent_path(target_id, '', false, true, &cb)
end