Class: Constree::Node
- Inherits:
-
Struct
- Object
- Struct
- Constree::Node
- Defined in:
- lib/constree/node.rb
Instance Attribute Summary collapse
-
#constant ⇒ Object
Returns the value of attribute constant.
-
#is_last ⇒ Object
Returns the value of attribute is_last.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#ref ⇒ Object
Returns the value of attribute ref.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #ancestors ⇒ Object
- #branch ⇒ Object
- #display_name ⇒ Object
- #full_name ⇒ Object
- #indent ⇒ Object
- #last? ⇒ Boolean
- #level ⇒ Object
- #not_yet?(seen) ⇒ Boolean
- #sub_nodes ⇒ Object
- #top? ⇒ Boolean
- #type ⇒ Object
Instance Attribute Details
#constant ⇒ Object
Returns the value of attribute constant
2 3 4 |
# File 'lib/constree/node.rb', line 2 def constant @constant end |
#is_last ⇒ Object
Returns the value of attribute is_last
2 3 4 |
# File 'lib/constree/node.rb', line 2 def is_last @is_last end |
#name ⇒ Object
Returns the value of attribute name
2 3 4 |
# File 'lib/constree/node.rb', line 2 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent
2 3 4 |
# File 'lib/constree/node.rb', line 2 def parent @parent end |
#ref ⇒ Object
Returns the value of attribute ref.
4 5 6 |
# File 'lib/constree/node.rb', line 4 def ref @ref end |
Instance Method Details
#==(other) ⇒ Object
59 60 61 62 |
# File 'lib/constree/node.rb', line 59 def == other return false unless other.is_a? Node constant == other.constant end |
#ancestors ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/constree/node.rb', line 14 def ancestors return [] unless parent p, anc = parent, [] while p anc.unshift p p = p.parent end anc end |
#branch ⇒ Object
32 33 34 35 |
# File 'lib/constree/node.rb', line 32 def branch return '' unless parent last? ? '└─' : '├─' end |
#display_name ⇒ Object
51 52 53 |
# File 'lib/constree/node.rb', line 51 def display_name (name || constant.name).to_s end |
#full_name ⇒ Object
55 56 57 |
# File 'lib/constree/node.rb', line 55 def full_name top? ? constant.name : "#{parent.full_name}::#{name}" end |
#indent ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/constree/node.rb', line 37 def indent ancestors.map do |a| if a.top? '' else a.last? ? ' ' : '│ ' end end.join end |
#last? ⇒ Boolean
24 25 26 |
# File 'lib/constree/node.rb', line 24 def last? is_last ? true : false end |
#level ⇒ Object
47 48 49 |
# File 'lib/constree/node.rb', line 47 def level indent + branch + display_name + ' ' + type end |
#not_yet?(seen) ⇒ Boolean
68 69 70 71 72 73 74 75 76 |
# File 'lib/constree/node.rb', line 68 def not_yet? seen i = seen.find_index self if i == seen.count - 1 true else self.ref = seen[i] false end end |
#sub_nodes ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/constree/node.rb', line 6 def sub_nodes return [] unless constant.is_a? Module names = constant.constants names.reduce([]) do |nodes, name| nodes << Node.new(constant.const_get(name), name, self, nodes.count == names.count - 1) end end |
#top? ⇒ Boolean
28 29 30 |
# File 'lib/constree/node.rb', line 28 def top? parent ? false : true end |
#type ⇒ Object
64 65 66 |
# File 'lib/constree/node.rb', line 64 def type ref ? "→ #{ref.full_name}" : "(#{constant.class.to_s})" end |