New Group class

This commit is contained in:
Rémy Coutable 2011-09-23 00:20:35 +02:00
parent 4c1cf825e5
commit ed97336c7d
3 changed files with 54 additions and 5 deletions

View File

@ -6,6 +6,7 @@ module Guard
autoload :UI, 'guard/ui' autoload :UI, 'guard/ui'
autoload :Dsl, 'guard/dsl' autoload :Dsl, 'guard/dsl'
autoload :DslDescriber, 'guard/dsl_describer' autoload :DslDescriber, 'guard/dsl_describer'
autoload :Group, 'guard/group'
autoload :Interactor, 'guard/interactor' autoload :Interactor, 'guard/interactor'
autoload :Listener, 'guard/listener' autoload :Listener, 'guard/listener'
autoload :Watcher, 'guard/watcher' autoload :Watcher, 'guard/watcher'
@ -28,7 +29,7 @@ module Guard
def setup(options = {}) def setup(options = {})
@options = options @options = options
@guards = [] @guards = []
@groups = [{ :name => :default, :options => {} }] @groups = [Group.new(:default)]
@interactor = Interactor.new @interactor = Interactor.new
@listener = Listener.select_and_init(@options[:watchdir] ? File.expand_path(@options[:watchdir]) : Dir.pwd) @listener = Listener.select_and_init(@options[:watchdir] ? File.expand_path(@options[:watchdir]) : Dir.pwd)
@ -140,9 +141,9 @@ module Guard
# @param [Array] files the list of files to pass to the task # @param [Array] files the list of files to pass to the task
# #
def execute_supervised_task_for_all_guards(task, files = nil) def execute_supervised_task_for_all_guards(task, files = nil)
groups.each do |group_hash|
catch group_hash[:options][:halt_on_fail] == true ? :task_has_failed : :no_catch do
guards.find_all { |guard| guard.group == group_hash[:name] }.each do |guard| guards.find_all { |guard| guard.group == group_hash[:name] }.each do |guard|
groups.each do |group|
catch group.options[:halt_on_fail] == true ? :task_has_failed : :no_catch do
if task == :run_on_change if task == :run_on_change
paths = Watcher.match_files(guard, files) paths = Watcher.match_files(guard, files)
UI.debug "#{guard.class.name}##{task} with #{paths.inspect}" UI.debug "#{guard.class.name}##{task} with #{paths.inspect}"
@ -200,10 +201,16 @@ module Guard
# #
# @param [String] name the group name # @param [String] name the group name
# @param [Hash] options the group options # @param [Hash] options the group options
# @option options [Boolean] halt_on_fail if a task execution should be halted for all Guards in this group if one Guard throws `:task_has_failed` # @option options [Boolean] halt_on_fail if a task execution
# should be halted for all Guards in this group if one Guard throws `:task_has_failed`
# #
def add_group(name, options = {}) def add_group(name, options = {})
@groups << { :name => name.to_sym, :options => options } unless name.nil? || @groups.find { |group| group[:name] == name } group = groups(name)
if group.nil?
group = Group.new(name, options)
@groups << group
end
group
end end
# Tries to load the Guard main class. # Tries to load the Guard main class.

23
lib/guard/group.rb Normal file
View File

@ -0,0 +1,23 @@
module Guard
# A group of Guards.
#
class Group
attr_accessor :name, :options
# Initialize a Group.
#
# @param [String] name the name of the group
# @param [Hash] options the group options
# @option options [Boolean] halt_on_fail if a task execution
# should be halted for all Guards in this group if one Guard throws `:task_has_failed`
#
def initialize(name, options = {})
@name = name.to_sym
@options = options
end
end
end

19
spec/guard/group_spec.rb Normal file
View File

@ -0,0 +1,19 @@
require 'spec_helper'
describe Guard::Group do
describe ".initialize" do
it "accepts a name as a string and provides an accessor for it (returning a symbol)" do
described_class.new('foo').name.should eql :foo
end
it "accepts a name as a symbol and provides an accessor for it (returning a symbol)" do
described_class.new(:foo).name.should eql :foo
end
it "accepts options and provides an accessor for it" do
described_class.new('foo', :halt_on_fail => true).options.should == { :halt_on_fail => true }
end
end
end