Skip to content

Commit

Permalink
Add some tests and change the name of the gem
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith Gaddis committed May 8, 2012
1 parent 50665fe commit 4e82f4b
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 46 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
25 changes: 0 additions & 25 deletions commando.gemspec

This file was deleted.

27 changes: 27 additions & 0 deletions imperator.gemspec
Original file line number Diff line number Diff line change
@@ -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
8 changes: 0 additions & 8 deletions lib/commando.rb

This file was deleted.

3 changes: 0 additions & 3 deletions lib/commando/invalid_command_error.rb

This file was deleted.

9 changes: 9 additions & 0 deletions lib/imperator.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions lib/commando/command.rb → lib/imperator/command.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Commando::Command
require 'uuidtools'
class Imperator::Command
include ActiveAttr::Model
extend ActiveModel::Callbacks

Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions lib/imperator/invalid_command_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Imperator::InvalidCommandError < ArgumentError

end
2 changes: 1 addition & 1 deletion lib/commando/version.rb → lib/imperator/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Commando
module Imperator
VERSION = "0.1.0"
end
45 changes: 45 additions & 0 deletions spec/imperator/command_spec.rb
Original file line number Diff line number Diff line change
@@ -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.