Class: FactoryFaster::Faster

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_faster/faster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Faster

Returns a new instance of Faster.



7
8
9
10
11
12
13
14
15
16
# File 'lib/factory_faster/faster.rb', line 7

def initialize(filename)
  @filename = filename
  data_store_file = if defined?(Rails)
    "#{Rails.root}/tmp/factory_faster.txt"
  else
    "tmp/factory_faster.txt"
  end
  @data_store = DataStore.new(data_store_file)
  @replacement_targets = load_replacement_targets
end

Instance Attribute Details

#data_storeObject (readonly)

Returns the value of attribute data_store.



5
6
7
# File 'lib/factory_faster/faster.rb', line 5

def data_store
  @data_store
end

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/factory_faster/faster.rb', line 5

def filename
  @filename
end

#replacement_targetsObject (readonly)

Returns the value of attribute replacement_targets.



5
6
7
# File 'lib/factory_faster/faster.rb', line 5

def replacement_targets
  @replacement_targets
end

Instance Method Details

#fixable_replacement_targetsObject



61
62
63
# File 'lib/factory_faster/faster.rb', line 61

def fixable_replacement_targets
  replacement_targets.select {|t| !t.skip? }
end

#load_replacement_targetsObject



81
82
83
84
85
86
87
88
# File 'lib/factory_faster/faster.rb', line 81

def load_replacement_targets
  res = []
  File.readlines(filename).each_with_index do |line, idx|
    next unless line.match /FactoryGirl.create/
    res << Target.new(idx, data_store.skips_for(filename).include?(idx))
  end
  res
end

#processObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/factory_faster/faster.rb', line 18

def process
  puts "Processing #{filename}"
  if data_store.unchanged?(filename)
    puts "Skipping since it hasn't changed since it was last checked"
    return
  end
  if fixable_replacement_targets.empty?
    puts "Skipping since there are no targets to fix"
    return
  end
  initial_fixable_targets_count = fixable_replacement_targets.size
  fixable_replacement_targets.select {|t| !t.skip? }.each_with_index do |target, idx|
    puts "Checking target #{idx+1} of #{initial_fixable_targets_count} on line #{target.line_number}"
    try_to_fix(target)
  end
  passed = fixable_replacement_targets.select {|t| t.passed? }
  if passed.any?
    puts "#{passed.size} of #{initial_fixable_targets_count} could be replaced, so replacing those"
    passed.each do |target|
      replace_create_with_build(target.line_number)
    end
  end
  data_store.set(filename, replacement_targets.select {|t| t.skip? }.map(&:line_number))
end

#replace_create_with_build(line_number) ⇒ Object



75
76
77
78
79
# File 'lib/factory_faster/faster.rb', line 75

def replace_create_with_build(line_number)
  sed_line_number = line_number + 1
  cmd = "sed -i '' '#{sed_line_number}s/FactoryGirl.create/FactoryGirl.build/' #{filename}"
  `#{cmd}`
end

#revert_fileObject



57
58
59
# File 'lib/factory_faster/faster.rb', line 57

def revert_file
  `git checkout -- #{filename}`
end

#run_testObject



70
71
72
73
# File 'lib/factory_faster/faster.rb', line 70

def run_test
  cmd = "bundle exec ruby #{filename} 2>&1"
  `#{cmd}`
end

#test_passes?Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/factory_faster/faster.rb', line 65

def test_passes?
  run_test
  $?.exitstatus == 0
end

#try_to_fix(target) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/factory_faster/faster.rb', line 43

def try_to_fix(target)
  puts "Replacing create with build"
  replace_create_with_build(target.line_number)
  puts "Running test"
  if test_passes?
    puts "Passed!"
    target.passed = true
  else
    puts "Error, so marking as a skip"
    target.skip = true
  end
  revert_file
end