Class: FileEditor

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ FileEditor

Returns a new instance of FileEditor.



5
6
7
8
9
# File 'lib/file_editor.rb', line 5

def initialize(file_name)
  @file_name = file_name
  @keep_backup = false
  @global = true #this will yield the equivalent of gsub instead of sub

end

Instance Attribute Details

#file_nameObject

Returns the value of attribute file_name.



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

def file_name
  @file_name
end

#globalObject

Returns the value of attribute global.



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

def global
  @global
end

#keep_backupObject

Returns the value of attribute keep_backup.



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

def keep_backup
  @keep_backup
end

#regexObject

Returns the value of attribute regex.



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

def regex
  @regex
end

#substitution_stringObject

Returns the value of attribute substitution_string.



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

def substitution_string
  @substitution_string
end

Class Method Details

.edit(file_name, &b) ⇒ Object

You can send an explicit receiver as an argument to this block and set variables on it (useful inside another object where you need to access its own methods inside the block), or simply set variables without an explicit receiver. Either way, the block must call the run method. See Ruby Best Practices by Gregory Brown, p. 63-64, for elaboration of this approach



16
17
18
19
# File 'lib/file_editor.rb', line 16

def self.edit(file_name, &b)
  editor = FileEditor.new(file_name)
  b.arity == 0 ? editor.instance_eval(&b) : b.call(editor)
end

Instance Method Details

#get_random_extensionObject

Returns a random 6-letter extension (e.g., ‘kjeihl’); this ensures that we don’t inadvertantly delete a pre-existing backup file if we’re not saving the backup



79
80
81
# File 'lib/file_editor.rb', line 79

def get_random_extension
  ('a'..'z').to_a.shuffle.slice(0..5).join
end

#is_multiline(regex_string) ⇒ Object

Determines if a string derived from regexp.to_s represents a regular expression with the multiline option turned on



72
73
74
# File 'lib/file_editor.rb', line 72

def is_multiline(regex_string)
  regex_string.match(/\?.*?m.*?-/) || regex_string.match(/\?[^-]+:/)
end

#runObject

Runs file editor



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/file_editor.rb', line 22

def run
  raise "You need to provide a regular expression to the editor" unless @regex
  raise "You need to provide a substitution string" unless @substitution_string
  raise "The file #{@file_name} does not exist" unless File.exist?(@file_name)
  raise "The path #{@file_name} does not point to a file" unless File.file?(@file_name)
  raise "You do not have permission to read #{@file_name}" unless File.readable?(@file_name)
  raise "You do not have permission to edit #{@file_name}" unless File.writable?(@file_name)
  begin
    if @keep_backup
      temp_file_name = @file_name + ".backup"
      File.delete(temp_file_name) if File.exist?(temp_file_name)
    else
      temp_file_name = @file_name + "." + get_random_extension
    end
    File.rename(@file_name, temp_file_name)
    read_handle = File.new(temp_file_name, "r")
    write_handle = File.new(@file_name, "w")
    write_to_file(read_handle, write_handle)
  ensure
    read_handle.close if read_handle
    write_handle.close if write_handle
    File.delete(temp_file_name) unless @keep_backup
  end
end

#write_to_file(read_handle, write_handle) ⇒ Object

Testable write method, using io handles as arguments to enable the use of String.IO in testing



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/file_editor.rb', line 49

def write_to_file(read_handle, write_handle)
  if is_multiline(@regex.to_s) 
    file_string = read_handle.read #no sense doing a line-by-line multiline

    if (@global)
      write_handle.puts(file_string.gsub(@regex, @substitution_string))
    else
      write_handle.puts(file_string.sub(@regex, @substitution_string))
    end
  else
    if (@global)
      read_handle.each do |line|
        write_handle.puts (line.gsub(@regex, @substitution_string))
      end
    else
      read_handle.each do |line|
        write_handle.puts (line.sub(@regex, @substitution_string))
      end    
    end
  end
end