Sha256: a705c7643e2ffb94c10aa7a70b9d85be556805af3cbbb0f93da4623d80927a68
Contents?: true
Size: 1.57 KB
Versions: 1
Compression:
Stored size: 1.57 KB
Contents
require 'optparse' module WordReplacer class Processor OPTIONS = [:word, :replacement, :path] include Singleton def options @options||= {} end def process parse_input case when File.directory?(options[:path]) replace_in_path options[:word], options[:replacement], options[:path] else replace_text options[:word], options[:replacement], options[:path] end puts "Replacement Done" end def parse_input opts = {} OptionParser.new do |opt| opt.on('-w','--word [WORD]', 'the word to replace') { |o| opts[:word] = o } opt.on('-r','--replacement [REPLACEMENT]', 'the replacement') { |o| opts[:replacement] = o } opt.on('-p','--path [PATH]', 'the path or file where is going to be replaced') { |o| opts[:path] = o } end.parse! OPTIONS.each do |opt_name| options[opt_name] = opts.fetch(opt_name) do raise ArgumentError, "no '#{opt_name.to_s}' option specified as a parameter" end end puts "Processing input: #{options.inspect}" end def replace_text(word, replacement, path) data = File.read(path) if data.include? word filtered_data = data.gsub(word, replacement) # open the file for writing File.open(path, "w") do |f| f.write(filtered_data) end end end def replace_in_path(word, replacement, path) Dir["#{path}/**/*"].select{|f| File.file?(f) }.each do |file_path| replace_text(word, replacement, file_path) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
word_replacer-0.1.0 | lib/word_replacer/processor.rb |