Class: VanishingVision::Engine

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

Constant Summary collapse

SKIP_ENCODINGS =
[NKF::BINARY, NKF::UNKNOWN]

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Engine

Returns a new instance of Engine.



9
10
11
# File 'lib/vanishing_vision/engine.rb', line 9

def initialize(path)
  @path = path
end

Instance Method Details

#run(auto_correct) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vanishing_vision/engine.rb', line 13

def run(auto_correct)
  source = File.read(@path)

  return if SKIP_ENCODINGS.include?(NKF.guess(source))

  core = Core.new(source)

  changed_source = core.vanish

  unless source == changed_source
    puts "diff #{@path}"
    puts Diffy::Diff.new(source, changed_source)

    File.open(@path, 'w+') {|f| f.write(changed_source) } if auto_correct
  end
end