imperator-ext 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -89,7 +89,7 @@ require 'imperator/command/macros'
89
89
 
90
90
  imperator_command_factory.use do |factory|
91
91
  # put common/shared logic in this REST base class
92
- factory.default_rest_class = Imperator::Mongoid::Command::Rest
92
+ factory.set_default_rest_class Imperator::Mongoid::Command::Rest, :mongoid
93
93
 
94
94
  factory.create_rest :all, Post do
95
95
  on_error do
@@ -122,17 +122,30 @@ If you include the macros, the following macros are exposed:
122
122
  * `#create_rest_command`
123
123
  * `#imperator_class_factory
124
124
 
125
+ The CommandFactory can be used standalone, by subclassing or even by instance as the example below illustrates! (see specs for details)
126
+
125
127
  Usage example:
126
128
 
127
129
  ```ruby
128
- # ensure all attributes of model are reflected in Command (if adaptor makes it possible)
129
- Imperator::Command::ClassFactory.default_options = {auto_attributes: true}
130
+ class AutoCommandFactory < Imperator::Command::ClassFactory
131
+ def initialize
132
+ # if Model adaptor makes it possible
133
+ # - ensure all attributes of model are reflected in Command
134
+ @default_options = {auto_attributes: true}
135
+ end
136
+ end
130
137
 
131
138
  module PostController
132
139
  class Update < Action
133
140
 
134
141
  command do
135
- @command ||= create_rest_command(:update, Post).new object_hash
142
+ @command ||= command_factory.create_rest_command(:update, Post).new object_hash
143
+ end
144
+
145
+ protected
146
+
147
+ def command_factory
148
+ AutoCommandFactory.instance
136
149
  end
137
150
  end
138
151
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "imperator-ext"
8
- s.version = "0.2.1"
8
+ s.version = "0.2.2"
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"]
@@ -4,17 +4,57 @@ require 'imperator/command/rest'
4
4
  class Imperator::Command
5
5
  # http://johnragan.wordpress.com/2010/02/18/ruby-metaprogramming-dynamically-defining-classes-and-methods/
6
6
  class ClassFactory
7
- class << self
7
+ include Singleton
8
+
9
+ module ClassMethods
8
10
  def use &block
9
11
  yield self
10
12
  end
13
+ end
14
+
15
+ module Methods
16
+ attr_writer :default_class, :initial_rest_classes, :initial_rest_class
17
+ attr_accessor :default_rest_classes
18
+
19
+ def initial_rest_classes
20
+ @initial_rest_classes ||= {:mongoid => Imperator::Mongoid::Command::Rest}
21
+ end
22
+
23
+ def default_class
24
+ @default_class ||= ::Imperator::Command
25
+ end
26
+
27
+ def default_rest_class model = nil
28
+ return initial_rest_class unless model
29
+ return default_rest_classes[:mongoid] if model.ancestors.include?(Mongoid::Document)
30
+ end
31
+
32
+ def initial_rest_class
33
+ @initial_rest_class ||= Imperator::Command::Rest
34
+ end
11
35
 
12
- def default_parent clazz
13
- @default_parent ||= clazz
36
+ def set_default_rest_class model, type = nil
37
+ unless type.nil?
38
+ @default_rest_classes[type.to_sym] = model
39
+ return
40
+ end
41
+ @initial_rest_class = model
14
42
  end
15
43
 
16
- def get_default_parent
17
- @default_parent ||= ::Imperator::Command
44
+ def reset_rest!
45
+ @default_rest_classes = initial_rest_classes
46
+ @initial_rest_class = Imperator::Command::Rest
47
+ end
48
+
49
+ def reset!
50
+ reset_rest!
51
+ default_class = ::Imperator::Command
52
+ end
53
+
54
+ attr_writer :default_options
55
+
56
+ def default_options
57
+ @default_options ||= {}
18
58
  end
19
59
 
20
60
  # Usage:
@@ -23,10 +63,11 @@ class Imperator::Command
23
63
  # end
