Sha256: ef1816c3f471e144a729e159b19cfbab45357be289af784860752b2365af5b94

Contents?: true

Size: 1.94 KB

Versions: 6

Compression:

Stored size: 1.94 KB

Contents

# Mostly pinched from http://github.com/ryanb/nifty-generators/tree/master

Rails::Generator::Commands::Base.class_eval do
  def file_contains?(relative_destination, line)
    File.read(destination_path(relative_destination)).include?(line)
  end
end

Rails::Generator::Commands::Create.class_eval do
  def insert_before(file, line, stop='^(class|module) .+$')
    logger.insert "#{line} into #{file}"
    unless options[:pretend] || file_contains?(file, line)
      gsub_file file, /^#{stop}/ do |match|
        "#{line}\n#{match}"
      end
    end
  end
  
  def insert_after(file, line, stop='^(class|module) .+$')
    logger.insert "#{line} into #{file}"
    @run = false
    unless options[:pretend] || file_contains?(file, line)
      gsub_file file, /#{stop}/ do |match|
        @run = true
        "#{match}\n  #{line}"
      end
      raise "Append Key Not Found, was looking for #{stop}" unless @run
    end
  end
  
  def append(file, line)
    logger.insert "added legacy adapter to end of database.yml"
    unless options[:pretend] || file_contains?(file, line)
      File.open(file, "a") do |file|
        file.write("\n" + line)
      end
    end
  end
  
end

Rails::Generator::Commands::Destroy.class_eval do
  def insert_before(file, line, stop='')
    logger.remove "#{line} from #{file}"
    unless options[:pretend]
      gsub_file file, "\n  #{line}", ''
    end
  end

  def insert_after(file, line, stop='')
    logger.remove "#{line} from #{file}"
    unless options[:pretend]
      gsub_file file, "\n  #{line}", ''
    end
  end
  
  def append(file, line)
    logger.insert "added legacy adapter to end of database.yml"
  end

end

Rails::Generator::Commands::List.class_eval do
  def insert_before(file, line, stop='')
    logger.insert "#{line} into #{file}"
  end
  
  def insert_after(file, line, stop='')
    logger.insert "#{line} into #{file}"
  end
  
  def append(file, line)
    logger.insert "added legacy adapter to end of database.yml"
  end
  
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
trucker-0.5.1 generators/truck/lib/insert_commands.rb
trucker-0.5.0 generators/truck/lib/insert_commands.rb
trucker-0.4.1 generators/truck/lib/insert_commands.rb
trucker-0.3.0 generators/truck/lib/insert_commands.rb
trucker-0.2.0 generators/truck/lib/insert_commands.rb
trucker-0.1.0 generators/truck/lib/insert_commands.rb