imperator-ext 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -3
- data/VERSION +1 -1
- data/imperator-ext.gemspec +2 -2
- data/lib/imperator/command/method_factory.rb +10 -2
- data/spec/imperator-ext/command/method_factory_spec.rb +30 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -139,14 +139,27 @@ end
|
|
139
139
|
```
|
140
140
|
## Command Method Factory
|
141
141
|
|
142
|
-
By including the `Imperator::Command::MethodFactory` you get access to the `#command_method`
|
143
|
-
|
142
|
+
By including the `Imperator::Command::MethodFactory` you get access to the `#command_method`and `#command_methods` macros, which let you easily define Imperator Command methods.
|
143
|
+
These macros are typically used in a Controller like in the example below.
|
144
|
+
Note: The options passed in will be used to initialize the command
|
144
145
|
|
145
146
|
```ruby
|
146
147
|
class ServicesController
|
147
148
|
include Imperator::Command::MethodFactory
|
148
149
|
|
149
|
-
command_method :
|
150
|
+
command_method :pay, service: 'paypal'
|
151
|
+
|
152
|
+
command_method (:ship_product, service: 'fedex') do
|
153
|
+
{id: get_id } # late evaled option args (Hash) for command constructor!
|
154
|
+
end
|
155
|
+
|
156
|
+
command_methods :sign_in, :sign_out
|
157
|
+
|
158
|
+
protected
|
159
|
+
|
160
|
+
def get_id
|
161
|
+
params[:id]
|
162
|
+
end
|
150
163
|
end
|
151
164
|
```
|
152
165
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/imperator-ext.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "imperator-ext"
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kristian Mandrup"]
|
12
|
-
s.date = "2012-08-
|
12
|
+
s.date = "2012-08-15"
|
13
13
|
s.description = "Factories, Macros, REST helpers and Mongoid integration"
|
14
14
|
s.email = "kmandrup@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -1,17 +1,25 @@
|
|
1
1
|
class Imperator::Command
|
2
2
|
module MethodFactory
|
3
|
-
def command_method command, options = {}
|
3
|
+
def command_method command, options = {}, &block
|
4
4
|
namespace = (options[:ns] || '').to_s
|
5
5
|
namespace.sub! /Controller$/, ''
|
6
6
|
define_method "#{command}_command" do
|
7
7
|
instance_var = "@#{command}_command"
|
8
8
|
unless instance_variable_get(instance_var)
|
9
9
|
clazz = [namespace, "#{command.to_s.camelize}Command"].join('::').constantize
|
10
|
-
|
10
|
+
opts = options.merge(initiator: self)
|
11
|
+
opts.merge!(instance_eval &block) if block_given?
|
12
|
+
clazz_inst = clazz.new(opts)
|
13
|
+
instance_variable_set instance_var, clazz_inst
|
11
14
|
end
|
12
15
|
end
|
13
16
|
end
|
14
17
|
|
18
|
+
def command_methods *args, &block
|
19
|
+
options = args.extract_options!
|
20
|
+
args.flatten.each { |meth| command_method meth, options, &block }
|
21
|
+
end
|
22
|
+
|
15
23
|
extend self
|
16
24
|
end
|
17
25
|
end
|
@@ -7,6 +7,9 @@ end
|
|
7
7
|
class UpdateCommand < Imperator::Command
|
8
8
|
end
|
9
9
|
|
10
|
+
class ShowCommand < Imperator::Command
|
11
|
+
end
|
12
|
+
|
10
13
|
module Landlord
|
11
14
|
module Account
|
12
15
|
class PayCommand < Imperator::Command
|
@@ -22,7 +25,13 @@ module Landlord
|
|
22
25
|
class Pay < Action # imperator action
|
23
26
|
extend Imperator::Command::MethodFactory
|
24
27
|
|
25
|
-
command_method :pay, object: 'hello', ns: self.parent
|
28
|
+
command_method :pay, object: 'hello', ns: self.parent do
|
29
|
+
{id: id}
|
30
|
+
end
|
31
|
+
|
32
|
+
def id
|
33
|
+
'my id'
|
34
|
+
end
|
26
35
|
end
|
27
36
|
end
|
28
37
|
end
|
@@ -48,6 +57,26 @@ describe Imperator::Command::MethodFactory do
|
|
48
57
|
its(:update_command) { should be_a(Imperator::Command) }
|
49
58
|
end
|
50
59
|
|
60
|
+
describe '.command_methods' do
|
61
|
+
describe 'with name args' do
|
62
|
+
before do
|
63
|
+
clazz.command_methods :update, :show
|
64
|
+
end
|
65
|
+
|
66
|
+
its(:update_command) { should be_a(Imperator::Command) }
|
67
|
+
its(:show_command) { should be_a(Imperator::Command) }
|
68
|
+
end
|
69
|
+
|
70
|
+
describe 'with names and options' do
|
71
|
+
before do
|
72
|
+
clazz.command_methods :update, :show, id: 7
|
73
|
+
end
|
74
|
+
|
75
|
+
its(:update_command) { should be_a(Imperator::Command) }
|
76
|
+
its(:show_command) { should be_a(Imperator::Command) }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
51
80
|
describe 'with namespace :ns option' do
|
52
81
|
before do
|
53
82
|
clazz.command_method :pay, object: 'hello', ns: Landlord::Account
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: imperator-ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: virtus
|
@@ -179,7 +179,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
179
179
|
version: '0'
|
180
180
|
segments:
|
181
181
|
- 0
|
182
|
-
hash:
|
182
|
+
hash: 4197596095852566251
|
183
183
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
184
184
|
none: false
|
185
185
|
requirements:
|