Skip to content
Browse files

Add `scope` DSL method.

Allow the Guard scope to be defined from the
`Guardfile` with the `scope` DSL method
  • Loading branch information...
1 parent ad699c5 commit aaf779d221daf77809a25c436b6c788d67882f5b @netzpirat netzpirat committed Dec 20, 2012
Showing with 135 additions and 11 deletions.
  1. +2 −1 CHANGELOG.md
  2. +7 −5 Guardfile
  3. +23 −0 README.md
  4. +5 −5 lib/guard.rb
  5. +28 −0 lib/guard/dsl.rb
  6. +70 −0 spec/guard/dsl_spec.rb
View
3 CHANGELOG.md
@@ -2,6 +2,7 @@
### New features
+- Allow the Guard scope to be defined from the `Guardfile` with the `scope` DSL method. ([@netzpirat][])
- [#378][] Scope plugins and groups from CLI and interactor. ([@netzpirat][])
- [#369][] Allow Guard plugins to specify their template location. ([@schmurfy][])
- [#364][] Add `ignore!` and `filter!` DSL methods. ([@tarsolya][])
@@ -775,4 +776,4 @@ The Listen integration has been supervised by [@thibaudgg][] and executed by [@M
[@waldo]: https://github.com/waldo
[@wereHamster]: https://github.com/wereHamster
[@yannlugrin]: https://github.com/yannlugrin
-[@zonque]: https://github.com/zonque
+[@zonque]: https://github.com/zonque
View
12 Guardfile
@@ -1,3 +1,5 @@
+scope :group => :specs
+
group :specs do
guard :rspec, :cli => '--fail-fast --format doc' do
watch(%r{^spec/.+_spec\.rb$})
@@ -6,8 +8,8 @@ group :specs do
end
end
-#group :docs do
-# guard :ronn do
-# watch(%r{^man/.+\.ronn?$})
-# end
-#end
+group :docs do
+ guard :ronn do
+ watch(%r{^man/.+\.ronn?$})
+ end
+end
View
23 README.md
@@ -646,6 +646,29 @@ $ guard -g specs
Guard plugins that don't belong to a group are considered global and are always run.
+### scope
+
+The `scope` method allows you to define the default plugin or group scope for Guard, if not
+specified as command line option. Thus command line group and plugin scope takes precedence over
+the DSL scope configuration.
+
+You can define either a single plugin or group:
+
+```ruby
+scope :plugin => :rspec
+scope :group => :docs
+```
+
+or specify multiple plugins or groups.
+
+```ruby
+scope :plugins => [:test, :jasmine]
+scope :groups => [:docs, :frontend]
+```
+
+If you define both the plugin and group scope, the plugin scope has precedence. If you use both the
+plural and the singular option, the plural has precedence.
+
### notification
If you don't specify any notification configuration in your `Guardfile`, Guard goes through the list of available
View
10 lib/guard.rb
@@ -46,7 +46,7 @@ class << self
def setup(options = {})
@running = true
@lock = Mutex.new
- @options = options
+ @options = options.dup
@watchdir = (options[:watchdir] && File.expand_path(options[:watchdir])) || Dir.pwd
@runner = ::Guard::Runner.new
@scope = { :plugins => [], :groups => []}
@@ -68,12 +68,12 @@ def setup(options = {})
::Guard::Dsl.evaluate_guardfile(options)
::Guard::UI.error 'No guards found in Guardfile, please add at least one.' if @guards.empty?
- if options[:group]
- @scope[:groups] = options[:group].map { |g| ::Guard.groups(g) }
+ if @options[:group]
+ @scope[:groups] = @options[:group].map { |g| ::Guard.groups(g) }
end
- if options[:plugin]
- @scope[:plugins] = options[:plugin].map { |p| ::Guard.guards(p) }
+ if @options[:plugin]
+ @scope[:plugins] = @options[:plugin].map { |p| ::Guard.guards(p) }
end
runner.deprecation_warning if @options[:show_deprecations]
View
28 lib/guard/dsl.rb
@@ -553,5 +553,33 @@ def logger(options)
::Guard::UI.options = ::Guard::UI.options.merge options
end
+ # Sets the default scope on startup
+ #
+ # @example Scope Guard to a single group
+ # scope :group => :frontend
+ #
+ # @example Scope Guard to multiple groups
+ # scope :groups => [:specs, :docs]
+ #
+ # @example Scope Guard to a single plugin
+ # scope :plugin => :test
+ #
+ # @example Scope Guard to multiple plugins
+ # scope :plugins => [:jasmine, :rspec]
+ #
+ # @param [Hash] scopes the scope for the groups and plugins
+ #
+ def scope(scopes = {})
+ if ::Guard.options[:plugin].empty?
+ ::Guard.options[:plugin] = [scopes[:plugin]] if scopes[:plugin]
+ ::Guard.options[:plugin] = scopes[:plugins] if scopes[:plugins]
+ end
+
+ if ::Guard.options[:group].empty?
+ ::Guard.options[:group] = [scopes[:group]] if scopes[:group]
+ ::Guard.options[:group] = scopes[:groups] if scopes[:groups]
+ end
+ end
+
end
end
View
70 spec/guard/dsl_spec.rb
@@ -593,6 +593,76 @@ def self.call(guard_class, event, args)
end
end
+ describe '#scope' do
+ context 'with an existing command line plugin scope' do
+ before do
+ ::Guard.options[:plugin] = ['rspec']
+ ::Guard.options[:group] = []
+ end
+
+ it 'does not use the DSL scope plugin' do
+ described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugin => :baz')
+ ::Guard.options[:plugin].should eql(['rspec'])
+ end
+
+ it 'does not use the DSL scope plugins' do
+ described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugins => [:foo, :bar]')
+ ::Guard.options[:plugin].should eql(['rspec'])
+ end
+ end
+
+ context 'without an existing command line plugin scope' do
+ before do
+ ::Guard.options[:plugin] = []
+ ::Guard.options[:group] = []
+ end
+
+ it 'does use the DSL scope plugin' do
+ described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugin => :baz')
+ ::Guard.options[:plugin].should eql([:baz])
+ end
+
+ it 'does use the DSL scope plugins' do
+ described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugins => [:foo, :bar]')
+ ::Guard.options[:plugin].should eql([:foo, :bar])
+ end
+ end
+
+ context 'with an existing command line group scope' do
+ before do
+ ::Guard.options[:plugin] = []
+ ::Guard.options[:group] = ['frontend']
+ end
+
+ it 'does not use the DSL scope plugin' do
+ described_class.evaluate_guardfile(:guardfile_contents => 'scope :group => :baz')
+ ::Guard.options[:group].should eql(['frontend'])
+ end
+
+ it 'does not use the DSL scope plugins' do
+ described_class.evaluate_guardfile(:guardfile_contents => 'scope :groups => [:foo, :bar]')
+ ::Guard.options[:group].should eql(['frontend'])
+ end
+ end
+
+ context 'without an existing command line group scope' do
+ before do
+ ::Guard.options[:plugin] = []
+ ::Guard.options[:group] = []
+ end
+
+ it 'does use the DSL scope group' do
+ described_class.evaluate_guardfile(:guardfile_contents => 'scope :group => :baz')
+ ::Guard.options[:group].should eql([:baz])
+ end
+
+ it 'does use the DSL scope groups' do
+ described_class.evaluate_guardfile(:guardfile_contents => 'scope :groups => [:foo, :bar]')
+ ::Guard.options[:group].should eql([:foo, :bar])
+ end
+ end
+ end
+
private
def fake_guardfile(name, contents)

0 comments on commit aaf779d

Please sign in to comment.
Something went wrong with that request. Please try again.