Class: DRG::Tasks::UpgradeFile

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/drg/tasks/upgrade_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

log

Constructor Details

#initialize(file_name) ⇒ UpgradeFile

Returns a new instance of UpgradeFile.



8
9
10
# File 'lib/drg/tasks/upgrade_file.rb', line 8

def initialize(file_name)
  @file = Bundler.root.join(file_name.to_s)
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



6
7
8
# File 'lib/drg/tasks/upgrade_file.rb', line 6

def file
  @file
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/drg/tasks/upgrade_file.rb', line 12

def call
  ruby_files.each do |ruby_file|
    contents = File.read(ruby_file)
    log %(Updating "#{ruby_file}")
    contents.gsub! /:(\w+)\s?=>/, '\1:'
    contents.gsub!(/([A-Z]*[a-z0-9_!?.\[\]'()+=>:,&]+)\.(should)\s?==/, 'expect(\1).to eq')
    contents.gsub! /Factory\.create/, 'create'
    contents.gsub! /Factory\.build/, 'build'
    contents.gsub! /Factory\.next/, 'generate'
    contents.gsub! /Factory\(/, 'create('
    contents.gsub! /Factory\.attributes_for/, 'FactoryGirl.attributes_for'
    File.write(ruby_file, contents)
  end
  if ruby_files.empty?
    log %(No files found for "#{file}")
  end
  log 'Done.'
end

#ruby_filesObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/drg/tasks/upgrade_file.rb', line 31

def ruby_files
  if File.directory?(file)
    Dir[File.join(file, '**', '*.rb')]
  else
    if file.extname.empty?
      ["#{file}.rb"]
    else
      [file]
    end
  end
end