diff --git a/lib/guard.rb b/lib/guard.rb index 5f79470..37653fb 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -6,6 +6,7 @@ module Guard autoload :UI, 'guard/ui' autoload :Dsl, 'guard/dsl' autoload :DslDescriber, 'guard/dsl_describer' + autoload :Group, 'guard/group' autoload :Interactor, 'guard/interactor' autoload :Listener, 'guard/listener' autoload :Watcher, 'guard/watcher' @@ -28,7 +29,7 @@ module Guard def setup(options = {}) @options = options @guards = [] - @groups = [{ :name => :default, :options => {} }] + @groups = [Group.new(:default)] @interactor = Interactor.new @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 # 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| + groups.each do |group| + catch group.options[:halt_on_fail] == true ? :task_has_failed : :no_catch do if task == :run_on_change paths = Watcher.match_files(guard, files) UI.debug "#{guard.class.name}##{task} with #{paths.inspect}" @@ -200,10 +201,16 @@ module Guard # # @param [String] name the group name # @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 = {}) - @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 # Tries to load the Guard main class. diff --git a/lib/guard/group.rb b/lib/guard/group.rb new file mode 100644 index 0000000..a044fbd --- /dev/null +++ b/lib/guard/group.rb @@ -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 diff --git a/spec/guard/group_spec.rb b/spec/guard/group_spec.rb new file mode 100644 index 0000000..3711c0d --- /dev/null +++ b/spec/guard/group_spec.rb @@ -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