Module: Imperator::Command::ClassFactory::Methods
- Defined in:
- lib/imperator/command/class_factory.rb
Instance Attribute Summary collapse
- #default_class ⇒ Object
- #default_options ⇒ Object
-
#default_rest_classes ⇒ Object
Returns the value of attribute default_rest_classes.
- #initial_rest_class ⇒ Object
- #initial_rest_classes ⇒ Object
Instance Method Summary collapse
-
#build_command(action, model, options = {}, &block) ⇒ Object
Usage: Imperator::Command::ClassFactory.create :update, Post, parent: Imperator::Mongoid::Command do ..
- #default_rest_class(model = nil) ⇒ Object
- #reset! ⇒ Object
- #reset_rest! ⇒ Object
-
#rest_command(action, model, options = {}, &block) ⇒ Object
Usage: Imperator::Command::ClassFactory.create_rest :all, Post, parent: Imperator::Mongoid::Command do ..
- #set_default_rest_class(model, type = nil) ⇒ Object
Instance Attribute Details
#default_class ⇒ Object
23 24 25 |
# File 'lib/imperator/command/class_factory.rb', line 23 def default_class @default_class ||= ::Imperator::Command end |
#default_options ⇒ Object
56 57 58 |
# File 'lib/imperator/command/class_factory.rb', line 56 def @default_options ||= {} end |
#default_rest_classes ⇒ Object
Returns the value of attribute default_rest_classes.
17 18 19 |
# File 'lib/imperator/command/class_factory.rb', line 17 def default_rest_classes @default_rest_classes end |
Instance Method Details
#build_command(action, model, options = {}, &block) ⇒ Object
Usage: Imperator::Command::ClassFactory.create :update, Post, parent: Imperator::Mongoid::Command do
..
end
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/imperator/command/class_factory.rb', line 64 def build_command action, model, = {}, &block clazz_name = "#{action.to_s.camelize}#{model.to_s.camelize}Command" parent = [:parent] || default_class clazz = parent ? Class.new(parent) : Class.new Object.const_set clazz_name, clazz context = self.kind_of?(Class) ? self : self.class clazz = context.const_get(clazz_name) if [:auto_attributes] clazz.instance_eval do if respond_to? :attributes_for attributes_for(model, :except => [:except], :only => [:only]) end end end if block_given? clazz.instance_eval &block end clazz end |
#default_rest_class(model = nil) ⇒ Object
27 28 29 30 |
# File 'lib/imperator/command/class_factory.rb', line 27 def default_rest_class model = nil return initial_rest_class unless model return default_rest_classes[:mongoid] if model.ancestors.include?(Mongoid::Document) end |
#reset! ⇒ Object
49 50 51 52 |
# File 'lib/imperator/command/class_factory.rb', line 49 def reset! reset_rest! default_class = ::Imperator::Command end |
#reset_rest! ⇒ Object
44 45 46 47 |
# File 'lib/imperator/command/class_factory.rb', line 44 def reset_rest! @default_rest_classes = initial_rest_classes @initial_rest_class = Imperator::Command::Rest end |
#rest_command(action, model, options = {}, &block) ⇒ Object
Usage: Imperator::Command::ClassFactory.create_rest :all, Post, parent: Imperator::Mongoid::Command do
..
end
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/imperator/command/class_factory.rb', line 88 def rest_command action, model, = {}, &block .reverse_merge! [:parent] ||= default_rest_class(model) rest_commands_for(model, , &block) and return if action.to_sym == :all if rest_actions.include? action.to_sym action_name = "#{action}_command_for" send action_name, model, , &block else raise ArgumentError, "Not a supported REST action. Must be one of #{rest_actions}, was #{action}" end end |
#set_default_rest_class(model, type = nil) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/imperator/command/class_factory.rb', line 36 def set_default_rest_class model, type = nil unless type.nil? @default_rest_classes[type.to_sym] = model return end @initial_rest_class = model end |