Class: Management::Command

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



5
6
7
# File 'lib/management/command.rb', line 5

def self.all
  @all ||= []
end

.inherited(subclass) ⇒ Object



9
10
11
# File 'lib/management/command.rb', line 9

def self.inherited(subclass)
  all << subclass.new
end

Instance Method Details

#call_with(args, error_handler) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/management/command.rb', line 43

def call_with(args, error_handler)
  arity = self.true_arity

  error_handler.call "not enough arguments" if args.count < arity.begin
  error_handler.call "too many arguments"   if args.count > arity.end

  fn.call *args
end

#command_nameObject



17
18
19
20
21
22
23
# File 'lib/management/command.rb', line 17

def command_name
  self.class.name.split('::').last.
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr('_', '-').
    downcase
end

#fnObject



13
14
15
# File 'lib/management/command.rb', line 13

def fn
  method(:run)
end

#help_stringObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/management/command.rb', line 25

def help_string
  return sprintf("%20s ", self.command_name) + fn.parameters.map do |req, name|
    name = '<' + name.to_s.sub(/_names?/, '') + '>'
    case req
    when :opt then '[' + name + ']'
    when :rest then name + ' [...]'
    else name
    end
  end.join(' ')
end

#true_arityObject



36
37
38
39
40
41
# File 'lib/management/command.rb', line 36

def true_arity
  min = fn.parameters.take_while{|req, name| req == :req}.count
  max = fn.parameters.count
  max = Float::INFINITY if max > 0 && fn.parameters.last.first == :rest
  min..max
end