24
64
  def build_command action, model, options = {}, &block
25
65
  clazz_name = "#{action.to_s.camelize}#{model.to_s.camelize}Command"
26
- parent = options[:parent] || get_default_parent
66
+ parent = options[:parent] || default_class
27
67
  clazz = parent ? Class.new(parent) : Class.new
28
68
  Object.const_set clazz_name, clazz
29
- clazz = self.const_get(clazz_name)
69
+ context = self.kind_of?(Class) ? self : self.class
70
+ clazz = context.const_get(clazz_name)
30
71
  if options[:auto_attributes]
31
72
  clazz.instance_eval do
32
73
  if respond_to? :attributes_for
@@ -46,7 +87,7 @@ class Imperator::Command
46
87
  # end
47
88
  def rest_command action, model, options = {}, &block
48
89
  options.reverse_merge! default_options
49
- options[:parent] ||= get_default_rest_class(model)
90
+ options[:parent] ||= default_rest_class(model)
50
91
  rest_commands_for(model, options, &block) and return if action.to_sym == :all
51
92
  if rest_actions.include? action.to_sym
52
93
  action_name = "#{action}_command_for"
@@ -56,30 +97,6 @@ class Imperator::Command
56
97
  end
57
98
  end
58
99
 
59
- attr_writer :default_rest_class
60
-
61
- def get_default_rest_class model
62
- model.ancestors.include?(Mongoid::Document) ? default_mongoid_rest_class : default_rest_class
63
- end
64
-
65
- def default_rest_class
66
- @default_rest_class ||= Imperator::Command::Rest
67
- end
68
-
69
- def default_mongoid_rest_class
70
- @default_mongoid_rest_class ||= Imperator::Mongoid::Command::Rest
71
- end
72
-
73
- def reset_rest_class
74
- @default_rest_class = Imperator::Command::Rest
75
- end
76
-
77
- attr_writer :default_options
78
-
79
- def default_options
80
- @default_options ||= {}
81
- end
82
-
83
100
 
84
101
  protected
85
102
 
@@ -117,5 +134,10 @@ class Imperator::Command
117
134
  end
118
135
  end
119
136
  end
137
+
138
+ extend Methods
139
+ extend ClassMethods
140
+
141
+ include Methods
120
142
  end
121
143
  end
@@ -25,13 +25,16 @@ end
25
25
  class Payment
26
26
  end
27
27
 
28
+ class Property
29
+ end
30
+
28
31
  describe Imperator::Command::ClassFactory do
29
32
  subject { Imperator::Command::ClassFactory }
30
33
 
31
34
  describe '.rest_command' do
32
35
  context 'update' do
33
36
  before :all do
34
- subject.default_rest_class = Imperator::Command::Rest
37
+ subject.set_default_rest_class Imperator::Command::Rest
35
38
  subject.rest_command :update, Article, :parent => UpdateRestCommand, :auto_attributes => true do
36
39
  def hello
37
40
  "hello"
@@ -49,7 +52,7 @@ describe Imperator::Command::ClassFactory do
49
52
 
50
53
  context 'create' do
51
54
  before :all do
52
- subject.default_rest_class = Imperator::Command::Rest
55
+ subject.set_default_rest_class Imperator::Command::Rest
53
56
  subject.rest_command :create, Article, :parent => CreateRestCommand, :auto_attributes => true do
54
57
  def hello
55
58
  "hello"
@@ -67,7 +70,7 @@ describe Imperator::Command::ClassFactory do
67
70
 
68
71
  context 'delete' do
69
72
  before :all do
70
- subject.default_rest_class = Imperator::Command::Rest
73
+ subject.set_default_rest_class Imperator::Command::Rest
71
74
  subject.rest_command :delete, Article, :parent => DeleteRestCommand, :auto_attributes => true do
72
75
  def hello
73
76
  "hello"
@@ -94,7 +97,7 @@ describe Imperator::Command::ClassFactory do
94
97
  subject.build_command :show, Post
