Class: Tabs2spaces::Converter

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

Instance Method Summary collapse

Constructor Details

#initialize(folder, options) ⇒ Converter

Returns a new instance of Converter.



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

def initialize(folder, options)
    @folder = folder || "./"
    @options = options
end

Instance Method Details

#convertObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tabs2spaces.rb', line 33

def convert
  unless File.directory?(@folder)
    new_file = file_expand @folder
    file_write @folder, new_file
  else
    folder = File.join(@folder, '**', @options[:pattern])
    Dir.glob(folder).each do |file|
      new_file = file_expand file
      file_write file, new_file
    end
  end
end

#file_expand(file) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/tabs2spaces.rb', line 11

def file_expand(file)
  unless @options[:inversed]
    puts "Expanding tabs on: " + file
    return `expand -i -t #{@options[:number]} #{file}` 
  else
    puts "Unexpanding spaces on: " + file
    return `unexpand --first-only -t #{@options[:number]} #{file}`
  end
end

#file_write(file, data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tabs2spaces.rb', line 21

def file_write(file, data)
  begin
    new_file = File.open(file, "w") do |f|
      f.puts data
    end
  rescue IOError => e
    puts e
  ensure
    new_file.close unless new_file == nil
  end
end