Sha256: d6a63386c54967344eb16aae57af3a9946e34a16f98028c89a5443d1951a6cb6
Contents?: true
Size: 1.73 KB
Versions: 2
Compression:
Stored size: 1.73 KB
Contents
module FileMutate module InsertContent def insert *args, &block File.insert_into self.path, *args, &block end module ClassMethods # insert_into 'my_file.txt', :after => 'Blip', :content => 'Hello # insert_into 'my_file.txt', 'Hello', :after => 'Blip' # insert_into 'my_file.txt', :after => 'Blip' do # 'Hello' # end def insert_into file_name, *args, &block options = last_option args content = insertion_content options, *args, &block # no content to insert? return nil if content.blank? file = begin get_file(file_name) rescue return nil end # already inserted? return nil if !options[:repeat] && file.has_content?(content) # find where to insert place, marker = if options[:before] [ :before, options[:before] ] elsif options[:before_last] [ :before_last, options[:before_last] ] else [ :after, options[:after] ] end return nil if !file.has_content?(marker) # do mutation res = file.mutate marker, place do content end res end def insertion_content options = {}, *args, &block case args.first when String args.first when Hash options[:content] || (yield if block) else return yield if block raise ArgumentError, "You must supply content to insert, either as a String before the options hash, a :content option or a block" end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
file_mutate-0.1.3 | lib/file_mutate/insert_content.rb |
file_mutate-0.1.2 | lib/file_mutate/insert_content.rb |