Module: Ruby::Node::Traversal::Patch

Included in:
Ruby::Node
Defined in:
lib/traversal/patch.rb

Instance Method Summary collapse

Instance Method Details

#check_pair(type, value) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/traversal/patch.rb', line 39

def check_pair(type, value)
  res = case type
  when :is_a
    has_type?(value)
  when :class
    is_instance_of?(value)
  when :token
    has_token?(value)
  when :left_token
    left.has_token?(value)
  when :right_token
    right.has_token?(value)
  when :value
    has_value?(value)
  when :identifier
    has_identifier?(value)
  when :const
    has_const?(value)
  when :block
    has_block?
  when :namespace          
    has_namespace?(value)
  when :superclass
    superclass?(value)
  when :args, :params
    args?(value)                        
  when :block_params          
    args?(value, :withblock)          
  when :pos, :position
    position?(value)
  when :right_of
    right_of?(value)
  when :left_of
    left_of?(value) 
  else
    true
  end
  # puts "check pair type:#{type}, value:#{value} => #{res}, self: #{self.inspect}" if self.class == Ruby::Variable 
  res
end

#childrenObject



17
18
19
# File 'lib/traversal/patch.rb', line 17

def children
  (self.try(:elements).to_a || prolog.try(:elements).to_a || []) + nodes
end

#matches?(args, &block) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/traversal/patch.rb', line 21

def matches?(args, &block) 
  conditions = args.last.is_a?(::Hash) ? args.pop : {}
  conditions[:is_a] = args unless args.empty?

  type_condition = conditions[:is_a][0]
  if type_condition
    return nil if !has_type?(type_condition)
  end

  res = conditions.inject(!conditions.empty?) do |result, (type, value)|
    block_result = (!block_given? || block.call(self))
    check_res = check_pair(type, value)
    return false if !check_res
    result && check_res && block_result
  end
  res
end

#select(*args, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/traversal/patch.rb', line 5

def select(*args, &block)
  result = []
  result << self if matches?(args.dup, &block)
  children.flatten.compact.inject(result) do |result, node|
    if node.class.to_s == 'Symbol'
      return result
    end
    selected = node.select(*args, &block)                        
    result + selected
  end   
end