95
98
  end
96
99
 
97
- its(:get_default_parent) { should == Imperator::Command }
100
+ its(:default_class) { should == Imperator::Command }
98
101
  specify { ShowPostCommand.superclass.should == Imperator::Command }
99
102
  end
100
103
 
@@ -116,14 +119,14 @@ describe Imperator::Command::ClassFactory do
116
119
  end
117
120
  end
118
121
 
119
- describe '.default_rest_class' do
122
+ describe '.set_default_rest_class' do
120
123
  describe 'default class' do
121
124
  its(:default_rest_class) { should == Imperator::Command::Rest }
122
125
  end
123
126
 
124
- describe 'set default_rest_class to UpdateRestCommand' do
127
+ describe 'set_default_rest_class to UpdateRestCommand' do
125
128
  before :all do
126
- subject.default_rest_class = UpdateRestCommand
129
+ subject.set_default_rest_class UpdateRestCommand
127
130
  end
128
131
 
129
132
  its(:default_rest_class) { should == UpdateRestCommand }
@@ -146,7 +149,7 @@ describe 'private methods' do
146
149
  describe 'rest command builders' do
147
150
  describe '.create_command_for' do
148
151
  before do
149
- subject.reset_rest_class
152
+ subject.reset_rest!
150
153
  subject.send :create_command_for, Post
151
154
  end
152
155
 
@@ -155,7 +158,7 @@ describe 'private methods' do
155
158
 
156
159
  describe '.update_command_for' do
157
160
  before do
158
- subject.default_rest_class = UpdateRestCommand
161
+ subject.set_default_rest_class UpdateRestCommand
159
162
  subject.send :update_command_for, Account
160
163
  end
161
164
 
@@ -164,7 +167,7 @@ describe 'private methods' do
164
167
 
165
168
  describe '.delete_command_for' do
166
169
  before do
167
- subject.reset_rest_class
170
+ subject.reset!
168
171
  subject.send :delete_command_for, Account
169
172
  end
170
173
 
@@ -184,3 +187,52 @@ describe 'private methods' do
184
187
  end
185
188
  end
186
189
 
190
+ class MySubFactory < Imperator::Command::ClassFactory
191
+ def self.initial_rest_class
192
+ @initial_rest_class ||= UpdateRestCommand
193
+ end
194
+
195
+ def initialize
196
+ @default_options = {:hello => 'you'}
197
+ end
198
+ end
199
+
200
+ describe 'subclass' do
201
+ describe MySubFactory do
202
+ subject { MySubFactory }
203
+
204
+ describe 'new initial_rest_class' do
205
+ its(:initial_rest_class) { should == UpdateRestCommand }
206
+ end
207
+ end
208
+ end
209
+
210
+ describe 'instance' do
211
+ describe MySubFactory do
212
+ subject { MySubFactory.instance }
213
+
214
+ describe 'new default_options' do
215
+ its(:default_options) {should == {:hello => 'you'} }
216
+ end
217
+
218
+ describe '.rest_command' do
219
+ context 'update' do
220
+ before :all do
221
+ subject.set_default_rest_class Imperator::Command::Rest
222
+ subject.rest_command :update, Property, :parent => UpdateRestCommand, :auto_attributes => true do
223
+ def hello
224
+ "hello"
225
+ end
226
+ end
227
+ end
228
+
229
+ context 'UpdatePropertyCommand created' do
230
+ let(:command) { UpdatePropertyCommand.new }
231
+
232
+ specify { UpdatePropertyCommand.superclass.should == UpdateRestCommand }
233
+ specify { command.hello.should == "hello" }
234
+ end
235
+ end
236
+ end
237
+ end
238
+ end
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.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -179,7 +179,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
179
179
  version: '0'
180
180
  segments:
181
181
  - 0
182
- hash: 4197596095852566251
182
+ hash: 3676978282257002910
183
183
  required_rubygems_version: !ruby/object:Gem::Requirement
184
184
  none: false
185
185
  requirements: