Class: Outil::OCS::ObjectParser

Inherits:
Object
  • Object
show all
Defined in:
lib/outil/ocs/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, name) ⇒ ObjectParser

Returns a new instance of ObjectParser.



7
8
9
10
11
# File 'lib/outil/ocs/parser.rb', line 7

def initialize(path, name)
  @path = path
  @name = name
  @found = false
end

Instance Attribute Details

#foundObject (readonly)

Returns the value of attribute found.



5
6
7
# File 'lib/outil/ocs/parser.rb', line 5

def found
  @found
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/outil/ocs/parser.rb', line 5

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/outil/ocs/parser.rb', line 5

def path
  @path
end

Instance Method Details

#findObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/outil/ocs/parser.rb', line 21

def find
  return @found if @found
  set_and_stop = Proc.new do |node|
    return @found = node
  end
  traverse = Proc.new do |node|
    # if we're not at a node, we can move on
    next unless node.is_a?(Parser::AST::Node)
    # unless we're at a method definition
    # we're probably in a begin block, a class, or module
    # in which case traverse the children of the current node
    node.children.each &traverse unless [:defs, :def].include?(node.type)
    # bottom-level node interrogation...
    case node.type
    when :defs
      # class methods
      set_and_stop.call(node) if node.children[1] == name
      next
    when :def
      # instance method
      set_and_stop.call(node) if node.children.first == name
      next
    else
      # other kind of object
      if node.is_a?(Parser::AST::Node)
        # another parent node
        node.children.each &traverse
      else
        # the bottom of a node
        next
      end
    end
  end
  begin
    traverse.call(node)
  rescue
    false
  end
end

#nodeObject



13
14
15
16
17
18
19
# File 'lib/outil/ocs/parser.rb', line 13

def node
  @node ||= Parser::CurrentRuby.parse(
  File.open(@path, 'r') do |f|
    f.read
  end
  )
end