Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for configurable background processing

also killed a bug that causes ActiveModel::Validations to be a
requirement
  • Loading branch information...
commit ebc848b6248841712198382e587f194675d5f4a9 1 parent 1d9ee6b
@karmajunkie authored
View
7 lib/imperator.rb
@@ -1,7 +1,8 @@
+module Imperator
+end
require "imperator/version"
require 'imperator/invalid_command_error'
+require 'imperator/config'
+require 'imperator/null_background_processor'
require 'imperator/command'
-module Imperator
- # Your code goes here...
-end
View
24 lib/imperator/command.rb
@@ -1,6 +1,7 @@
require 'virtus'
class Imperator::Command
include Virtus
+ extend Imperator::Config
def self.action(&block)
define_method(:action, &block)
@@ -8,6 +9,7 @@ def self.action(&block)
alias_method :params, :attributes
+
def as_json(*args)
attributes.as_json(*args)
end
@@ -17,13 +19,12 @@ def persisted?
end
def commit!
- raise Imperator::InvalidCommandError.new "Command was invalid" unless valid?
+ check_valid_command
self.commit
end
def commit
- #TODO: background code for this
- self.perform
+ self.class.background_processor.commit(self)
end
def initialize(*)
@@ -34,12 +35,20 @@ def self.load(command_string)
self.new(JSON.parse(command_string))
end
+ def self.background_processor
+ @background_processor ||= superclass.background_processor || Imperator::NullBackgroundProcessor
+ end
+
+ def self.background_processor=(backgrounder)
+ @background_processor = backgrounder
+ end
+
def load(command_string)
self.attributes = HashWithIndifferentAccess.new(JSON.parse(command_string))
end
def perform!
- raise InvalidCommandError.new "Command was invalid" unless valid?
+ check_valid_command
self.perform
end
@@ -52,4 +61,11 @@ def perform
action
end
+ private
+ def check_valid_command
+ if self.respond_to?(:valid?)
+ raise InvalidCommandError.new "Command was invalid" unless valid?
+ end
+ end
+
end
View
6 lib/imperator/config.rb
@@ -0,0 +1,6 @@
+module Imperator::Config
+ def configure(&block)
+ raise ArgumentError.new("must supply configuration block") unless block_given?
+ yield self
+ end
+end
View
5 lib/imperator/null_background_processor.rb
@@ -0,0 +1,5 @@
+class Imperator::NullBackgroundProcessor
+ def self.commit(command)
+ command.perform
+ end
+end
View
10 lib/imperator/test_background_processor.rb
@@ -0,0 +1,10 @@
+class Imperator::TestBackgroundProcessor
+ @commits = []
+ class << self
+ attr_accessor :commits
+ end
+
+ def self.commit(command)
+ @commits << command
+ end
+end
View
48 spec/imperator/command_spec.rb
@@ -1,5 +1,6 @@
require 'imperator'
require 'rspec/mocks'
+require 'imperator/test_background_processor'
describe Imperator::Command do
describe "actions" do
@@ -30,6 +31,20 @@ def action
end
end
+ describe "performing" do
+ it "bang version doesn't raise exception if validations not enabled" do
+ class PerformBangValidCommand < Imperator::Command
+ attribute :foo, String
+ def action
+ "this is fine"
+ end
+ end
+
+ lambda{PerformBangValidCommand.new.perform!}.should_not raise_exception(Imperator::InvalidCommandError)
+ lambda{PerformBangValidCommand.new.commit!}.should_not raise_exception(Imperator::InvalidCommandError)
+ end
+ end
+
describe "attributes" do
class AttributeCommand < Imperator::Command
attribute :gets_default, String, :default => "foo"
@@ -56,5 +71,38 @@ class AttributeCommand < Imperator::Command
end
end
+ describe "#commit" do
+ before do
+ Imperator::Command.background_processor = Imperator::TestBackgroundProcessor
+ end
+
+ after do
+ Imperator::Command.background_processor = Imperator::NullBackgroundProcessor
+ end
+
+ class TestCommand < Imperator::Command
+ attribute :foo, String
+ def action
+ end
+ end
+
+ context "subclassed commands" do
+ class SubTestCommand < TestCommand
+
+ end
+ it "commits like the parent class" do
+ command = SubTestCommand.new(:foo => "bar")
+ command.commit
+ Imperator::TestBackgroundProcessor.commits.should include(command)
+ end
+ end
+
+ it "sends the command into the configured background processor" do
+ command = TestCommand.new(:foo => "bar")
+ command.commit
+ Imperator::TestBackgroundProcessor.commits.should include(command)
+ end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.