Class: Constree::Node

Inherits:
Struct
  • Object
show all
Defined in:
lib/constree/node.rb

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



2
3
4
# File 'lib/constree/node.rb', line 2

def constant
  @constant
end

#is_lastObject

Returns the value of attribute is_last

Returns:

  • (Object)

    the current value of is_last



2
3
4
# File 'lib/constree/node.rb', line 2

def is_last
  @is_last
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/constree/node.rb', line 2

def name
  @name
end

#parentObject

Returns the value of attribute parent

Returns:

  • (Object)

    the current value of parent



2
3
4
# File 'lib/constree/node.rb', line 2

def parent
  @parent
end

#refObject

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

#ancestorsObject



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

#branchObject



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

def branch
  return '' unless parent
  last? ? '└─' : '├─'
end

#display_nameObject



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

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

#full_nameObject



55
56
57
# File 'lib/constree/node.rb', line 55

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

#indentObject



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

Returns:

  • (Boolean)


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

def last?
  is_last ? true : false
end

#levelObject



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

def level
  indent + branch + display_name + ' ' + type
end

#not_yet?(seen) ⇒ Boolean

Returns:

  • (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_nodesObject



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

Returns:

  • (Boolean)


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

def top?
  parent ? false : true
end

#typeObject



64
65
66
# File 'lib/constree/node.rb', line 64

def type
  ref ? "#{ref.full_name}" : "(#{constant.class.to_s})"
end