Class: Scl::Control::ControllerModule
- Inherits:
-
Object
- Object
- Scl::Control::ControllerModule
show all
- Defined in:
- lib/scl/control/controller_module.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ControllerModule.
5
6
7
|
# File 'lib/scl/control/controller_module.rb', line 5
def initialize(controller)
@controller = controller
end
|
Instance Attribute Details
#controller ⇒ Object
Returns the value of attribute controller.
4
5
6
|
# File 'lib/scl/control/controller_module.rb', line 4
def controller
@controller
end
|
Class Method Details
.help(message, method) ⇒ Object
29
30
31
32
33
|
# File 'lib/scl/control/controller_module.rb', line 29
def self.help(message, method)
@@help ||= {}
@@help[self.name] ||= {}
@@help[self.name][method.to_s] = message
end
|
.print_help(args, method, own_methods = []) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/scl/control/controller_module.rb', line 35
def self.print_help(args, method, own_methods=[])
module_name = self.name.split('::').last.downcase
puts "======="
if method && @@help[self.name] && @@help[self.name][method]
puts "Usage: scl #{module_name} #{method} (options)\n"
puts
puts @@help[self.name][method].split("\n").map{|line| line.gsub(/^\s{6}/,'')}.join("\n")
puts
else
puts "No help docs found for \"#{method}\"\n=======\n" if method
puts "Usage: scl #{module_name} [command] (options)"
puts
puts "Supported commands are [#{own_methods.join(' ')}]"
puts
puts "Try scl #{module_name} [command] -h for more info"
puts
puts args.opts.to_s[/Where options.*/m]
end
exit(0)
end
|
Instance Method Details
#action(action_name, args) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/scl/control/controller_module.rb', line 56
def action(action_name, args)
args = args.dup
ARGV.clear
if @controller.args.help || !action_name
self.class.print_help(@controller.args, action_name, self.public_methods.select{|m| self.method(m).owner == self.class })
exit(0)
end
if self.respond_to?(action_name)
begin
action = self.method(action_name)
required_args = action.arity >= 0 ? action.arity : -(action.arity + 1)
unless required_args <= args.length
raise ControlError.new("#{action_name} expected at least #{required_args} arguments\nE.g.\n#{action_name} #{action.parameters.map(&:last).map{|x| "[#{x}]"}[-required_args..-1].join(' ')} (options)")
end
self.send(action_name, *args)
rescue ArgumentError => e
puts e.message
puts "#{action_name} expects #{required_args} arguments by default\nE.g.\n#{action_name} #{action.parameters.map(&:last).map{|x| "[#{x}]"}[-required_args..-1].join(' ')}"
self.class.print_help(@controller.args, action_name)
puts e.backtrace if @controller.verbose?
rescue ControlError => e
puts e.message
puts e.cause
puts e.cause.backtrace if @controller.verbose?
self.class.print_help(@controller.args, action_name)
end
else
own_methods = self.public_methods.select{|m| self.method(m).owner == self.class }
puts "Command not supported: \"#{action_name}\""
puts "Supported commands are [#{own_methods.join(' ')}]"
exit(1)
end
end
|
#args ⇒ Object
9
10
11
|
# File 'lib/scl/control/controller_module.rb', line 9
def args
@controller.args
end
|
#help? ⇒ Boolean
25
26
27
|
# File 'lib/scl/control/controller_module.rb', line 25
def help?
args.help
end
|
13
14
15
|
# File 'lib/scl/control/controller_module.rb', line 13
def input_decoder
@controller.input_decoder
end
|
#key_coder ⇒ Object
21
22
23
|
# File 'lib/scl/control/controller_module.rb', line 21
def key_coder
@controller.key_coder
end
|
#output_encoder ⇒ Object
17
18
19
|
# File 'lib/scl/control/controller_module.rb', line 17
def output_encoder
@controller.output_encoder
end
|