Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow background options to be passed to the background processor

  • Loading branch information...
commit e018e3330b5a8f667e22c33e66aa53ffe8397c38 1 parent ebc848b
@karmajunkie authored
View
24 README.md
@@ -84,6 +84,30 @@ Then you can use them on a form just as you would a model:
###Using a command in the background (Delayed::Job)
Delayed::Job.enqueue command
+###Using a background processor
+You can create a custom background processor very easily. It merely
+needs to implement the class method `#commit`
+
+ class Imperator::NullBackgroundProcessor
+ def self.commit(command, options = nil)
+ command.perform
+ end
+ end
+
+You can also pass options to the background processor either by the
+command or instance
+
+ class CompletePurchase < Imperator::Command
+ background :queue => "high"
+
+ def action
+ end
+ end
+
+ command = CompletePurchase.new
+ command.commit(:queue => "low")
+
+
###Contributors
Many thanks to the following contributors for bugfixes, testing, and
additional functionality
View
14 lib/imperator/command.rb
@@ -23,8 +23,8 @@ def commit!
self.commit
end
- def commit
- self.class.background_processor.commit(self)
+ def commit(options = nil)
+ self.class.background_processor.commit(self, options || self.class.background_options)
end
def initialize(*)
@@ -32,7 +32,15 @@ def initialize(*)
end
def self.load(command_string)
- self.new(JSON.parse(command_string))
+ self.new(JSON.parse(command_string))
+ end
+
+ def self.background(options = {})
+ @background_options = options
+ end
+
+ def self.background_options
+ @background_options
end
def self.background_processor
View
2  lib/imperator/null_background_processor.rb
@@ -1,5 +1,5 @@
class Imperator::NullBackgroundProcessor
- def self.commit(command)
+ def self.commit(command, options = nil)
command.perform
end
end
View
2  lib/imperator/test_background_processor.rb
@@ -4,7 +4,7 @@ class << self
attr_accessor :commits
end
- def self.commit(command)
+ def self.commit(command, options = nil)
@commits << command
end
end
View
21 spec/imperator/command_spec.rb
@@ -1,5 +1,5 @@
-require 'imperator'
require 'rspec/mocks'
+require 'imperator'
require 'imperator/test_background_processor'
describe Imperator::Command do
@@ -86,15 +86,30 @@ def action
end
end
- context "subclassed commands" do
+ context "in subclassed commands" do
class SubTestCommand < TestCommand
-
+ background :any_option => :foo
end
it "commits like the parent class" do
command = SubTestCommand.new(:foo => "bar")
command.commit
Imperator::TestBackgroundProcessor.commits.should include(command)
end
+
+ context "with background options" do
+ it "receives background options" do
+ command = SubTestCommand.new(:foo => "bar")
+ Imperator::TestBackgroundProcessor.should_receive(:commit).with(command, :any_option => :foo)
+ command.commit
+ end
+
+ it "receives options supplied on the command instance" do
+ command = SubTestCommand.new(:foo => "bar")
+ Imperator::TestBackgroundProcessor.should_receive(:commit).with(command, :any_option => :bar)
+ command.commit(:any_option => :bar)
+
+ end
+ end
end
it "sends the command into the configured background processor" do
Please sign in to comment.
Something went wrong with that request. Please try again.