Module: Main::Program::InstanceMethods
- Included in:
- Main::Program
- Defined in:
- lib/main/program/instance_methods.rb
Instance Method Summary collapse
- #abort(message = 'exit') ⇒ Object
- #after_initialize ⇒ Object
- #after_parse_parameters ⇒ Object
- #after_run ⇒ Object
- #before_initialize ⇒ Object
- #before_parse_parameters ⇒ Object
- #before_run ⇒ Object
- #config(&block) ⇒ Object
- #daemon ⇒ Object
- #db(&block) ⇒ Object
- #fcall(object, method, *argv, &block) ⇒ Object
- #finalize ⇒ Object
- #handle_exception(e) ⇒ Object
- #handle_exit(status) ⇒ Object
- #help!(status = 0) ⇒ Object
- #help? ⇒ Boolean
- #initialize ⇒ Object
- #input ⇒ Object
- #instance_eval_block(*argv, &block) ⇒ Object
- #logger=(log) ⇒ Object
- #main_env(*args, &block) ⇒ Object
- #main_initialize ⇒ Object
- #modes ⇒ Object
- #output ⇒ Object
- #parse_parameters ⇒ Object
- #post_initialize ⇒ Object
- #post_parse_parameters ⇒ Object
- #post_run ⇒ Object
- #pre_initialize ⇒ Object
- #pre_parse_parameters ⇒ Object
- #pre_run ⇒ Object
- #run ⇒ Object
- #setup_finalizers ⇒ Object
- #setup_io_redirection ⇒ Object
- #setup_io_restoration ⇒ Object
- #setup_logging ⇒ Object
- #shell! ⇒ Object
- #state_path(&block) ⇒ Object (also: #dotdir)
- #stderr=(io) ⇒ Object
- #stdin=(io) ⇒ Object
- #stdout=(io) ⇒ Object
Instance Method Details
#abort(message = 'exit') ⇒ Object
219 220 221 |
# File 'lib/main/program/instance_methods.rb', line 219 def abort( = 'exit') raise SystemExit.new() end |
#after_initialize ⇒ Object
62 |
# File 'lib/main/program/instance_methods.rb', line 62 def after_initialize() :hook end |
#after_parse_parameters ⇒ Object
187 |
# File 'lib/main/program/instance_methods.rb', line 187 def after_parse_parameters() :hook end |
#after_run ⇒ Object
195 |
# File 'lib/main/program/instance_methods.rb', line 195 def after_run() :hook end |
#before_initialize ⇒ Object
55 |
# File 'lib/main/program/instance_methods.rb', line 55 def before_initialize() :hook end |
#before_parse_parameters ⇒ Object
176 |
# File 'lib/main/program/instance_methods.rb', line 176 def before_parse_parameters() :hook end |
#before_run ⇒ Object
191 |
# File 'lib/main/program/instance_methods.rb', line 191 def before_run() :hook end |
#config(&block) ⇒ Object
298 299 300 |
# File 'lib/main/program/instance_methods.rb', line 298 def config(&block) self.class.config(&block) end |
#daemon ⇒ Object
310 311 312 |
# File 'lib/main/program/instance_methods.rb', line 310 def daemon @daemon ||= Main::Daemon.new(self) end |
#db(&block) ⇒ Object
294 295 296 |
# File 'lib/main/program/instance_methods.rb', line 294 def db(&block) self.class.db(&block) end |
#fcall(object, method, *argv, &block) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/main/program/instance_methods.rb', line 256 def fcall(object, method, *argv, &block) method = object.method(method) arity = method.arity if arity >= 0 argv = argv[0, arity] else arity = arity.abs - 1 argv = argv[0, arity] + argv[arity .. -1] end method.call(*argv, &block) end |
#finalize ⇒ Object
69 70 71 |
# File 'lib/main/program/instance_methods.rb', line 69 def finalize while((f = finalizers.pop)); f.call; end end |
#handle_exception(e) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/main/program/instance_methods.rb', line 223 def handle_exception(e) if e.respond_to?(:error_handler_before) fcall(e, :error_handler_before, self) end if e.respond_to?(:error_handler_instead) fcall(e, :error_handler_instead, self) else if e.respond_to?(:status) exit_status(( e.status )) end if Softspoken === e or SystemExit === e quiet = ((SystemExit === e and e..respond_to?('abort')) or # see main/stdext.rb (SystemExit === e and e. == 'exit')) stderr.puts e. unless quiet else fatal{ e } end end if e.respond_to?(:error_handler_after) fcall(e, :error_handler_after, self) end exit_status(( exit_failure )) if exit_status == exit_success exit_status(( Util.integer(exit_status) rescue(exit_status ? 0 : 1) )) end |
#handle_exit(status) ⇒ Object
252 253 254 |
# File 'lib/main/program/instance_methods.rb', line 252 def handle_exit(status) exit(( Integer(status) rescue 1 )) end |
#help!(status = 0) ⇒ Object
203 204 205 206 |
# File 'lib/main/program/instance_methods.rb', line 203 def help!(status = 0) print usage.to_s exit(status) end |
#help? ⇒ Boolean
208 209 210 |
# File 'lib/main/program/instance_methods.rb', line 208 def help? (params['help'] and params['help'].given?) or argv.first == 'help' end |
#initialize ⇒ Object
61 |
# File 'lib/main/program/instance_methods.rb', line 61 def initialize() :hook end |
#input ⇒ Object
302 303 304 |
# File 'lib/main/program/instance_methods.rb', line 302 def input @input ||= params[:input].value if params[:input] end |
#instance_eval_block(*argv, &block) ⇒ Object
276 277 278 279 280 281 282 283 |
# File 'lib/main/program/instance_methods.rb', line 276 def instance_eval_block(*argv, &block) singleton_class = class << self self end singleton_class.module_eval{ define_method('__instance_eval_block', &block) } fcall(self, '__instance_eval_block', *argv, &block) end |
#logger=(log) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/main/program/instance_methods.rb', line 84 def logger= log unless(defined?(@logger) and @logger == log) case log when ::Logger, Logger @logger = log else if log.is_a?(Array) @logger = Logger.new(*log) else @logger = Logger.new(log) @logger.level = logger_level end end end @logger end |
#main_env(*args, &block) ⇒ Object
285 286 287 |
# File 'lib/main/program/instance_methods.rb', line 285 def main_env(*args, &block) self.class.main_env(*args, &block) end |
#main_initialize ⇒ Object
56 57 58 59 60 |
# File 'lib/main/program/instance_methods.rb', line 56 def main_initialize() setup_finalizers setup_io_redirection setup_logging end |
#modes ⇒ Object
199 200 201 |
# File 'lib/main/program/instance_methods.rb', line 199 def modes self.class.modes end |
#output ⇒ Object
306 307 308 |
# File 'lib/main/program/instance_methods.rb', line 306 def output @output ||= params[:output].value if params[:output] end |
#parse_parameters ⇒ Object
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/main/program/instance_methods.rb', line 177 def parse_parameters pre_parse_parameters before_parse_parameters self.class.parameters.parse(self) self.class.parameters.each{|p| params[p.name.to_s] = p} after_parse_parameters post_parse_parameters end |
#post_initialize ⇒ Object
63 |
# File 'lib/main/program/instance_methods.rb', line 63 def post_initialize() :hook end |
#post_parse_parameters ⇒ Object
188 |
# File 'lib/main/program/instance_methods.rb', line 188 def post_parse_parameters() :hook end |
#post_run ⇒ Object
196 |
# File 'lib/main/program/instance_methods.rb', line 196 def post_run() :hook end |
#pre_initialize ⇒ Object
54 |
# File 'lib/main/program/instance_methods.rb', line 54 def pre_initialize() :hook end |
#pre_parse_parameters ⇒ Object
175 |
# File 'lib/main/program/instance_methods.rb', line 175 def pre_parse_parameters() :hook end |
#pre_run ⇒ Object
190 |
# File 'lib/main/program/instance_methods.rb', line 190 def pre_run() :hook end |
#run ⇒ Object
192 193 194 |
# File 'lib/main/program/instance_methods.rb', line 192 def run raise NotImplementedError, 'run not defined' end |
#setup_finalizers ⇒ Object
65 66 67 |
# File 'lib/main/program/instance_methods.rb', line 65 def setup_finalizers self.class.setup_finalizers(self) end |
#setup_io_redirection ⇒ Object
73 74 75 76 77 |
# File 'lib/main/program/instance_methods.rb', line 73 def setup_io_redirection self.stdin = opts['stdin'] || opts[:stdin] || stdin self.stdout = opts['stdout'] || opts[:stdout] || stdout self.stderr = opts['stderr'] || opts[:stderr] || stderr end |
#setup_io_restoration ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/main/program/instance_methods.rb', line 101 def setup_io_restoration [STDIN, STDOUT, STDERR].each do |io| dup = io.dup finalizers.push( lambda do io.reopen(dup) end ) end end |
#setup_logging ⇒ Object
79 80 81 82 |
# File 'lib/main/program/instance_methods.rb', line 79 def setup_logging log = self.class.logger || stderr self.logger = log end |
#shell! ⇒ Object
212 213 214 215 216 217 |
# File 'lib/main/program/instance_methods.rb', line 212 def shell! Pry.hooks.clear_all prompt = "#{ name } > " Pry.config.prompt = proc{|*a| prompt } binding.pry end |
#state_path(&block) ⇒ Object Also known as: dotdir
289 290 291 |
# File 'lib/main/program/instance_methods.rb', line 289 def state_path(&block) self.class.state_path(&block) end |
#stderr=(io) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/main/program/instance_methods.rb', line 155 def stderr= io unless(defined?(@stderr) and (@stderr == io)) @stderr = if io.respond_to?('write') io else fd = open(io.to_s, 'w+') finalizers.push(lambda{ fd.close }) fd end begin STDERR.reopen(@stderr) rescue $stderr = @stderr ::Object.send(:remove_const, 'STDERR') ::Object.send(:const_set, 'STDERR', @stderr) end end end |
#stdin=(io) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/main/program/instance_methods.rb', line 113 def stdin= io unless(defined?(@stdin) and (@stdin == io)) @stdin = if io.respond_to?('read') io else fd = open(io.to_s, 'r+') finalizers.push(lambda{ fd.close }) fd end begin STDIN.reopen(@stdin) rescue $stdin = @stdin ::Object.send(:remove_const, 'STDIN') ::Object.send(:const_set, 'STDIN', @stdin) end end end |
#stdout=(io) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/main/program/instance_methods.rb', line 134 def stdout= io unless(defined?(@stdout) and (@stdout == io)) @stdout = if io.respond_to?('write') io else fd = open(io.to_s, 'w+') finalizers.push(lambda{ fd.close }) fd end begin STDOUT.reopen(@stdout) rescue $stdout = @stdout ::Object.send(:remove_const, 'STDOUT') ::Object.send(:const_set, 'STDOUT', @stdout) end end end |