Module: Iroki::Utils

Defined in:
lib/iroki/utils/utils.rb

Instance Method Summary collapse

Instance Method Details

#add_color_to_leaf_branch(patterns, node, exact, iroki_to_name = nil) ⇒ Object



25
26
27
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
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/iroki/utils/utils.rb', line 25

def add_color_to_leaf_branch patterns, node, exact, iroki_to_name=nil
  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
    elsif Iroki::Color::default_color_tag
      color = Iroki::Color::default_color_tag

      return color
    else
      return nil
    end
  else
    assert iroki_to_name, "iroki_to_name arg is nil"
    assert iroki_to_name[node.to_s],
           "iroki_to_name is missing #{node.to_s}"
    node_s = iroki_to_name[node.to_s]

    patterns.each do |pattern, this_color|
      if node_s =~ pattern
        abort_if already_matched,
                 "Non specific matching for #{node_s}. " +
                 "Previously matched pattern was " +
                 "#{already_matched.inspect}. Current pattern " +
                 "is " +
                 "#{pattern.inspect}. Color was #{this_color}."

        color = this_color
        already_matched = pattern
      end
    end

    # if there was no match and there is a default color
    if !color && Iroki::Color::default_color_tag
      color = Iroki::Color::default_color_tag
    end

    return color
  end
end

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/iroki/utils/utils.rb', line 80

def color_nodes patterns, tree, node, exact, iroki_to_name
  # # 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
  if node.name
    node.name = node.name #.clean_name
  else
    node.name = nil
  end

  # if its a leaf that hasnt been checked & needs color
  # TODO the node.name is there to make sure already_checked?
  # doesn't blow up. When can node.name be nil?
  if(leaf?(tree, node) &&
     node.name &&
     !node.name.already_checked?)
    # 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,
                                     iroki_to_name

    # add color to the name
    node.name = node.name + color[:branch] 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, iroki_to_name
      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



72
73
74
75
76
77
78
# File 'lib/iroki/utils/utils.rb', line 72

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)


21
22
23
# File 'lib/iroki/utils/utils.rb', line 21

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