imperator-ext 0.2.2 → 0.2.3

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/README.md CHANGED
@@ -43,7 +43,7 @@ class UpdatePostCommand < Imperator::Command::Rest
43
43
 
44
44
  validates_presence_of :some_object_id
45
45
 
46
- update do
46
+ update_action do
47
47
  puts "updated OK"
48
48
  end
49
49
  end
@@ -60,7 +60,7 @@ class UpdatePostCommand < Imperator::Mongoid::Command::Rest
60
60
 
61
61
  validates :name, presence: true
62
62
 
63
- update do
63
+ update_action do
64
64
  puts "#{object} was updated"
65
65
  end
66
66
 
@@ -226,13 +226,13 @@ This is the recommended pattern for linking Focused Controller actions to Impera
226
226
 
227
227
  ## Rest Commands
228
228
 
229
- The class `Imperator::Command::Rest` can be used as a base class for REST CUD commands (Create, Update, Delete). This class includes the module `Imperator::Command::RestHelper` which includes a number of useful methods for defining typical REST action behavior for an Imperator Command.
229
+ The class `Imperator::Command::Rest` can be used as a base class for REST CUD commands (Create, Update, Delete). This class includes the module `Imperator::Command::RestHelper` which includes a number of useful methods for defining typical REST action behavior for an Imperator Command. You can also use the `#rest_action name` macro, as demonstrated here.
230
230
 
231
231
  Usage example:
232
232
 
233
233
  ```ruby
234
234
  class UpdatePostCommand < Imperator::Command::Rest
235
- update_action do
235
+ rest_action :update do
236
236
  notify :updated
237
237
  end
238
238
  end
@@ -348,6 +348,47 @@ module PostController
348
348
  end
349
349
  ```
350
350
 
351
+ ## Namespacing convenience
352
+
353
+ You can also place your commands directly under the Commands namespace.
354
+ In this case you can do the following:
355
+
356
+ ``ruby
357
+ module Commands
358
+ class SignInCommand < Command # or MongoidCommand
359
+ # ...
360
+ end
361
+ end
362
+ ```
363
+
364
+ ## Rails generators
365
+
366
+ A simple `imperator:command` generator is included:
367
+
368
+ `$ rails g imperator:command sign_in`
369
+
370
+ * Will create an `app/commands` folder with your commands (if not present)
371
+ * Will create a `SignInCommand` class inheriting from `::Imperator::Command
372
+ * class will be put in `app/commands/sign_in_command.rb`
373
+
374
+ `$ rails g imperator:command sign_in --orm mongoid`
375
+
376
+ Namespacing:
377
+
378
+ `$ rails g imperator:command tenant::session::sign_in`
379
+
380
+ * Will create a `Tenant::Session::SignInCommand` class in `app/commands/tenant/session/sign_in_command.rb`
381
+
382
+ In this case you have to define/open the Tenant::Session namespace structure elsewhere.
383
+ Fx: `app/commands/tenant/session.rb`
384
+
385
+ ```ruby
386
+ class Tenant < User
387
+ module Session
388
+ end
389
+ end
390
+ ```
391
+
351
392
  ## Contributing to imperator-ext
352
393
 
353
394
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -111,7 +111,7 @@ class UpdatePostCommand < Imperator::Command::Restfull
111
111
 
112
112
  validates_presence_of :some_object_id
113
113
 
114
- update do
114
+ update_action do
115
115
  puts "updated OK"
116
116
  end
117
117
  end
@@ -128,7 +128,7 @@ class UpdatePostCommand < Imperator::Mongoid::RestCommand
128
128
 
129
129
  validates :name, presence: true
130
130
 
131
- update do
131
+ update_action do
132
132
  puts "#{object} was updated"
133
133
  end
134
134
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "imperator-ext"
8
- s.version = "0.2.2"
8
+ s.version = "0.2.3"
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-15"
12
+ s.date = "2012-08-17"
13
13
  s.description = "Factories, Macros, REST helpers and Mongoid integration"
14
14
  s.email = "kmandrup@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -27,6 +27,8 @@ Gem::Specification.new do |s|
27
27
  "VERSION",
28
28
  "docs/Design.md",
29
29
  "imperator-ext.gemspec",
30
+ "lib/generators/imperator/command_generator.rb",
31
+ "lib/generators/imperator/templates/command.tt",
30
32
  "lib/imperator-ext.rb",
31
33
  "lib/imperator/command-ext.rb",
32
34
  "lib/imperator/command/class_factory.rb",
@@ -39,6 +41,7 @@ Gem::Specification.new do |s|
39
41
  "lib/imperator/mongoid/attribute_helper.rb",
