lib/sugar-high/file.rb in sugar-high-0.3.2 vs lib/sugar-high/file.rb in sugar-high-0.3.3
- old
+ new
@@ -1,9 +1,10 @@
require 'sugar-high/blank'
require 'sugar-high/arguments'
require 'sugar-high/path'
require 'sugar-high/regexp'
+require 'sugar-high/string'
class File
def self.delete! name
return nil if !File.exist?(name)
File.delete name
@@ -124,14 +125,17 @@
# already inserted?
return nil if content.blank? || (file.read =~ /#{content}/)
place, marker = if options[:before]
- [ :before, options[:before] ]
+ [ :before, options[:before] ]
+ elsif options[:before_last]
+ [ :before_last, options[:before_last] ]
else
- [ :after, options[:after] ]
+ [ :after, options[:after] ]
end
+
marker = Insert.get_marker marker
return nil if !(File.new(file.path).read =~ /#{marker}/)
@@ -163,11 +167,18 @@
end
end
module Mutate
def self.mutate_file file, marker, place, &block
- raise ArgumentError, "You must define a replacement marker for a :before or :after key" if !marker
-
+ raise ArgumentError, "You must define a replacement marker for a :before, :before_last or :after key" if !marker
+
+ if place == :before_last
+ content = File.read(file)
+ content = content.insert_before_last yield, marker
+ File.open(file, 'wb') { |file| file.write(content) }
+ return
+ end
+
replace_in_file file, /(#{marker})/mi do |match|
place == :after ? "#{match}\n #{yield}" : "#{yield}\n #{match}"
end
end