Module: Imperator::Command::MethodFactory

Extended by:
MethodFactory
Included in:
MethodFactory
Defined in:
lib/imperator/command/method_factory.rb

Instance Method Summary collapse

Instance Method Details

#command_method(command, options = {}, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/imperator/command/method_factory.rb', line 3

def command_method command, options = {}, &block
  namespace = (options[:ns] || '').to_s
  namespace.sub! /Controller$/, ''
  define_method "#{command}_command" do
    instance_var = "@#{command}_command"
    unless instance_variable_get(instance_var)
      clazz = [namespace, "#{command.to_s.camelize}Command"].join('::').constantize
      opts = options.merge(initiator: self)
      opts.merge!(instance_eval &block) if block_given?
      clazz_inst = clazz.new(opts)
      instance_variable_set instance_var, clazz_inst
    end
  end
end

#command_methods(*args, &block) ⇒ Object



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

def command_methods *args, &block
  options = args.extract_options!
  args.flatten.each { |meth| command_method meth, options, &block }
end