Class: Constree::Node

Inherits:
Struct
  • Object
show all
Includes:
TreeGraph, TreeHtml
Defined in:
lib/constree/node.rb

Direct Known Subclasses

NodeWithAncestors

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#constantObject

Returns the value of attribute constant

Returns:

  • (Object)

    the current value of constant



5
6
7
# File 'lib/constree/node.rb', line 5

def constant
  @constant
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



5
6
7
# File 'lib/constree/node.rb', line 5

def name
  @name
end

#parentObject

Returns the value of attribute parent

Returns:

  • (Object)

    the current value of parent



5
6
7
# File 'lib/constree/node.rb', line 5

def parent
  @parent
end

#refObject

Returns the value of attribute ref.



29
30
31
# File 'lib/constree/node.rb', line 29

def ref
  @ref
end

Instance Method Details

#==(other) ⇒ Object



51
52
53
54
55
# File 'lib/constree/node.rb', line 51

def == other
  return false unless constant.is_a? Module
  return false unless other.is_a? Node
  constant == other.constant
end

#children_for_tree_graphObject Also known as: children_for_tree_html



13
14
15
# File 'lib/constree/node.rb', line 13

def children_for_tree_graph
  @sub_consts ||= []
end

#css_for_tree_htmlObject



25
26
27
# File 'lib/constree/node.rb', line 25

def css_for_tree_html
  '.hl{color: coral;}'
end

#display_nameObject



43
44
45
# File 'lib/constree/node.rb', line 43

def display_name
  (name || constant.name).to_s
end

#full_nameObject



47
48
49
# File 'lib/constree/node.rb', line 47

def full_name
  top? ? constant.name : "#{parent.full_name}::#{name}"
end

#label_for_tree_graphObject



9
10
11
# File 'lib/constree/node.rb', line 9

def label_for_tree_graph
  "#{display_name} #{verbose}"
end

#label_for_tree_htmlObject



19
20
21
# File 'lib/constree/node.rb', line 19

def label_for_tree_html
  "<span class='hl'>#{display_name}</span> #{verbose}"
end

#not_yet?(seen) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
# File 'lib/constree/node.rb', line 75

def not_yet? seen
  i = seen.find_index self
  if i == seen.count - 1
   true
  else
    self.ref = seen[i]
    false
  end
end

#sub_nodesObject



31
32
33
34
35
36
37
# File 'lib/constree/node.rb', line 31

def sub_nodes
  return [] unless constant.is_a? Module
  constant.constants.sort!.map! do |name|
    const_value = constant.const_get(name)
    Node.new(const_value, name, self)
  end
end

#top?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/constree/node.rb', line 39

def top?
  parent ? false : true
end

#verboseObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/constree/node.rb', line 57

def verbose
  return {ref: ref.full_name} if ref

  info = {kla: constant.class}
  return info unless constant.is_a? Module

  ancestors = constant.ancestors
  idx = ancestors.index(constant)
  if idx > 0 && !(before = ancestors[0 .. (idx - 1)]).empty?
    info[:bef] = before
  end
  unless (after = ancestors[(idx + 1) .. -1]).empty?
    info[:aft] = after
  end

  info
end