Class: FactorySloth::CodeMod
- Inherits:
-
Object
- Object
- FactorySloth::CodeMod
- Defined in:
- lib/factory_sloth/code_mod.rb
Instance Attribute Summary collapse
-
#changed_create_calls ⇒ Object
readonly
Returns the value of attribute changed_create_calls.
-
#create_calls ⇒ Object
readonly
Returns the value of attribute create_calls.
-
#original_code ⇒ Object
readonly
Returns the value of attribute original_code.
-
#patched_code ⇒ Object
readonly
Returns the value of attribute patched_code.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
- #change_count ⇒ Object
- #changed? ⇒ Boolean
- #create_count ⇒ Object
-
#initialize(path, code) ⇒ CodeMod
constructor
A new instance of CodeMod.
- #ok? ⇒ Boolean
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_calls ⇒ Object
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_calls ⇒ Object
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_code ⇒ Object
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_code ⇒ Object
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 |
#path ⇒ Object
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
#call ⇒ Object
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_count ⇒ Object
48 49 50 |
# File 'lib/factory_sloth/code_mod.rb', line 48 def change_count changed_create_calls.count end |
#changed? ⇒ Boolean
40 41 42 |
# File 'lib/factory_sloth/code_mod.rb', line 40 def changed? change_count > 0 end |
#create_count ⇒ Object
44 45 46 |
# File 'lib/factory_sloth/code_mod.rb', line 44 def create_count create_calls.count end |
#ok? ⇒ Boolean
36 37 38 |
# File 'lib/factory_sloth/code_mod.rb', line 36 def ok? @ok end |