Skip to content

Commit

Permalink
fix lots of RuboCop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed Apr 22, 2016
1 parent 2e5c409 commit e21066c
Show file tree
Hide file tree
Showing 36 changed files with 125 additions and 151 deletions.
5 changes: 1 addition & 4 deletions lib/listen.rb
Expand Up @@ -45,10 +45,7 @@ def stop
@listeners ||= []

# TODO: should use a mutex for this
@listeners.each do |listener|
# call stop to halt the main loop
listener.stop
end
@listeners.each(&:stop)
@listeners = nil
end
end
Expand Down
52 changes: 27 additions & 25 deletions lib/listen/adapter.rb
Expand Up @@ -7,37 +7,39 @@

module Listen
module Adapter
OPTIMIZED_ADAPTERS = [Darwin, Linux, BSD, Windows]
OPTIMIZED_ADAPTERS = [Darwin, Linux, BSD, Windows].freeze
POLLING_FALLBACK_MESSAGE = 'Listen will be polling for changes.'\
'Learn more at https://github.com/guard/listen#listen-adapters.'
'Learn more at https://github.com/guard/listen#listen-adapters.'.freeze

def self.select(options = {})
_log :debug, 'Adapter: considering polling ...'
return Polling if options[:force_polling]
_log :debug, 'Adapter: considering optimized backend...'
return _usable_adapter_class if _usable_adapter_class
_log :debug, 'Adapter: falling back to polling...'
_warn_polling_fallback(options)
Polling
rescue
_log :warn, format('Adapter: failed: %s:%s', $ERROR_POSITION.inspect,
$ERROR_POSITION * "\n")
raise
end
class << self
def select(options = {})
_log :debug, 'Adapter: considering polling ...'
return Polling if options[:force_polling]
_log :debug, 'Adapter: considering optimized backend...'
return _usable_adapter_class if _usable_adapter_class
_log :debug, 'Adapter: falling back to polling...'
_warn_polling_fallback(options)
Polling
rescue
_log :warn, format('Adapter: failed: %s:%s', $ERROR_POSITION.inspect,
$ERROR_POSITION * "\n")
raise
end

private
private

def self._usable_adapter_class
OPTIMIZED_ADAPTERS.detect(&:usable?)
end
def _usable_adapter_class
OPTIMIZED_ADAPTERS.detect(&:usable?)
end

def self._warn_polling_fallback(options)
msg = options.fetch(:polling_fallback_message, POLLING_FALLBACK_MESSAGE)
Kernel.warn "[Listen warning]:\n #{msg}" if msg
end
def _warn_polling_fallback(options)
msg = options.fetch(:polling_fallback_message, POLLING_FALLBACK_MESSAGE)
Kernel.warn "[Listen warning]:\n #{msg}" if msg
end

def self._log(type, message)
Listen::Logger.send(type, message)
def _log(type, message)
Listen::Logger.send(type, message)
end
end
end
end
10 changes: 7 additions & 3 deletions lib/listen/adapter/base.rb
Expand Up @@ -8,7 +8,7 @@ class Base
attr_reader :options

# TODO: only used by tests
DEFAULTS = {}
DEFAULTS = {}.freeze

attr_reader :config

Expand Down Expand Up @@ -129,8 +129,12 @@ def _log_exception(msg, caller_stack)
_log(:error, formatted)
end

def self._log(*args, &block)
Listen::Logger.send(*args, &block)
class << self
private

def _log(*args, &block)
Listen::Logger.send(*args, &block)
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/listen/adapter/bsd.rb
Expand Up @@ -16,7 +16,7 @@ class BSD < Base
:rename
# :link, :revoke
]
}
}.freeze

BUNDLER_DECLARE_GEM = <<-EOS.gsub(/^ {6}/, '')
Please add the following to your Gemfile to avoid polling for changes:
Expand All @@ -38,9 +38,9 @@ def self.usable?

private

def _configure(directory, &_callback)
def _configure(directory, &callback)
@worker ||= KQueue::Queue.new
@callback = _callback
@callback = callback
# use Record to make a snapshot of dir, so we
# can detect new files
_find(directory.to_s) { |path| _watch_file(path, @worker) }
Expand Down
4 changes: 2 additions & 2 deletions lib/listen/adapter/darwin.rb
Expand Up @@ -9,7 +9,7 @@ class Darwin < Base
OS_REGEXP = /darwin(?<major_version>1\d+)/i

