Class: InsertionHelper

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

Overview

Suprails: The customizable wrapper to the rails command

Copyright 2008 Bradley Grzesiak This file is part of Suprails.

Suprails is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Suprails is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Suprails.  If not, see <http://www.gnu.org/licenses/>.

Class Method Summary collapse

Class Method Details

.file_sub!(filename, pattern, contents) ⇒ Object



29
30
31
32
33
# File 'lib/insertion_helper.rb', line 29

def self.file_sub! filename, pattern, contents
  file_contents = File.read(filename)
  file_contents.sub!(pattern, contents)
  File.open(filename, 'w') {|f| f << file_contents }
end

.insert_above(filename, search_string, contents) ⇒ Object



35
36
37
38
39
# File 'lib/insertion_helper.rb', line 35

def self.insert_above filename, search_string, contents
  file_contents = File.read(filename)
  file_contents.sub!(/(^.*#{Regexp.escape(search_string)}.*$)/, "#{contents}\n\\1")
  File.open(filename, 'w') {|f| f << file_contents}
end

.insert_at(filename, line_number, contents) ⇒ Object



23
24
25
26
27
# File 'lib/insertion_helper.rb', line 23

def self.insert_at filename, line_number, contents
  file_contents = File.readlines(filename)
  file_contents.insert(line_number-1, "#{contents}\n")
  File.open(filename, 'w') {|f| f << file_contents }
end