Module: CodeBreaker::Parsable::Node

Included in:
Assignments, DataTypes, Keywords, LanguageElements, Ranges, VariableTypes, Wrappers
Defined in:
lib/code_breaker/parsable/node.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/code_breaker/parsable/node.rb', line 33

def method_missing(method, *args, &block)
  node_type = method.to_s.match(/^parse_(.+)_node$/).captures.first

  if node_type.empty?
    super
  else
    raise NotImplementedError, not_implemented_message(node_type)
  end
end

Instance Method Details

#not_implemented_message(node_type) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/code_breaker/parsable/node.rb', line 43

def not_implemented_message(node_type)
  [
    %(Breaking the node type "#{node_type}" is not yet implemented.),
    'You can open an issue on this in the project’s Github repo under:',
    'https://github.com/daigaku-ruby/code_breaker/issues/new',
    ''
  ].join("\n")
end

#parse(node) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/code_breaker/parsable/node.rb', line 4

def parse(node)
  return if node.nil?

  if node.is_a?(Symbol)
    node
  else
    send("parse_#{node.type}_node", node)
  end
end

#parse_as_hash(node) ⇒ Object



21
22
23
# File 'lib/code_breaker/parsable/node.rb', line 21

def parse_as_hash(node)
  { node.type => parse_children(node) }
end

#parse_as_last_child_hash(node) ⇒ Object



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

def parse_as_last_child_hash(node)
  { node.type => node.children.last }
end

#parse_as_node_type(node) ⇒ Object



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

def parse_as_node_type(node)
  node.type
end

#parse_children(node) ⇒ Object



14
15
16
17
18
19
# File 'lib/code_breaker/parsable/node.rb', line 14

def parse_children(node)
  node.children.each_with_object([]) do |child, nodes|
    nodes << parse(child) unless child.nil?
    nodes
  end
end