Module: FeideeUtils::Mixins::ParentAndPath

Included in:
AccountGroup, Category
Defined in:
lib/feidee_utils/mixins/parent_and_path.rb

Overview

Requires:

instance methods: poid, parent_poid, raw_path
class methods: find_by_id

Defined Under Namespace

Classes: InconsistentDepthException, InconsistentPathException

Constant Summary collapse

NullPOID =
0

Instance Method Summary collapse

Instance Method Details

#has_parent?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/feidee_utils/mixins/parent_and_path.rb', line 66

def has_parent?
  parent_poid != NullPOID
end

#parentObject



62
63
64
# File 'lib/feidee_utils/mixins/parent_and_path.rb', line 62

def parent
  self.class.find_by_id(parent_poid)
end

#pathObject



58
59
60
# File 'lib/feidee_utils/mixins/parent_and_path.rb', line 58

def path
  @path ||= (raw_path.split("/").map do |poid| poid.to_i end)[1..-1]
end

#validate_depth_integrityObject



15
16
17
18
19
20
21
22
# File 'lib/feidee_utils/mixins/parent_and_path.rb', line 15

def validate_depth_integrity
  path_depth = path.length - 1
  if path_depth != depth
    raise InconsistentDepthException,
      "Path is #{path}, but the given depth is #{depth}.\n" +
      inspect
  end
end

#validate_one_level_path_integrityObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/feidee_utils/mixins/parent_and_path.rb', line 24

def validate_one_level_path_integrity
  path_array = path.clone
  last_poid = path_array.pop

  if last_poid != poid
    raise InconsistentPathException,
      "The last node in path is #{last_poid}, but the current poid is #{poid}.\n" +
      inspect
  end

  if has_parent? and path_array != parent.path
    raise InconsistentPathException,
      "Path is #{path}, but path of parent is #{parent.path}.\n" +
      inspect
  end
end

#validate_path_integrity_hardObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/feidee_utils/mixins/parent_and_path.rb', line 41

def validate_path_integrity_hard
  cur = self
  step = 0
  while cur or step != path.length
    step += 1
    poid = path[-step]
    if !cur or poid == nil or poid != cur.poid
      raise InconsistentPathException,
        "Reverse path and trace are different at step #{step}. " +
        "Path shows #{poid}, but trace shows #{cur and cur.poid}.\n" +
        inspect
    end

    cur = cur.has_parent? && cur.parent
  end
end