Skip to content
Browse files

Add some tests and change the name of the gem

  • Loading branch information...
1 parent 50665fe commit 4e82f4b3080f216652f78234ab8ebf24055808c5 Keith Gaddis committed May 7, 2012
View
16 README.md
@@ -1,23 +1,25 @@
-#Commando
-Commando is a small gem to help with command objects. The [command pattern](http://c2.com/cgi/wiki?CommandPattern) is a design pattern used to encapsulate all of the information needed to execute a method or process at a point in time. In a web application, commands are typically used to delay execution of a method from the request cycle to a background processor.
+#Imperator
+Imperator is a small gem to help with command objects. The [command pattern](http://c2.com/cgi/wiki?CommandPattern) is a design pattern used to encapsulate all of the information needed to execute a method or process at a point in time. In a web application, commands are typically used to delay execution of a method from the request cycle to a background processor.
###Why use commands?
The problem with controllers in Rails is that they're a part of the web domain—their job is to respond to requests, and ONLY to respond to requests. Anything that happens between the receipt of a request and sending a response is Somebody Else's Job™. Commands are that Somebody Else™. Commands are also very commonly utilized to put work into the background.
Why are commands an appropriate place to handle that logic? Commands give you the opportunity to encapsulate all of the logic required for an interaction in one spot. Sometimes that interaction is as simple as a method call—more often there are several method calls involved, not all of which deal with domain logic (and thus, are inappropriate for inclusion on the models). Commands give you a place to bring all of these together in one spot without increasing coupling in your controllers or models.
+Commands can also be regarded as the contexts from DCI.
+
###Validation
Commands also give you an appropriate place to handle interaction validation. Validation is most often regarded as a responsibility of the data model. This is a poor fit, because the idea of what's valid for data is very temporally tied to the understanding of the business domain at the time the data was created. Data that's valid today may well be invalid tomorrow, and when that happens you're going to run into a situation where your ActiveRecord models will refuse to work with old data that is no longer valid. Commands don't absolve you of the need to migrate your data when business requirements change, but they do let you move validation to the interaction where it belongs.
-`Commando::Command`'s are ActiveModel objects, which gives you a lot of flexibility both in and out of Rails projects. Validations are included, as is serialization support. Commando intends to support all major queueing systems in the Rails ecosystem out of the box, including DelayedJob and Resque. The standard validations for ActiveModel are included, though not the ones that ActiveRecord provides such as `#validates_uniqueness_of`.
+`Imperator::Command`'s are ActiveModel objects, which gives you a lot of flexibility both in and out of Rails projects. Validations are included, as is serialization support. Imperator intends to support all major queueing systems in the Rails ecosystem out of the box, including DelayedJob and Resque. The standard validations for ActiveModel are included, though not the ones that ActiveRecord provides such as `#validates_uniqueness_of`.
Commands can also be used on forms in place of ActiveRecord models.
###TODO
-* test coverage—Commando was extracted out of some other work, and coverage was a part of those test suites.
+* test coverage—Imperator was extracted out of some other work, and coverage was a part of those test suites.
* Ensure compatibility with DJ, Resque and Sidekiq
-#Using Commando
+#Using Imperator
##Requirements:
* ActiveSupport 3.0 or higher
@@ -26,13 +28,13 @@ Commands can also be used on forms in place of ActiveRecord models.
##Installation
In your Gemfile:
- gem 'commando'
+ gem 'imperator'
##Usage
###Creating the command:
- class DoSomethingCommand < Commando::Command
+ class DoSomethingCommand < Imperator::Command
attribute :some_object_id
attribute :some_value
View
25 commando.gemspec
@@ -1,25 +0,0 @@
-# -*- encoding: utf-8 -*-
-$:.push File.expand_path("../lib", __FILE__)
-require "commando/version"
-
-Gem::Specification.new do |s|
- s.name = "commando"
- s.version = Commando::VERSION
- s.authors = ["Keith Gaddis"]
- s.email = ["keith.gaddis@gmail.com"]
- s.homepage = "http://github.com/karmajunkie/commando"
- s.summary = %q{Commando supports the command pattern}
- s.description = %q{Commando is a small gem to help with command objects. The command pattern is a design pattern used to encapsulate all of the information needed to execute a method or process at a point in time. In a web application, commands are typically used to delay execution of a method from the request cycle to a background processor.}
-
- #s.rubyforge_project = "commando"
-
- s.files = `git ls-files`.split("\n")
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
- s.require_paths = ["lib"]
-
- # specify any dependencies here; for example:
- # s.add_development_dependency "rspec"
- # s.add_runtime_dependency "rest-client"
- s.add_runtime_dependency "active_attr"
-end
View
27 imperator.gemspec
@@ -0,0 +1,27 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "imperator/version"
+
+Gem::Specification.new do |s|
+ s.name = "imperator"
+ s.version = Imperator::VERSION
+ s.authors = ["Keith Gaddis"]
+ s.email = ["keith.gaddis@gmail.com"]
+ s.homepage = "http://github.com/karmajunkie/imperator"
+ s.summary = %q{Imperator supports the command pattern}
+ s.description = %q{Imperator is a small gem to help with command objects. The command pattern is a design pattern used to encapsulate all of the information needed to execute a method or process at a point in time. In a web application, commands are typically used to delay execution of a method from the request cycle to a background processor.}
+
+ #s.rubyforge_project = "imperator"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ # specify any dependencies here; for example:
+ s.add_development_dependency "rspec"
+ s.add_development_dependency "pry"
+ # s.add_runtime_dependency "rest-client"
+ s.add_runtime_dependency "uuidtools"
+ s.add_runtime_dependency "active_attr"
+end
View
8 lib/commando.rb
@@ -1,8 +0,0 @@
-require 'active_attr'
-require "commando/version"
-require 'commando/invalid_command_error'
-require 'commando/command'
-
-module Commando
- # Your code goes here...
-end
View
3 lib/commando/invalid_command_error.rb
@@ -1,3 +0,0 @@
-class InvalidCommandError < ArgumentError
-
-end
View
9 lib/imperator.rb
@@ -0,0 +1,9 @@
+require 'active_attr'
+require "imperator/version"
+require 'imperator/invalid_command_error'
+require 'imperator/command'
+require 'pry'
+
+module Imperator
+ # Your code goes here...
+end
View
5 lib/commando/command.rb → lib/imperator/command.rb
@@ -1,4 +1,5 @@
-class Commando::Command
+require 'uuidtools'
+class Imperator::Command
include ActiveAttr::Model
extend ActiveModel::Callbacks
@@ -28,7 +29,7 @@ def persisted?
end
def commit!
- raise Commando::InvalidCommandError.new "Command was invalid" unless valid?
+ raise Imperator::InvalidCommandError.new "Command was invalid" unless valid?
self.commit
end
View
3 lib/imperator/invalid_command_error.rb
@@ -0,0 +1,3 @@
+class Imperator::InvalidCommandError < ArgumentError
+
+end
View
2 lib/commando/version.rb → lib/imperator/version.rb
@@ -1,3 +1,3 @@
-module Commando
+module Imperator
VERSION = "0.1.0"
end
View
45 spec/imperator/command_spec.rb
@@ -0,0 +1,45 @@
+require 'imperator'
+describe Imperator::Command do
+
+ describe "#perform" do
+ class CommandTestException < Exception; end
+ class TestCommand < Imperator::Command
+ action do
+ raise CommandTestException.new
+ end
+ end
+
+ let(:command){TestCommand.new}
+ it "runs the action block when #perform is called" do
+ lambda{command.perform}.should raise_exception(CommandTestException)
+ end
+ end
+
+ describe "attributes" do
+ class AttributeCommand < Imperator::Command
+ attribute :gets_default, :default => "foo"
+ attribute :declared_attr
+ end
+
+ it "throws away undeclared attributes in mass assignment" do
+ command = AttributeCommand.new(:undeclared_attr => "foo")
+ lambda{command.undeclared_attr}.should raise_exception(NoMethodError)
+ end
+
+ it "accepts declared attributes in mass assignment" do
+ command = AttributeCommand.new(:declared_attr => "bar")
+ command.declared_attr.should == "bar"
+ end
+
+ it "allows default values to be used on commands" do
+ command = AttributeCommand.new
+ command.gets_default.should == "foo"
+ end
+ it "overrides default when supplied in constructor args" do
+ command = AttributeCommand.new :gets_default => "bar"
+ command.gets_default.should == "bar"
+ end
+ end
+
+end
+

0 comments on commit 4e82f4b

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