imperator 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in commando.gemspec
4
4
  gemspec
5
+
6
+ gem "pry", :group => [:test, :development]
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
 
@@ -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 "active_attr"
25
+ s.add_runtime_dependency "activemodel"
26
+ s.add_runtime_dependency "virtus"
27
27
  end
@@ -1,8 +1,6 @@
1
- require 'active_attr'
2
1
  require "imperator/version"
3
2
  require 'imperator/invalid_command_error'
4
3
  require 'imperator/command'
5
- require 'pry'
6
4
 
7
5
  module Imperator
8
6
  # Your code goes here...
@@ -1,27 +1,31 @@
1
1
  require 'uuidtools'
2
+ require 'active_model'
3
+ require 'virtus'
2
4
  class Imperator::Command
3
- include ActiveAttr::Model
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
- cattr_accessor :commit_mode
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
- @perform_block = block
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
- 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
68
+ # @abstract
69
+ def action
70
+ raise NoMethodError.new("Please define #action for #{self.class.name}")
69
71
  end
70
72
 
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?
73
+ def perform
74
+ run_callbacks(:perform) { action }
85
75
  end
86
76
  end
@@ -1,3 +1,3 @@
1
1
  module Imperator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -3,22 +3,46 @@ describe Imperator::Command do
3
3
 
4
4
  describe "#perform" do
5
5
  class CommandTestException < Exception; end
6
- class TestCommand < Imperator::Command
7
- action do
8
- raise CommandTestException.new
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
- let(:command){TestCommand.new}
13
- it "runs the action block when #perform is called" do
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.1.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-05-08 00:00:00.000000000 Z
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: !ruby/object:Gem::Requirement
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: pry
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: :development
34
+ type: :runtime
39
35
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
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: uuidtools
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: !ruby/object:Gem::Requirement
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: active_attr
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: !ruby/object:Gem::Requirement
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.8.24
100
+ rubygems_version: 1.6.2
119
101
  signing_key:
120
102
  specification_version: 3
121
103
  summary: Imperator supports the command pattern