Skip to content

Commit

Permalink
Create watcher class #20
Browse files Browse the repository at this point in the history
  • Loading branch information
julianrubisch committed Sep 8, 2019
1 parent d5bd184 commit d3f2c70
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/attractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'attractor/calculator'
require 'attractor/reporter'
require 'attractor/suggester'
require 'attractor/watcher'

module Attractor
class Error < StandardError; end
Expand Down
10 changes: 0 additions & 10 deletions lib/attractor/calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,5 @@ def calculate
Value.new(file_path: change[:file_path], churn: change[:times_changed], complexity: complexity)
end
end

def self.watch(file_prefix: '')
listener = Listen.to(file_prefix) do |_modified, _added, _removed|
puts "modified #{_modified}"
puts "added #{_added}"
puts "removed #{_removed}"
end
listener.start
sleep
end
end
end
4 changes: 1 addition & 3 deletions lib/attractor/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ def calc
option :file_prefix, aliases: :p
option :watch, aliases: :w, type: :boolean
def report
puts 'Generating an HTML report'
if options[:watch]
puts 'Listening for file changes...'
Attractor::Calculator.watch(file_prefix: options[:file_prefix])
Attractor::HtmlReporter.new(file_prefix: options[:file_prefix]).watch
else
puts "Generated HTML report at #{File.expand_path './attractor_output/index.html'}"
case options[:format]
when 'html'
Attractor::HtmlReporter.new(file_prefix: options[:file_prefix]).report
Expand Down
8 changes: 8 additions & 0 deletions lib/attractor/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
module Attractor
# base reporter
class Reporter
extend Forwardable
attr_accessor :values
def_delegator :@watcher, :watch

def initialize(file_prefix: '')
@calculator = Calculator.new(file_prefix: file_prefix)
@values = @calculator.calculate
@suggester = Suggester.new(values)

@watcher = Watcher.new(file_prefix, lambda do
report
end)
end

def report
Expand All @@ -37,12 +43,14 @@ class HtmlReporter < Reporter
def report
super

puts 'Generating an HTML report'
template = Tilt.new(File.expand_path('../templates/index.html.erb', __dir__))
output = template.render self

FileUtils.mkdir_p './attractor_output'

File.open('./attractor_output/index.html', 'w') { |file| file.write(output) }
puts "Generated HTML report at #{File.expand_path './attractor_output/index.html'}"
end
end
end
24 changes: 24 additions & 0 deletions lib/attractor/watcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Attractor
# functionality for watching file system changes
class Watcher
def initialize(file_prefix, callback)
@file_prefix = file_prefix
@callback = callback
end

def watch
@callback.call

listener = Listen.to(@file_prefix, ignore: /^attractor_output/) do |modified, _added, _removed|
if modified
puts "#{modified.map(&:to_s).join(', ')} modified, recalculating..."
@callback.call
end
end
listener.start
sleep
end
end
end

0 comments on commit d3f2c70

Please sign in to comment.