Module: IrokiLib::Utils

Includes:
AbortIf, CoreExt::String
Defined in:
lib/iroki_lib/utils/utils.rb

Instance Method Summary collapse

Methods included from CoreExt::String

#clean, #clean_name, #has_color?, #hex?

Instance Method Details

#add_color_to_leaf_branch(patterns, node, exact) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/iroki_lib/utils/utils.rb', line 28

def add_color_to_leaf_branch patterns, node, exact
  num_matches = 0
  color = nil
  already_matched = false

  if exact # treat patterns as string matching
    node_s = node.to_s
    if patterns.has_key? node_s
      color = patterns[node_s]

      return color
    else
      return nil
    end
  else
    node_s = node.to_s

    patterns.each do |pattern, this_color|
      if node_s =~ pattern
        abort_if already_matched,
                 "Non specific matching for #{node_s}"

        color = this_color
        already_matched = true
      end
    end

    return color
  end
end

#color_nodes(patterns, tree, node, exact) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/iroki_lib/utils/utils.rb', line 67

def color_nodes patterns, tree, node, exact
  # # check if it needs color, if so set the color
  # color = add_color_to_leaf_branch patterns, node, exact

  # clean the name no matter what
  node.name = clean_name node.name

  # if its a leaf that hasnt been checked & needs color
  if leaf?(tree, node) && !already_checked?(node.name) # && color
    # check if it needs color, if so set the color

    # NOTE: this was originally before cleaning the node name a
    # couple lines up, does it matter that it is after?
    color = add_color_to_leaf_branch patterns, node, exact

    # add color to the name
    node.name = node.name + color if color
  elsif !leaf?(tree, node)
    children = tree.children(node) # get the children
    children_colors = []
    children.each do |child|
      # recurse to color the child if needed
      color_nodes patterns, tree, child, exact
      children_colors << get_color(child) # add color of the child
    end

    # if all the children have the same color
    if children_colors.uniq.count == 1
      # set the branch node to only the color name
      node.name = children_colors[0]
    end
  end

  return node
end

#get_color(node) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/iroki_lib/utils/utils.rb', line 59

def get_color node
  begin
    node.name.match(/\[&!color="#[0-9A-Fa-f]{6}"\]/)[0]
  rescue NoMethodError => e
    nil
  end
end

#leaf?(tree, node) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/iroki_lib/utils/utils.rb', line 24

def leaf? tree, node
  tree.children(node).empty?
end