Module: Imperator::Command::ClassFactory::Methods

Included in:
Imperator::Command::ClassFactory, Imperator::Command::ClassFactory
Defined in:
lib/imperator/command/class_factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_classObject



23
24
25
# File 'lib/imperator/command/class_factory.rb', line 23

def default_class
  @default_class ||= ::Imperator::Command
end

#default_optionsObject



56
57
58
# File 'lib/imperator/command/class_factory.rb', line 56

def default_options
  @default_options ||= {}
end

#default_rest_classesObject

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

#initial_rest_classObject



32
33
34
# File 'lib/imperator/command/class_factory.rb', line 32

def initial_rest_class
  @initial_rest_class ||= Imperator::Command::Rest
end

#initial_rest_classesObject



19
20
21
# File 'lib/imperator/command/class_factory.rb', line 19

def initial_rest_classes
  @initial_rest_classes ||= {:mongoid => Imperator::Mongoid::Command::Rest}
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, options = {}, &block
  clazz_name = "#{action.to_s.camelize}#{model.to_s.camelize}Command"
  parent = options[: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 options[:auto_attributes]
    clazz.instance_eval do
      if respond_to? :attributes_for
        attributes_for(model, :except => options[:except], :only => options[: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, options = {}, &block
  options.reverse_merge! default_options
  options[:parent] ||= default_rest_class(model)
  rest_commands_for(model, options, &block) and return if action.to_sym == :all
  if rest_actions.include? action.to_sym        
    action_name = "#{action}_command_for"
    send action_name, model, options, &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