40
42
  "lib/imperator/mongoid/command.rb",
41
43
  "lib/imperator/mongoid/command/rest.rb",
44
+ "lib/imperator/rails/engine.rb",
42
45
  "spec/imperator-ext/command/class_factory_spec.rb",
43
46
  "spec/imperator-ext/command/macros_spec.rb",
44
47
  "spec/imperator-ext/command/method_factory_spec.rb",
@@ -0,0 +1,35 @@
1
+ module Imperator
2
+ module Generators
3
+ class CommandGenerator < ::Rails::Generators::NamedBase
4
+ desc 'Generates an Imperator Command'
5
+
6
+ class_option :orm, type: :string, default: nil, desc: 'Speficic ORM adapter to use'
7
+
8
+ def main_flow
9
+ empty_directory "app/commands"
10
+ inside "app/commands" do
11
+ template "command.tt", "#{file_path}_command.rb"
12
+ end
13
+ end
14
+
15
+ protected
16
+
17
+ def orm
18
+ options[:orm]
19
+ end
20
+
21
+ def parent_class
22
+ raise ArgumentError, "Invalid ORM: #{orm}, must be one of #{valid_orms}" unless valid_orm? orm
23
+ orm ? "::Imperator::#{orm.camelize}::Command" : "::Imperator::Command"
24
+ end
25
+
26
+ def valid_orm? orm
27
+ valid_orms.include? orm.to_s
28
+ end
29
+
30
+ def valid_orms
31
+ %w{mongoid}
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ class <%= class_name %>Command < <%= parent_class %>
2
+ # attributes :a, :b
3
+ # attribute :c, Integer
4
+
5
+ # validates_presence_of :c
6
+
7
+ action do
8
+ # ...
9
+ end
10
+
11
+ protected
12
+
13
+ end
@@ -3,3 +3,10 @@ require 'imperator/factory'
3
3
  require 'imperator/command-ext'
4
4
 
5
5
  require 'imperator/mongoid' if defined?(Mongoid)
6
+
7
+ require 'imperator/rails/engine' if defined?(::Rails::Engine)
8
+
9
+ module Commands
10
+ Command = ::Imperator::Command
11
+ MongoidCommand = ::Imperator::Mongoid::Command
12
+ end
@@ -34,6 +34,10 @@ class Imperator::Command
34
34
  end
35
35
  end
36
36
 
37
+ def rest_action name, &block
38
+ send("#{name}_action", &block) if supported_rest_actions.include? name.to_sym
39
+ end
40
+
37
41
  def on_error &block
38
42
  define_method(:on_error, &block)
39
43
  end
@@ -48,6 +52,10 @@ class Imperator::Command
48
52
 
49
53
  protected
50
54
 
55
+ def supported_rest_actions
56
+ [:create, :update, :delete]
57
+ end
58
+
51
59
  # convert to underscore format, fx UpdatePostCommand becomes update_post_command
52
60
  # remove 'create', 'update' or 'delete' in the front of name: _post_command
53
61
  # then remove command at the back: _post_
@@ -0,0 +1,9 @@
1
+ module Imperator
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ initializer 'Imperator ext setup' do
5
+ config.auto_load_paths += Dir[Rails.root.join('app/commands')]
6
+ end
7
+ end
8
+ end
9
+ 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.2
4
+ version: 0.2.3
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-15 00:00:00.000000000 Z
12
+ date: 2012-08-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: virtus
@@ -141,6 +141,8 @@ files:
141
141
  - VERSION
142
142
  - docs/Design.md
143
143
  - imperator-ext.gemspec
144
+ - lib/generators/imperator/command_generator.rb
145
+ - lib/generators/imperator/templates/command.tt
144
146
  - lib/imperator-ext.rb
145
147
  - lib/imperator/command-ext.rb
146
148
  - lib/imperator/command/class_factory.rb
@@ -153,6 +155,7 @@ files:
153
155
  - lib/imperator/mongoid/attribute_helper.rb
154
156
  - lib/imperator/mongoid/command.rb
155
157
  - lib/imperator/mongoid/command/rest.rb
158
+ - lib/imperator/rails/engine.rb
156
159
  - spec/imperator-ext/command/class_factory_spec.rb
157
160
  - spec/imperator-ext/command/macros_spec.rb
158
161
  - spec/imperator-ext/command/method_factory_spec.rb
@@ -179,7 +182,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
179
182
  version: '0'
180
183
  segments:
181
184
  - 0
182
- hash: 3676978282257002910
185
+ hash: 1670776621181325968
183
186
  required_rubygems_version: !ruby/object:Gem::Requirement
184
187
  none: false
185
188
  requirements: