Skip to content

Commit

Permalink
refactor watcher and matching
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed Oct 22, 2014
1 parent bcbf95f commit 8af30d5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 46 deletions.
19 changes: 10 additions & 9 deletions lib/guard/setuper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ def _debug_command_execution
end
end

# TODO: Guard::Watch or Guard::Scope should provide this
def _scoped_watchers
watchers = []
runner.send(:_scoped_plugins) { |guard| watchers += guard.watchers }
watchers
end

# Check if any of the changes are actually watched for
#
# NOTE: this is called from the listen thread - be careful to not
Expand All @@ -237,15 +244,9 @@ def _debug_command_execution
# TODO: move this to watcher class?
#
def _relevant_changes?(changes)
# TODO: make a Guardfile reloader "plugin" instead of a special case
return true if ::Guard::Watcher.match_guardfile?(changes[:modified])

# TODO: ignoring irrelevant files should be Listen's responsibility
all_files = changes.values.flatten(1)
runner.send(:_scoped_plugins) do |guard|
return true if ::Guard::Watcher.match_files?([guard], all_files)
end
false
files = changes.values.flatten(1)
watchers = _scoped_watchers
watchers.any? { |watcher| files.any? { |file| watcher.match(file) } }
end

def _relative_pathname(path)
Expand Down
16 changes: 1 addition & 15 deletions lib/guard/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,6 @@ def self.match_files(guard, files)
end
end

# Tests if a file would be matched by any of the Guard plugin watchers.
#
# @param [Array<Guard::Plugin>] plugins the Guard plugins to use the
# watchers from
# @param [Array<String>] files the files to test
# @return [Boolean] Whether a file matches
#
def self.match_files?(plugins, files)
plugins.any? do |plugin|
plugin.watchers.any? do |watcher|
files.any? { |file| watcher.match(file) }
end
end
end

# Tests if any of the files is the Guardfile.
#
# @param [Array<String>] files the files to test
Expand All @@ -109,6 +94,7 @@ def self.match_guardfile?(files)
# if the pattern is a string)
#
def match(string_or_pathname)
# TODO: use only match() - and show fnmatch example
file = string_or_pathname.to_s
return (file == @pattern ? [file] : nil) unless @pattern.is_a?(Regexp)
return unless (m = @pattern.match(file))
Expand Down
22 changes: 0 additions & 22 deletions spec/lib/guard/watcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -417,28 +417,6 @@

@plugins = [@guard1, @guard2]
end

context "with a watcher that matches a file" do
specify do
result = described_class.match_files?(
@plugins,
["lib/my_wonderful_lib.rb", "guard_rocks_spec.rb"]
)

expect(result).to be_truthy
end
end

context "with no watcher that matches a file" do
specify do
result = described_class.match_files?(
@plugins,
["lib/my_wonderful_lib.rb"]
)

expect(result).to be_falsey
end
end
end

describe ".match" do
Expand Down

0 comments on commit 8af30d5

Please sign in to comment.