# The default delay between checking for changes.
DEFAULTS = { latency: 0.1 }
DEFAULTS = { latency: 0.1 }.freeze

INCOMPATIBLE_GEM_VERSION = <<-EOS.gsub(/^ {8}/, '')
rb-fsevent > 0.9.4 no longer supports OS X 10.6 through 10.8.
Expand Down Expand Up @@ -58,7 +58,7 @@ def _run
def _process_event(dir, event)
_log :debug, "fsevent: processing event: #{event.inspect}"
event.each do |path|
new_path = Pathname.new(path.sub(/\/$/, ''))
new_path = Pathname.new(path.sub(%r{\/$}, ''))
_log :debug, "fsevent: #{new_path}"
# TODO: does this preserve symlinks?
rel_path = new_path.relative_path_from(dir).to_s
Expand Down
8 changes: 3 additions & 5 deletions lib/listen/adapter/linux.rb
Expand Up @@ -14,12 +14,12 @@ class Linux < Base
:close_write
],
wait_for_delay: 0.1
}
}.freeze

private

WIKI_URL = 'https://github.com/guard/listen'\
'/wiki/Increasing-the-amount-of-inotify-watchers'
'/wiki/Increasing-the-amount-of-inotify-watchers'.freeze

INOTIFY_LIMIT_MESSAGE = <<-EOS.gsub(/^\s*/, '')
FATAL: Listen error: unable to monitor directories for changes.
Expand Down Expand Up @@ -50,10 +50,8 @@ def _process_event(dir, event)
if /1|true/ =~ ENV['LISTEN_GEM_SIMULATE_FSEVENT']
if (event.flags & [:moved_to, :moved_from]) || _dir_event?(event)
rel_path = path.dirname.relative_path_from(dir).to_s
_queue_change(:dir, dir, rel_path, {})
else
_queue_change(:dir, dir, rel_path, {})
end
_queue_change(:dir, dir, rel_path, {})
return
end

Expand Down
2 changes: 1 addition & 1 deletion lib/listen/adapter/polling.rb
Expand Up @@ -8,7 +8,7 @@ module Adapter
class Polling < Base
OS_REGEXP = // # match every OS

DEFAULTS = { latency: 1.0, wait_for_delay: 0.05 }
DEFAULTS = { latency: 1.0, wait_for_delay: 0.05 }.freeze

private

Expand Down
11 changes: 5 additions & 6 deletions lib/listen/adapter/windows.rb
Expand Up @@ -24,21 +24,21 @@ def self.usable?

private

def _configure(dir, &callback)
def _configure(dir)
require 'wdm'
_log :debug, 'wdm - starting...'
@worker ||= WDM::Monitor.new
@worker.watch_recursively(dir.to_s, :files) do |change|
callback.call([:file, change])
yield([:file, change])
end

@worker.watch_recursively(dir.to_s, :directories) do |change|
callback.call([:dir, change])
yield([:dir, change])
end

events = [:attributes, :last_write]
@worker.watch_recursively(dir.to_s, *events) do |change|
callback.call([:attr, change])
yield([:attr, change])
end
end

