Module: ReplaceClass
- Defined in:
- lib/replace_class/replace_class.rb,
lib/replace_class/utils.rb,
lib/replace_class/version.rb,
lib/replace_class/option_parser.rb
Overview
main module for this gem
Example
ReplaceClass.start
Defined Under Namespace
Constant Summary collapse
- VERSION =
'1.1.1'
Class Method Summary collapse
-
.checkDir(dir) ⇒ Object
:stopdoc:.
-
.match?(line_or_filename) ⇒ Boolean
short cut for matching string.
- .replace(line_or_filename) ⇒ Object
-
.start ⇒ Object
replace class from source to dest (include file name).
Class Method Details
.checkDir(dir) ⇒ Object
:stopdoc:
traverse dir for checking replacement
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/replace_class/replace_class.rb', line 66 def self.checkDir(dir) aDir = Dir.new(dir) aDir.each do |filename| filePath = File.join(dir, filename) isFile = File.file?(filePath) isDir = File.directory?(filePath) if isDir then if !ReplaceClass::Util.hidden?(filename) && !ReplaceClass::Util.check_ignore_dir(filename) checkDir(filePath) end end if isFile then unless ReplaceClass::Util.check_valid_with_file(filename) next end if ReplaceClass::Util.hidden?(filename) next end if match?(filename) puts "matching filename: #{filename}" newname = replace(filename) File.rename(filePath, File.join(dir, newname)) filePath = File.join(dir, newname) end lines = IO.readlines(filePath) lines.each_index do |index| line = lines[index] if match?(line) puts "matching in : [#{filename} : #{index + 1}] with content:\n#{line}" newline = replace(line) lines[index] = newline end end file_content = lines.join File.open(filePath, "r+") do |file| file.write(file_content) end end end end |
.match?(line_or_filename) ⇒ Boolean
short cut for matching string
51 52 53 54 55 |
# File 'lib/replace_class/replace_class.rb', line 51 def self.match?(line_or_filename) regex = Regexp.new("([^a-zA-Z0-9]|^)#{$options[:source]}([^a-zA-Z0-9]|$)") line_or_filename =~ regex end |
.replace(line_or_filename) ⇒ Object
57 58 59 60 61 |
# File 'lib/replace_class/replace_class.rb', line 57 def self.replace(line_or_filename) regex = Regexp.new("([^a-zA-Z0-9]|^)#{$options[:source]}([^a-zA-Z0-9]|$)") line_or_filename.gsub(regex, "\\1#{$options[:dest]}\\2") end |
.start ⇒ Object
replace class from source to dest (include file name)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/replace_class/replace_class.rb', line 17 def self.start # option parse ReplaceClass::OptParser.parse do |parser, | = # check option if !.has_key?(:source) || !.has_key?(:dest) puts parser.help exit false end # check project file when no path if ARGV.empty? then if Dir["*.xcodeproj"].empty? then puts "current directory have not a filename end with 'xcodeproj'" exit false end end # perform if ARGV.empty? then src_root = File.('../', __FILE__) else src_root = File.(ARGV.last) end checkDir(src_root) end end |