Class: PryToggle::Service

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

Instance Method Summary collapse

Instance Method Details

#executeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pry_toggle/service.rb', line 6

def execute
  File.open(tmp_file_path, 'w') do |d|
    File.open(abs_file_path, 'r') do |o|
      if on
        o.each.with_index(1) do |line, i|
          d << str if i == line_num
          d << line
          # d << str if line =~ %r|def( +)#{mth_name}|
        end
      else
        o.each.with_index(1) do |line, i|
          d << line unless (i == line_num || line_num.nil?) && line == str
        end
      end
    end
  end

  FileUtils.mv(tmp_file_path, abs_file_path)
rescue Errno::ENOENT
  puts "No such file - #{origin_path}"
ensure
  FileUtils.rm_f(tmp_file_path)
end