Permalink
Browse files

improved command dsl defaults and code structure

  • Loading branch information...
1 parent 58f72d0 commit 53fa7da6c33313c6ba64176f3e25546f79ab29da @kristianmandrup committed Aug 1, 2012
Showing with 45 additions and 25 deletions.
  1. +45 −25 lib/imperator/command/rest.rb
@@ -1,36 +1,56 @@
class Imperator::Command
module Rest
- attribute :object_class, Object
+ class << self
+ attr_writer :object_class
- def self.create_new &block
- action do
- object_class.create attribute_set if object_class
- yield
- end
- end
+ def create_action &block
+ action do
+ object_class.create attribute_set if object_class
+ yield
+ end
+ end
- def self.update &block
- action do
- find_object.update_attributes attribute_set if find_object
- yield
- rescue e: Imperator::ResourceNotFoundError
- on_error e
- end
- end
+ def update_action &block
+ action do
+ find_object.update_attributes attribute_set if find_object
+ yield
+ rescue e: Imperator::ResourceNotFoundError
+ on_error e
+ end
+ end
- def self.delete &block
- action do
- find_object.delete
- yield
- end
- end
+ def delete_action &block
+ action do
+ find_object.delete
+ yield
+ end
+ end
+
+ def on_error &block
+ define_method(:on_error, &block)
+ end
+
+ def for_class clazz
+ @object_class = clazz
+ end
+
+ def object_class
+ @object_class ||= filtered_class_name
+ end
+
+ protected
- def self.on_error &block
- define_method(:on_error, &block)
+ # 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_
+ # then remove any '_': post
+ def filtered_class_name
+ self.class.name.underscore.sub(/^(create|update|delete)/, '').sub(/command$/, '').sub(/_/, '')
+ end
end
- def self.for_class clazz
- object_class = clazz
+ def object_class
+ self.class.object_class
end
def on_error exception

0 comments on commit 53fa7da

Please sign in to comment.