Class: FactorySloth::CodeMod

Inherits:
Object
  • Object
show all
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.



8
9
10
11
12
# File 'lib/factory_sloth/code_mod.rb', line 8

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.



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

def changed_create_calls
  @changed_create_calls
end

#create_callsObject

Returns the value of attribute create_calls.



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

def create_calls
  @create_calls
end

#original_codeObject

Returns the value of attribute original_code.



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

def original_code
  @original_code
end

#patched_codeObject

Returns the value of attribute patched_code.



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

def patched_code
  @patched_code
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.call(path, code) ⇒ Object



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

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

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/factory_sloth/code_mod.rb', line 14

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

  # Performance note: it might be faster to write ALL possible patches for a
  # given spec file to tempfiles first, and then run them all in a single
  # rspec call. However, this would make it impossible to use `--fail-fast`,
  # and might make examples fail that are not as idempotent as they should be.
  self.changed_create_calls =
    create_calls.sort_by { |call| [-call.line, -call.column] }.select do |call|
      build_result = try_patch(call, 'build')
      next if build_result == ABORT

      build_result == SUCCESS || try_patch(call, 'build_stubbed') == SUCCESS
    end

  # validate whole spec after changes, e.g. to detect side-effects
  self.ok = changed_create_calls.none? ||
    FactorySloth::SpecRunner.call(path, patched_code).success?
  changed_create_calls.clear unless ok?
  patched_code.replace(original_code) unless ok?
end

#change_countObject



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

def change_count
  changed_create_calls.count
end

#changed?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/factory_sloth/code_mod.rb', line 40

def changed?
  change_count > 0
end

#create_countObject



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

def create_count
  create_calls.count
end

#ok?Boolean

Returns:

  • (Boolean)


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

def ok?
  @ok
end