imperator 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +2 -0
- data/README.md +7 -0
- data/imperator.gemspec +2 -2
- data/lib/imperator.rb +0 -2
- data/lib/imperator/command.rb +21 -31
- data/lib/imperator/version.rb +1 -1
- data/spec/imperator/command_spec.rb +39 -8
- metadata +17 -35
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -68,5 +68,12 @@ Commands can also be used on forms in place of ActiveRecord models.
|
|
68
68
|
###Using a command in the background (Delayed::Job)
|
69
69
|
Delayed::Job.enqueue command
|
70
70
|
|
71
|
+
###Contributors
|
72
|
+
Many thanks to the following contributors for bugfixes, testing, and
|
73
|
+
additional functionality
|
74
|
+
|
75
|
+
* Jason Staten (@statianzo)
|
76
|
+
* Jay Adkisson (@jayferd)
|
77
|
+
* Nick Maloney (@nmaloney)
|
71
78
|
|
72
79
|
|
data/imperator.gemspec
CHANGED
@@ -20,8 +20,8 @@ Gem::Specification.new do |s|
|
|
20
20
|
|
21
21
|
# specify any dependencies here; for example:
|
22
22
|
s.add_development_dependency "rspec"
|
23
|
-
s.add_development_dependency "pry"
|
24
23
|
# s.add_runtime_dependency "rest-client"
|
25
24
|
s.add_runtime_dependency "uuidtools"
|
26
|
-
s.add_runtime_dependency "
|
25
|
+
s.add_runtime_dependency "activemodel"
|
26
|
+
s.add_runtime_dependency "virtus"
|
27
27
|
end
|
data/lib/imperator.rb
CHANGED
data/lib/imperator/command.rb
CHANGED
@@ -1,27 +1,31 @@
|
|
1
1
|
require 'uuidtools'
|
2
|
+
require 'active_model'
|
3
|
+
require 'virtus'
|
2
4
|
class Imperator::Command
|
3
|
-
include
|
5
|
+
include ActiveModel::Validations
|
4
6
|
extend ActiveModel::Callbacks
|
7
|
+
include Virtus
|
8
|
+
|
9
|
+
if defined? ActiveModel::Serializable
|
10
|
+
include ActiveModel::Serializable::JSON
|
11
|
+
include ActiveModel::Serializable::XML
|
12
|
+
else
|
13
|
+
include ActiveModel::Serializers::JSON
|
14
|
+
include ActiveModel::Serializers::Xml
|
15
|
+
end
|
5
16
|
|
6
17
|
define_model_callbacks :create, :perform, :initialize
|
7
18
|
|
8
|
-
|
9
|
-
attribute :id
|
10
|
-
|
11
|
-
after_initialize :set_uuid
|
12
|
-
|
13
|
-
class << self
|
14
|
-
attr_accessor :perform_block
|
15
|
-
end
|
19
|
+
attribute :id, String, :default => proc { UUIDTools::UUID.timestamp_create.to_s }
|
16
20
|
|
17
21
|
def self.action(&block)
|
18
|
-
|
22
|
+
define_method(:action, &block)
|
19
23
|
end
|
20
24
|
|
21
25
|
alias_method :params, :attributes
|
22
26
|
|
23
|
-
def as_json
|
24
|
-
attributes.as_json
|
27
|
+
def as_json(*args)
|
28
|
+
attributes.as_json(*args)
|
25
29
|
end
|
26
30
|
|
27
31
|
def persisted?
|
@@ -61,26 +65,12 @@ class Imperator::Command
|
|
61
65
|
self.perform
|
62
66
|
end
|
63
67
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
self.instance_exec(&self.class.perform_block)
|
68
|
-
end
|
68
|
+
# @abstract
|
69
|
+
def action
|
70
|
+
raise NoMethodError.new("Please define #action for #{self.class.name}")
|
69
71
|
end
|
70
72
|
|
71
|
-
def
|
72
|
-
|
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?
|
73
|
+
def perform
|
74
|
+
run_callbacks(:perform) { action }
|
85
75
|
end
|
86
76
|
end
|
data/lib/imperator/version.rb
CHANGED
@@ -3,22 +3,46 @@ describe Imperator::Command do
|
|
3
3
|
|
4
4
|
describe "#perform" do
|
5
5
|
class CommandTestException < Exception; end
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
context "using DSL " do
|
7
|
+
class DSLTestCommand < Imperator::Command
|
8
|
+
action do
|
9
|
+
raise CommandTestException.new
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:command){DSLTestCommand.new}
|
14
|
+
it "runs the action block when #perform is called" do
|
15
|
+
lambda{command.perform}.should raise_exception(CommandTestException)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "using method definition" do
|
20
|
+
class MethodTestCommand < Imperator::Command
|
21
|
+
def action
|
22
|
+
raise CommandTestException.new
|
23
|
+
end
|
24
|
+
end
|
25
|
+
let(:command){MethodTestCommand.new}
|
26
|
+
it "runs the action method when #perform is called" do
|
27
|
+
lambda{command.perform}.should raise_exception(CommandTestException)
|
9
28
|
end
|
10
29
|
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "actions" do
|
33
|
+
context "using DSL" do
|
34
|
+
class ActionDSLExampleCommand < Imperator::Command
|
35
|
+
action do
|
11
36
|
|
12
|
-
|
13
|
-
|
14
|
-
lambda{command.perform}.should raise_exception(CommandTestException)
|
37
|
+
end
|
38
|
+
end
|
15
39
|
end
|
16
40
|
end
|
17
41
|
|
18
42
|
describe "attributes" do
|
19
43
|
class AttributeCommand < Imperator::Command
|
20
|
-
attribute :gets_default, :default => "foo"
|
21
|
-
attribute :declared_attr
|
44
|
+
attribute :gets_default, String, :default => "foo"
|
45
|
+
attribute :declared_attr, String
|
22
46
|
end
|
23
47
|
|
24
48
|
it "throws away undeclared attributes in mass assignment" do
|
@@ -39,6 +63,13 @@ describe Imperator::Command do
|
|
39
63
|
command = AttributeCommand.new :gets_default => "bar"
|
40
64
|
command.gets_default.should == "bar"
|
41
65
|
end
|
66
|
+
|
67
|
+
it "will create attributes as json" do
|
68
|
+
command = AttributeCommand.new
|
69
|
+
command.as_json.should == {"id" => command.id,
|
70
|
+
"gets_default" => "foo",
|
71
|
+
"declared_attr" => nil}
|
72
|
+
end
|
42
73
|
end
|
43
74
|
|
44
75
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: imperator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,12 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-11 00:00:00.000000000 -05:00
|
13
|
+
default_executable:
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: rspec
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirement: &70180076248340 !ruby/object:Gem::Requirement
|
17
18
|
none: false
|
18
19
|
requirements:
|
19
20
|
- - ! '>='
|
@@ -21,31 +22,21 @@ dependencies:
|
|
21
22
|
version: '0'
|
22
23
|
type: :development
|
23
24
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
25
|
+
version_requirements: *70180076248340
|
30
26
|
- !ruby/object:Gem::Dependency
|
31
|
-
name:
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
27
|
+
name: uuidtools
|
28
|
+
requirement: &70180076247920 !ruby/object:Gem::Requirement
|
33
29
|
none: false
|
34
30
|
requirements:
|
35
31
|
- - ! '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
|
-
type: :
|
34
|
+
type: :runtime
|
39
35
|
prerelease: false
|
40
|
-
version_requirements:
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ! '>='
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: '0'
|
36
|
+
version_requirements: *70180076247920
|
46
37
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
38
|
+
name: activemodel
|
39
|
+
requirement: &70180076278720 !ruby/object:Gem::Requirement
|
49
40
|
none: false
|
50
41
|
requirements:
|
51
42
|
- - ! '>='
|
@@ -53,15 +44,10 @@ dependencies:
|
|
53
44
|
version: '0'
|
54
45
|
type: :runtime
|
55
46
|
prerelease: false
|
56
|
-
version_requirements:
|
57
|
-
none: false
|
58
|
-
requirements:
|
59
|
-
- - ! '>='
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
47
|
+
version_requirements: *70180076278720
|
62
48
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
64
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
+
name: virtus
|
50
|
+
requirement: &70180076278300 !ruby/object:Gem::Requirement
|
65
51
|
none: false
|
66
52
|
requirements:
|
67
53
|
- - ! '>='
|
@@ -69,12 +55,7 @@ dependencies:
|
|
69
55
|
version: '0'
|
70
56
|
type: :runtime
|
71
57
|
prerelease: false
|
72
|
-
version_requirements:
|
73
|
-
none: false
|
74
|
-
requirements:
|
75
|
-
- - ! '>='
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
version: '0'
|
58
|
+
version_requirements: *70180076278300
|
78
59
|
description: Imperator is a small gem to help with command objects. The command pattern
|
79
60
|
is a design pattern used to encapsulate all of the information needed to execute
|
80
61
|
a method or process at a point in time. In a web application, commands are typically
|
@@ -95,6 +76,7 @@ files:
|
|
95
76
|
- lib/imperator/invalid_command_error.rb
|
96
77
|
- lib/imperator/version.rb
|
97
78
|
- spec/imperator/command_spec.rb
|
79
|
+
has_rdoc: true
|
98
80
|
homepage: http://github.com/karmajunkie/imperator
|
99
81
|
licenses: []
|
100
82
|
post_install_message:
|
@@ -115,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
97
|
version: '0'
|
116
98
|
requirements: []
|
117
99
|
rubyforge_project:
|
118
|
-
rubygems_version: 1.
|
100
|
+
rubygems_version: 1.6.2
|
119
101
|
signing_key:
|
120
102
|
specification_version: 3
|
121
103
|
summary: Imperator supports the command pattern
|