Class: Holistic::Ruby::Parser::NestingSyntax

Inherits:
Object
  • Object
show all
Defined in:
lib/holistic/ruby/parser/nesting_syntax.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = []) ⇒ NestingSyntax

Returns a new instance of NestingSyntax.



37
38
39
40
# File 'lib/holistic/ruby/parser/nesting_syntax.rb', line 37

def initialize(value = [])
  @value = Array(value)
  @is_root_scope = false
end

Instance Attribute Details

#is_root_scopeObject (readonly)

Returns the value of attribute is_root_scope.



5
6
7
# File 'lib/holistic/ruby/parser/nesting_syntax.rb', line 5

def is_root_scope
  @is_root_scope
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/holistic/ruby/parser/nesting_syntax.rb', line 5

def value
  @value
end

Class Method Details

.build(node) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/holistic/ruby/parser/nesting_syntax.rb', line 7

def self.build(node)
  nesting_syntax = NestingSyntax.new

  append = ->(node) do
    case node
    when ::SyntaxTree::ConstRef     then nesting_syntax << node.child_nodes[0].value
    when ::SyntaxTree::VarField     then nesting_syntax << node.child_nodes.first.value
    when ::SyntaxTree::Const        then nesting_syntax << node.value
    when ::SyntaxTree::VCall        then append.(node.child_nodes.first)
    when ::SyntaxTree::Ident        then nesting_syntax << node.value
    when ::SyntaxTree::Kw           then nesting_syntax << node.value
    when ::SyntaxTree::IVar         then nesting_syntax << node.value
    when ::SyntaxTree::Period       then nesting_syntax << "."
    when ::SyntaxTree::Paren        then append.(node.child_nodes[1])       # node.child_nodes[0] is ::SyntaxTree::LParen
    when ::SyntaxTree::ARef         then append.(node.child_nodes.first)    # not sure what to do here e.g. `ActiveRecord::Migration[7.0]`
    when ::SyntaxTree::CallNode     then node.child_nodes.each(&append)
    when ::SyntaxTree::IfOp         then nesting_syntax << "[conditional]"  # not sure what tod o here
    when ::SyntaxTree::VarRef       then node.child_nodes.each(&append)
    when ::SyntaxTree::ConstPathRef then node.child_nodes.each(&append)
    when ::SyntaxTree::Statements   then node.child_nodes.each(&append)
    when ::SyntaxTree::TopConstRef  then nesting_syntax.mark_as_root_scope! and node.child_nodes.each(&append)
    # else pp(original_node) and pp(Current.file.path) and raise "Unexpected node type: #{node.class}"
    end
  end

  append.(node)

  nesting_syntax
end

Instance Method Details

#constant?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/holistic/ruby/parser/nesting_syntax.rb', line 58

def constant?
  return false if unsupported?

  @value.last[0].then { _1 == _1.upcase }
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


68
69
70
# File 'lib/holistic/ruby/parser/nesting_syntax.rb', line 68

def eql?(other)
  other.class == self.class && other.to_s == to_s
end

#mark_as_root_scope!Object



42
43
44
# File 'lib/holistic/ruby/parser/nesting_syntax.rb', line 42

def mark_as_root_scope!
  @is_root_scope = true
end

#root_scope_resolution?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/holistic/ruby/parser/nesting_syntax.rb', line 54

def root_scope_resolution?
  is_root_scope
end

#supported?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/holistic/ruby/parser/nesting_syntax.rb', line 46

def supported?
  @value.any?
end

#to_sObject



64
65
66
# File 'lib/holistic/ruby/parser/nesting_syntax.rb', line 64

def to_s
  @value.join("::")
end

#unsupported?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/holistic/ruby/parser/nesting_syntax.rb', line 50

def unsupported?
  !supported?
end