Module: CommandProc::ClassMethods

Defined in:
lib/command_proc.rb

Instance Method Summary collapse

Instance Method Details

#get_instance(name) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/command_proc.rb', line 27

def get_instance(name)
  if self.instances[name]
    self.instances[name]
  else
    self.instances[name] = self.new(name)
  end
  self.instances[name]
end

#init_instances ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/command_proc.rb', line 15

def init_instances
  class_eval <<-CODE
  def self.instances
      @@instances ||= nil
  end
  def self.instances=(value)
      @@instances = value
  end
  @@instances = {}
  CODE
end

#invoke(args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/command_proc.rb', line 36

def invoke(args)
  instance = get_instance(nil)
  command = args.shift
  if command == nil
    command = "help"
  end
  if instance.respond_to?(command)
    instance.send(command, args)
  else
    name = command
    instance = get_instance(name)
    command = args.shift
    instance.send(command, args)
  end
end