Expand Down Expand Up @@ -70,7 +70,6 @@ def _process_event(dir, event)
_queue_change(:dir, dir, Pathname(rel_path).dirname.to_s, {})
elsif change.type == :added
_queue_change(:dir, dir, rel_path, {})
else
# do nothing - changed directory means either:
# - removed subdirs (handled above)
# - added subdirs (handled above)
Expand All @@ -81,7 +80,7 @@ def _process_event(dir, event)
end
rescue
details = event.inspect
_log :error, format('wdm - callback (%): %s:%s', details, $ERROR_INFO,
_log :error, format('wdm - callback (%s): %s:%s', details, $ERROR_INFO,
$ERROR_POSITION * "\n")
raise
end
Expand Down
15 changes: 5 additions & 10 deletions lib/listen/backend.rb
Expand Up @@ -6,6 +6,8 @@
# from exploding with huge test setup blocks
module Listen
class Backend
extend Forwardable

def initialize(directories, queue, silencer, config)
adapter_select_opts = config.adapter_select_options

Expand All @@ -22,17 +24,10 @@ def initialize(directories, queue, silencer, config)
@adapter = adapter_class.new(aconfig)
end

def start
adapter.start
end
delegate start: :adapter
delegate stop: :adapter

def stop
adapter.stop
end

def min_delay_between_events
@min_delay_between_events
end
attr_reader :min_delay_between_events

private

Expand Down
18 changes: 8 additions & 10 deletions lib/listen/change.rb
Expand Up @@ -35,7 +35,7 @@ def invalidate(type, rel_path, options)
cookie = options[:cookie]

if !cookie && config.silenced?(rel_path, type)
Listen::Logger.debug { "(silenced): #{rel_path.inspect}" }
Listen::Logger.debug { "(silenced): #{rel_path.inspect}" }
return
end

Expand All @@ -49,16 +49,14 @@ def invalidate(type, rel_path, options)
if change
options = cookie ? { cookie: cookie } : {}
config.queue(type, change, watched_dir, rel_path, options)
elsif type == :dir
# NOTE: POSSIBLE RECURSION
# TODO: fix - use a queue instead
Directory.scan(self, rel_path, options)
else
if type == :dir
# NOTE: POSSIBLE RECURSION
# TODO: fix - use a queue instead
Directory.scan(self, rel_path, options)
else
change = File.change(record, rel_path)
return if !change || options[:silence]
config.queue(:file, change, watched_dir, rel_path)
end
change = File.change(record, rel_path)
return if !change || options[:silence]
config.queue(:file, change, watched_dir, rel_path)
end
rescue RuntimeError => ex
msg = format(
Expand Down
8 changes: 2 additions & 6 deletions lib/listen/event/config.rb
Expand Up @@ -27,9 +27,7 @@ def timestamp
Time.now.to_f
end

def event_queue
@event_queue
end
attr_reader :event_queue

def callable?
@block
Expand All @@ -39,9 +37,7 @@ def optimize_changes(changes)
@queue_optimizer.smoosh_changes(changes)
end

def min_delay_between_events
@min_delay_between_events
end
attr_reader :min_delay_between_events

def stopped?
listener.state == :stopped
Expand Down
11 changes: 4 additions & 7 deletions lib/listen/event/queue.rb
Expand Up @@ -3,6 +3,8 @@
module Listen
module Event
class Queue
extend Forwardable

class Config
def initialize(relative)
@relative = relative
Expand Down Expand Up @@ -31,13 +33,8 @@ def <<(args)
block.call(args) if block
end

def empty?
event_queue.empty?
end

def pop
event_queue.pop
end
delegate empty?: :event_queue
delegate pop: :event_queue

private

Expand Down
2 changes: 1 addition & 1 deletion lib/listen/file.rb
Expand Up @@ -26,7 +26,7 @@ def self.change(record, rel_path)
end

return if /1|true/ =~ ENV['LISTEN_GEM_DISABLE_HASHING']
return unless self.inaccurate_mac_time?(lstat)
return unless inaccurate_mac_time?(lstat)

# Check if change happened within 1 second (maybe it's even
# too much, e.g. 0.3-0.5 could be sufficient).
Expand Down
3 changes: 2 additions & 1 deletion lib/listen/fsm.rb
Expand Up @@ -111,7 +111,8 @@ class State
attr_reader :name, :transitions

def initialize(name, transitions = nil, &block)
@name, @block = name, block
@name = name
@block = block
@transitions = nil
@transitions = Array(transitions).map(&:to_sym) if transitions
end
Expand Down
10 changes: 3 additions & 7 deletions lib/listen/listener/config.rb
Expand Up @@ -10,7 +10,7 @@ class Config
# Backend selecting options
force_polling: false,
polling_fallback_message: nil
}
}.freeze

def initialize(opts)
@options = DEFAULTS.merge(opts)
Expand All @@ -23,13 +23,9 @@ def relative?
@relative
end

def min_delay_between_events
@min_delay_between_events
end
attr_reader :min_delay_between_events

def silencer_rules
@silencer_rules
end
attr_reader :silencer_rules

def adapter_instance_options(klass)
valid_keys = klass.const_get('DEFAULTS').keys
Expand Down

0 comments on commit e21066c

Please sign in to comment.