Class: OptimusPrime::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/optimus_prime/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(handler, caller = nil) ⇒ Command

Returns a new instance of Command.



3
4
5
# File 'lib/optimus_prime/command.rb', line 3

def initialize(handler, caller=nil)
  @handler, @caller = handler, (caller && caller[1])
end

Instance Method Details

#arityObject



30
31
32
# File 'lib/optimus_prime/command.rb', line 30

def arity
  @handler.arity
end

#fileObject



22
23
24
# File 'lib/optimus_prime/command.rb', line 22

def file
  @file ||= File.read(@caller.split(':').first)
end

#helpObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/optimus_prime/command.rb', line 7

def help
  @help ||= begin
    lines = file.split(/\n/)
    result = []
    i = 0
    while (string = lines[line+i]) =~ /\s*##?/
      result << string.gsub(/^\s*#*\s?/, '')
      i += 1
    end
    result.shift if result.first.empty?
    result.pop   if result.last.empty?
    result.join("\n")
  end
end

#lineObject



26
27
28
# File 'lib/optimus_prime/command.rb', line 26

def line
  @caller.split(':').last.to_i
end

#to_procObject



34
35
36
37
38
39
# File 'lib/optimus_prime/command.rb', line 34

def to_proc
  case handler = @handler
  when Proc then handler
  else @handler.to_proc
  end
end