imperator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in commando.gemspec
4
+ gemspec
@@ -0,0 +1,72 @@
1
+ #Imperator
2
+ 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.
3
+
4
+ ###Why use commands?
5
+ 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.
6
+
7
+ 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.
8
+
9
+ Commands can also be regarded as the contexts from DCI.
10
+
11
+ ###Validation
12
+ 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.
13
+
14
+ `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`.
15
+
16
+ Commands can also be used on forms in place of ActiveRecord models.
17
+
18
+ ###TODO
19
+ * test coverage—Imperator was extracted out of some other work, and coverage was a part of those test suites.
20
+ * Ensure compatibility with DJ, Resque and Sidekiq
21
+
22
+ #Using Imperator
23
+
24
+ ##Requirements:
25
+ * ActiveSupport 3.0 or higher
26
+ * ActiveAttr
27
+
28
+ ##Installation
29
+ In your Gemfile:
30
+
31
+ gem 'imperator'
32
+
33
+ ##Usage
34
+
35
+ ###Creating the command:
36
+
37
+ class DoSomethingCommand < Imperator::Command
38
+ attribute :some_object_id
39
+ attribute :some_value
40
+
41
+ validates_presence_of :some_object_id
42
+
43
+ action do
44
+ obj = SomeObject.find(self.some_object_id)
45
+ obj.do_something(self.some_value)
46
+ end
47
+ end
48
+ end
49
+
50
+ ###Using a command on a form builder
51
+ <%= form_for(@command, :as => :do_something, :url => some_resource_path(@command.some_object_id), :method => :put) do |f| %>
52
+ ...
53
+ <% end %>
54
+
55
+ ###Using a command
56
+ class SomeController < ApplicationController
57
+ def update
58
+ command = DoSomethingCommand.new(params[:do_something])
59
+ if command.valid?
60
+ command.perform
61
+ redirect_to root_url
62
+ else
63
+ render edit_some_resource_path(command.some_object_id)
64
+ end
65
+ end
66
+ end
67
+
68
+ ###Using a command in the background (Delayed::Job)
69
+ Delayed::Job.enqueue command
70
+
71
+
72
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "imperator/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "imperator"
7
+ s.version = Imperator::VERSION
8
+ s.authors = ["Keith Gaddis"]
9
+ s.email = ["keith.gaddis@gmail.com"]
10
+ s.homepage = "http://github.com/karmajunkie/imperator"
11
+ s.summary = %q{Imperator supports the command pattern}
12
+ 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.}
13
+
14
+ #s.rubyforge_project = "imperator"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "pry"
24
+ # s.add_runtime_dependency "rest-client"
25
+ s.add_runtime_dependency "uuidtools"
26
+ s.add_runtime_dependency "active_attr"
27
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_attr'
2
+ require "imperator/version"
3
+ require 'imperator/invalid_command_error'
4
+ require 'imperator/command'
5
+ require 'pry'
6
+
7
+ module Imperator
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,86 @@
1
+ require 'uuidtools'
2
+ class Imperator::Command
3
+ include ActiveAttr::Model
4
+ extend ActiveModel::Callbacks
5
+
6
+ define_model_callbacks :create, :perform, :initialize
7
+
8
+ cattr_accessor :commit_mode
9
+ attribute :id
10
+
11
+ after_initialize :set_uuid
12
+
13
+ class << self
14
+ attr_accessor :perform_block
15
+ end
16
+
17
+ def self.action(&block)
18
+ @perform_block = block
19
+ end
20
+
21
+ alias_method :params, :attributes
22
+
23
+ def as_json
24
+ attributes.as_json
25
+ end
26
+
27
+ def persisted?
28
+ false
29
+ end
30
+
31
+ def commit!
32
+ raise Imperator::InvalidCommandError.new "Command was invalid" unless valid?
33
+ self.commit
34
+ end
35
+
36
+ def commit
37
+ #TODO: background code for this
38
+ self.perform
39
+ end
40
+
41
+ def initialize(*)
42
+ run_callbacks :initialize do
43
+ super
44
+ end
45
+ end
46
+
47
+ def dump
48
+ attributes.to_json
49
+ end
50
+
51
+ def self.load(command_string)
52
+ self.new(JSON.parse(command_string))
53
+ end
54
+
55
+ def load(command_string)
56
+ self.attributes = HashWithIndifferentAccess.new(JSON.parse(command_string))
57
+ end
58
+
59
+ def perform!
60
+ raise InvalidCommandError.new "Command was invalid" unless valid?
61
+ self.perform
62
+ end
63
+
64
+ def perform
65
+ raise "You need to define the perform block for #{self.class.name}" unless self.class.perform_block
66
+ run_callbacks :perform do
67
+ self.instance_exec(&self.class.perform_block)
68
+ end
69
+ end
70
+
71
+ def method_missing(method, *args)
72
+ method_root = method.to_s.gsub(/=$/, "")
73
+ if method.to_s[/=$/]
74
+ self.attributes[method_root] = args.first
75
+ elsif attributes.has_key?(method_root)
76
+ self.attributes[method]
77
+ else
78
+ super
79
+ end
80
+ end
81
+
82
+ private
83
+ def set_uuid
84
+ self.id = UUIDTools::UUID.timestamp_create.to_s if self.id.nil?
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ class Imperator::InvalidCommandError < ArgumentError
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ module Imperator
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,45 @@
1
+ require 'imperator'
2
+ describe Imperator::Command do
3
+
4
+ describe "#perform" do
5
+ class CommandTestException < Exception; end
6
+ class TestCommand < Imperator::Command
7
+ action do
8
+ raise CommandTestException.new
9
+ end
10
+ end
11
+
12
+ let(:command){TestCommand.new}
13
+ it "runs the action block when #perform is called" do
14
+ lambda{command.perform}.should raise_exception(CommandTestException)
15
+ end
16
+ end
17
+
18
+ describe "attributes" do
19
+ class AttributeCommand < Imperator::Command
20
+ attribute :gets_default, :default => "foo"
21
+ attribute :declared_attr
22
+ end
23
+
24
+ it "throws away undeclared attributes in mass assignment" do
25
+ command = AttributeCommand.new(:undeclared_attr => "foo")
26
+ lambda{command.undeclared_attr}.should raise_exception(NoMethodError)
27
+ end
28
+
29
+ it "accepts declared attributes in mass assignment" do
30
+ command = AttributeCommand.new(:declared_attr => "bar")
31
+ command.declared_attr.should == "bar"
32
+ end
33
+
34
+ it "allows default values to be used on commands" do
35
+ command = AttributeCommand.new
36
+ command.gets_default.should == "foo"
37
+ end
38
+ it "overrides default when supplied in constructor args" do
39
+ command = AttributeCommand.new :gets_default => "bar"
40
+ command.gets_default.should == "bar"
41
+ end
42
+ end
43
+
44
+ end
45
+
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imperator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Keith Gaddis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: pry
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: uuidtools
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: active_attr
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Imperator is a small gem to help with command objects. The command pattern
79
+ is a design pattern used to encapsulate all of the information needed to execute
80
+ a method or process at a point in time. In a web application, commands are typically
81
+ used to delay execution of a method from the request cycle to a background processor.
82
+ email:
83
+ - keith.gaddis@gmail.com
84
+ executables: []
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - .gitignore
89
+ - Gemfile
90
+ - README.md
91
+ - Rakefile
92
+ - imperator.gemspec
93
+ - lib/imperator.rb
94
+ - lib/imperator/command.rb
95
+ - lib/imperator/invalid_command_error.rb
96
+ - lib/imperator/version.rb
97
+ - spec/imperator/command_spec.rb
98
+ homepage: http://github.com/karmajunkie/imperator
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.24
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Imperator supports the command pattern
122
+ test_files:
123
+ - spec/imperator/command_spec.rb