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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/code_breaker/parsable/node.rb', line 34

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

  if node_type.empty?
    super
  else
    message = [
      "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\n"
    ].join("\n")

    raise NotImplementedError, message
  end
end

Instance Method Details

#parse(node) ⇒ Object



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

def parse(node)
  return if node.nil?

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

#parse_as_hash(node) ⇒ Object



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

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

#parse_as_last_child_hash(node) ⇒ Object



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

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

#parse_as_node_type(node) ⇒ Object



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

def parse_as_node_type(node)
  node.type
end

#parse_children(node) ⇒ Object



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

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