lib/sugar-high/file.rb in sugar-high-0.2.0 vs lib/sugar-high/file.rb in sugar-high-0.2.1

- old
+ new

@@ -1,8 +1,9 @@ require 'sugar-high/blank' require 'sugar-high/arguments' require 'sugar-high/path' +require 'sugar-high/regexp' class File def self.delete! name return nil if !File.exist?(name) File.delete name @@ -67,9 +68,36 @@ File.overwrite file_name, mutated_content true # signal success! end + + def self.read_from file_name, options = {}, &block + raise ArgumentError, "File to read from not found or not a file: #{file_name}" if !File.file? file_name + content = File.read file_name + + if options[:before] + begin + regexp = options[:before].to_regexp + index = content.match(regexp).offset_before + content = content[0..index] + rescue + raise ArgumentError, ":before option must be a string or regular expression, was : #{options[:before]}" + end + end + + if options[:after] + begin + regexp = options[:after].to_regexp + index = content.match(regexp).offset_after + content = content[index..-1] + rescue + raise ArgumentError, ":after option must be a string or regular expression, was : #{options[:after]}" + end + end + yield content if block + content + end def self.remove_content_from file_name, options = {}, &block replace_content_from file_name, options.merge(:with => ''), &block end