Class: NamespaceEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/NamespaceEditor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_namespace, old_namespace, src_path, file_extensions, list = false, overwrite = true) ⇒ NamespaceEditor

Returns a new instance of NamespaceEditor.



6
7
8
9
10
11
12
13
14
15
# File 'lib/NamespaceEditor.rb', line 6

def initialize(new_namespace, old_namespace, src_path, file_extensions, list = false, overwrite = true)
  @new_namespace = new_namespace
  @old_namespace = old_namespace
  @src_path = src_path
  @extensions = file_extensions
  @list = list
  @overwrite = overwrite

  @changed_files_count = 0
end

Instance Attribute Details

#changed_files_countObject (readonly)

Returns the value of attribute changed_files_count.



4
5
6
# File 'lib/NamespaceEditor.rb', line 4

def changed_files_count
  @changed_files_count
end

#extensionsObject

Returns the value of attribute extensions.



3
4
5
# File 'lib/NamespaceEditor.rb', line 3

def extensions
  @extensions
end

#new_namespaceObject

Returns the value of attribute new_namespace.



3
4
5
# File 'lib/NamespaceEditor.rb', line 3

def new_namespace
  @new_namespace
end

#old_namespaceObject

Returns the value of attribute old_namespace.



3
4
5
# File 'lib/NamespaceEditor.rb', line 3

def old_namespace
  @old_namespace
end

#src_pathObject

Returns the value of attribute src_path.



3
4
5
# File 'lib/NamespaceEditor.rb', line 3

def src_path
  @src_path
end

Instance Method Details

#do_replace_namespace(file) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/NamespaceEditor.rb', line 49

def do_replace_namespace file
  src = read_file file
  
  src_after_replacing_namespace = src.gsub(@old_namespace, @new_namespace)

  unless src_after_replacing_namespace === src
    if @overwrite
      begin
        output = File.new(file, "w")
        output.write src_after_replacing_namespace
        output.close
      rescue
        puts
        STDERR.puts "ERROR: There was a problem writing to '#{file}"
        exit 1
      end
    end
    
    if @list
      puts File.absolute_path(file)
    else
      print "."
    end
    @changed_files_count += 1
  end
  @changed_files_count
end

#read_file(file_path) ⇒ Object



17
18
19
20
21
# File 'lib/NamespaceEditor.rb', line 17

def read_file file_path
  File.open file_path, "r"  do | file |
    file.read
  end
end

#replace_namespaceObject



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/NamespaceEditor.rb', line 23

def replace_namespace
  @changed_files_count = 0
  @extensions.each do | extension |
    unless @list
      puts "\tprocessing #{extension} files in #{@src_path}"
      print "\t"
    end
    Dir.glob(@src_path + "/**/*.#{extension}") do |file|  #find src files in current folder and all subfolders

      do_replace_namespace file
    end
    unless @list
      puts
    end
  end
  unless @list
    puts
  end
  unless @list
    if @overwrite
      puts "There were #{@changed_files_count} changes made"
    else
      puts "#{@changed_files_count} file(s) were found. Use the --overwrite flag to make changes to the files"
    end
  end
end