Class: FactorySloth::CreateCallFinder

Inherits:
Ripper
  • Object
show all
Defined in:
lib/factory_sloth/create_call_finder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ CreateCallFinder

Returns a new instance of CreateCallFinder.



10
11
12
13
14
15
# File 'lib/factory_sloth/create_call_finder.rb', line 10

def initialize(code, ...)
  super
  @code = code
  @disabled = false
  @calls = []
end

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



4
5
6
# File 'lib/factory_sloth/create_call_finder.rb', line 4

def calls
  @calls
end

Class Method Details

.call(code:) ⇒ Object



6
7
8
# File 'lib/factory_sloth/create_call_finder.rb', line 6

def self.call(code:)
  new(code).tap(&:parse).calls
end

Instance Method Details

#on_call(mod, _, obj) ⇒ Object



27
28
29
# File 'lib/factory_sloth/create_call_finder.rb', line 27

def on_call(mod, _, obj, *)
  store_call(obj) if mod == 'FactoryBot'
end

#on_command_call(mod, _, obj) ⇒ Object



31
32
33
# File 'lib/factory_sloth/create_call_finder.rb', line 31

def on_command_call(mod, _, obj, *)
  store_call(obj) if mod == 'FactoryBot'
end

#on_comment(text) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/factory_sloth/create_call_finder.rb', line 35

def on_comment(text, *)
  return unless /sloth:(?<directive>disable|enable)/ =~ text

  directive == 'disable' &&
    @calls.reject! { |obj| obj[1] == lineno } ||
    (@lines ||= @code.lines)[lineno - 1].match?(/^\s*#/) &&
    (@disabled = directive != 'enable')
end

#on_fcall(loc) ⇒ Object



44
45
46
# File 'lib/factory_sloth/create_call_finder.rb', line 44

def on_fcall(loc, *)
  store_call(loc)
end

#on_ident(name) ⇒ Object



22
23
24
25
# File 'lib/factory_sloth/create_call_finder.rb', line 22

def on_ident(name, *)
  %w[create create_list create_pair].include?(name) &&
    FactorySloth::CreateCall.new(name: name, line: lineno, column: column)
end

#on_vcall(loc) ⇒ Object



48
49
50
# File 'lib/factory_sloth/create_call_finder.rb', line 48

def on_vcall(loc, *)
  store_call(loc)
end

#store_call(obj) ⇒ Object



18
19
20
# File 'lib/factory_sloth/create_call_finder.rb', line 18

def store_call(obj)
  @calls << obj if obj.is_a?(FactorySloth::CreateCall) && !@disabled
end