Class: Rbr::Matchers

Inherits:
Object
  • Object
show all
Defined in:
lib/rbr/matchers.rb

Overview

methods for determining if a node matches given conditions

Class Method Summary collapse

Class Method Details

.ar_update(node, name) ⇒ Object

Updating an ActiveRecord model attribute



16
17
18
19
20
21
22
23
24
25
# File 'lib/rbr/matchers.rb', line 16

def self.ar_update(node, name)
  name &&
    (
      ar_update_hash(node, name) ||
      ar_update_positional(node, name) ||
      ar_update_dynamic_method(node, name) ||
      ar_update_attributes(node, name) ||
      ar_update_hash_element(node, name)
    )
end

.assignment(node, name) ⇒ Object

Assignment to a specified lvalue



28
29
30
31
32
# File 'lib/rbr/matchers.rb', line 28

def self.assignment(node, name)
  name &&
    node.assignment? &&
    node.value == name
end

.const(node, name) ⇒ Object

Node is a Ruby constant



47
48
49
50
51
# File 'lib/rbr/matchers.rb', line 47

def self.const(node, name)
  name &&
    node.const? &&
    node.children.last == name
end

.literal(node, value) ⇒ Object

Node is a literal int, float, or string



35
36
37
# File 'lib/rbr/matchers.rb', line 35

def self.literal(node, value)
  number(node, value) || string(node, value)
end

.match(node, matcher, condition) ⇒ Object



6
7
8
# File 'lib/rbr/matchers.rb', line 6

def self.match(node, matcher, condition)
  send(matcher, node, condition)
end

.method_call(node, name) ⇒ Object

Method call



11
12
13
# File 'lib/rbr/matchers.rb', line 11

def self.method_call(node, name)
  name && node.method_call?(name)
end

.number(node, value) ⇒ Object

Node is a literal int or float



40
41
42
43
44
# File 'lib/rbr/matchers.rb', line 40

def self.number(node, value)
  value &&
    node.number? &&
    node.value.to_s == value
end

.string(node, pattern) ⇒ Object

Node is a string



54
55
56
57
58
59
60
# File 'lib/rbr/matchers.rb', line 54

def self.string(node, pattern)
  pattern &&
    node.str? &&
    node.any_descendant_matches?(
      ->(n) { n.is_a?(String) && n.match?(pattern) }
    )
end