Class: FactorySloth::CodeMod

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/factory_sloth/code_mod.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, code) ⇒ CodeMod

Returns a new instance of CodeMod.



16
17
18
19
20
# File 'lib/factory_sloth/code_mod.rb', line 16

def initialize(path, code)
  self.path = path
  self.original_code = code
  self.patched_code = code
end

Instance Attribute Details

#changed_create_callsObject

Returns the value of attribute changed_create_calls.



3
4
5
# File 'lib/factory_sloth/code_mod.rb', line 3

def changed_create_calls
  @changed_create_calls
end

#create_callsObject

Returns the value of attribute create_calls.



3
4
5
# File 'lib/factory_sloth/code_mod.rb', line 3

def create_calls
  @create_calls
end

#original_codeObject

Returns the value of attribute original_code.



3
4
5
# File 'lib/factory_sloth/code_mod.rb', line 3

def original_code
  @original_code
end

#patched_codeObject

Returns the value of attribute patched_code.



3
4
5
# File 'lib/factory_sloth/code_mod.rb', line 3

def patched_code
  @patched_code
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/factory_sloth/code_mod.rb', line 3

def path
  @path
end

Class Method Details

.call(path, code) ⇒ Object



12
13
14
# File 'lib/factory_sloth/code_mod.rb', line 12

def self.call(path, code)
  new(path, code).tap(&:call)
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/factory_sloth/code_mod.rb', line 22

def call
  self.create_calls = CreateCallFinder.call(code: original_code)

  self.changed_create_calls = find_changeable_create_calls

  # validate whole spec after changes, e.g. to detect side-effects
  self.ok = changed_create_calls.none? || begin
    FactorySloth.verbose && puts("Checking whole file after changes")
    run(patched_code).success?
  end
  ok? || changed_create_calls.clear && patched_code.replace(original_code)
end

#messageObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/factory_sloth/code_mod.rb', line 39

def message
  stats = "#{path}: #{create_count} create calls found, #{change_count} "\
          "#{FactorySloth.dry_run ? 'replaceable' : 'replaced'}"

  return "🔴 #{stats} (conflict)" unless ok?

  if create_count == 0
    "⚪️ #{stats}"
  elsif change_count == 0
    "🟡 #{stats}"
  else
    "🟢 #{stats}"
  end
end

#ok?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/factory_sloth/code_mod.rb', line 35

def ok?
  @ok
end