Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added engine and generator

  • Loading branch information...
commit be18bea59d3a1699e0779672ca8e14c786dd5da1 1 parent 196861b
@kristianmandrup authored
View
49 README.md
@@ -43,7 +43,7 @@ class UpdatePostCommand < Imperator::Command::Rest
validates_presence_of :some_object_id
- update do
+ update_action do
puts "updated OK"
end
end
@@ -60,7 +60,7 @@ class UpdatePostCommand < Imperator::Mongoid::Command::Rest
validates :name, presence: true
- update do
+ update_action do
puts "#{object} was updated"
end
@@ -226,13 +226,13 @@ This is the recommended pattern for linking Focused Controller actions to Impera
## Rest Commands
-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.
+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.
Usage example:
```ruby
class UpdatePostCommand < Imperator::Command::Rest
- update_action do
+ rest_action :update do
notify :updated
end
end
@@ -348,6 +348,47 @@ module PostController
end
```
+## Namespacing convenience
+
+You can also place your commands directly under the Commands namespace.
+In this case you can do the following:
+
+``ruby
+module Commands
+ class SignInCommand < Command # or MongoidCommand
+ # ...
+ end
+end
+```
+
+## Rails generators
+
+A simple `imperator:command` generator is included:
+
+`$ rails g imperator:command sign_in`
+
+* Will create an `app/commands` folder with your commands (if not present)
+* Will create a `SignInCommand` class inheriting from `::Imperator::Command
+* class will be put in `app/commands/sign_in_command.rb`
+
+`$ rails g imperator:command sign_in --orm mongoid`
+
+Namespacing:
+
+`$ rails g imperator:command tenant::session::sign_in`
+
+* Will create a `Tenant::Session::SignInCommand` class in `app/commands/tenant/session/sign_in_command.rb`
+
+In this case you have to define/open the Tenant::Session namespace structure elsewhere.
+Fx: `app/commands/tenant/session.rb`
+
+```ruby
+class Tenant < User
+ module Session
+ end
+end
+```
+
## Contributing to imperator-ext
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
View
2  VERSION
@@ -1 +1 @@
-0.2.2
+0.2.3
View
4 docs/Design.md
@@ -111,7 +111,7 @@ class UpdatePostCommand < Imperator::Command::Restfull
validates_presence_of :some_object_id
- update do
+ update_action do
puts "updated OK"
end
end
@@ -128,7 +128,7 @@ class UpdatePostCommand < Imperator::Mongoid::RestCommand
validates :name, presence: true
- update do
+ update_action do
puts "#{object} was updated"
end
View
35 lib/generators/imperator/command_generator.rb
@@ -0,0 +1,35 @@
+module Imperator
+ module Generators
+ class CommandGenerator < ::Rails::Generators::NamedBase
+ desc 'Generates an Imperator Command'
+
+ class_option :orm, type: :string, default: nil, desc: 'Speficic ORM adapter to use'
+
+ def main_flow
+ empty_directory "app/commands"
+ inside "app/commands" do
+ template "command.tt", "#{file_path}_command.rb"
+ end
+ end
+
+ protected
+
+ def orm
+ options[:orm]
+ end
+
+ def parent_class
+ raise ArgumentError, "Invalid ORM: #{orm}, must be one of #{valid_orms}" unless valid_orm? orm
+ orm ? "::Imperator::#{orm.camelize}::Command" : "::Imperator::Command"
+ end
+
+ def valid_orm? orm
+ valid_orms.include? orm.to_s
+ end
+
+ def valid_orms
+ %w{mongoid}
+ end
+ end
+ end
+end
View
13 lib/generators/imperator/templates/command.tt
@@ -0,0 +1,13 @@
+class <%= class_name %>Command < <%= parent_class %>
+ # attributes :a, :b
+ # attribute :c, Integer
+
+ # validates_presence_of :c
+
+ action do
+ # ...
+ end
+
+ protected
+
+end
View
7 lib/imperator-ext.rb
@@ -3,3 +3,10 @@
require 'imperator/command-ext'
require 'imperator/mongoid' if defined?(Mongoid)
+
+require 'imperator/rails/engine' if defined?(::Rails::Engine)
+
+module Commands
+ Command = ::Imperator::Command
+ MongoidCommand = ::Imperator::Mongoid::Command
+end
View
8 lib/imperator/command/rest_helper.rb
@@ -34,6 +34,10 @@ def delete_action &block
end
end
+ def rest_action name, &block
+ send("#{name}_action", &block) if supported_rest_actions.include? name.to_sym
+ end
+
def on_error &block
define_method(:on_error, &block)
end
@@ -48,6 +52,10 @@ def object_class
protected
+ def supported_rest_actions
+ [:create, :update, :delete]
+ end
+
# convert to underscore format, fx UpdatePostCommand becomes update_post_command
# remove 'create', 'update' or 'delete' in the front of name: _post_command
# then remove command at the back: _post_
View
9 lib/imperator/rails/engine.rb
@@ -0,0 +1,9 @@
+module Imperator
+ module Rails
+ class Engine < ::Rails::Engine
+ initializer 'Imperator ext setup' do
+ config.auto_load_paths += Dir[Rails.root.join('app/commands')]
+ end
+ end
+ end
+end

0 comments on commit be18bea

Please sign in to comment.
Something went wrong with that request. Please try again.