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

Classes: OptParser, Util

Constant Summary collapse

VERSION =
'1.1.0'

Class Method Summary collapse

Class Method Details

.checkDir(dir) ⇒ Object

:stopdoc:

traverse dir for checking replacement



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
118
119
120
# File 'lib/replace_class/replace_class.rb', line 69

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

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/replace_class/replace_class.rb', line 54

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



60
61
62
63
64
# File 'lib/replace_class/replace_class.rb', line 60

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

.startObject

– TODO: finish replace job ++ replace class from source to dest (include file name)



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
48
49
50
# File 'lib/replace_class/replace_class.rb', line 20

def self.start

     # option parse
     ReplaceClass::OptParser.parse do |parser, options|

       $options = options

       # check option
       if !options.has_key?(:source) || !options.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.expand_path('../', __FILE__)
       else
           src_root = File.expand_path(ARGV.last)
       end

       checkDir(src_root)
     end
end