Permalink
Browse files

better class factory

  • Loading branch information...
kristianmandrup committed Aug 3, 2012
1 parent f8f2740 commit 9d60169a1e83373cd67c1e8289d3615d1b0b6d72
Showing with 18 additions and 4 deletions.
  1. +1 −0 lib/imperator/command.rb
  2. +17 −4 lib/imperator/command/class_factory.rb
View
@@ -84,5 +84,6 @@ def action
def perform
run_callbacks(:perform) { action }
+ self
end
end
@@ -6,17 +6,30 @@ def use &block
yield self
end
+ def default_parent clazz
+ @default_parent ||= clazz
+ end
+
+ def get_default_parent
+ @default_default_parent ||= ::Imperator::Command
+ end
+
# Usage:
# Imperator::Command::ClassFactory.create :update, Post, parent: Imperator::Mongoid::Command do
# ..
# end
def create action, model, options = {}, &block
- parent = options[:parent] || Imperator::Command
- clazz = Class.new parent do
+ clazz_name = "#{action.to_s.camelize}#{model.to_s.camelize}Command"
+ parent ||= options[:parent] || get_default_parent
+ clazz = parent ? Class.new(parent) : Class.new
+ Kernel.const_set clazz_name, clazz
+ clazz = self.const_get(clazz_name)
+ clazz.class_eval do
attributes_for(model, :except => options[:except]) if options[:auto_attributes]
- yield
end
- Kernel.const_set "#{action}#{model}Command", clazz
+ if block_given?
+ clazz.class_eval &block
+ end
end
# Usage:

0 comments on commit 9d60169

Please sign in to comment.