Module: Main::Program::InstanceMethods

Included in:
Main::Program
Defined in:
lib/main/program/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#abort(message = 'exit') ⇒ Object

Raises:

  • (SystemExit)


214
215
216
# File 'lib/main/program/instance_methods.rb', line 214

def abort(message = 'exit')
  raise SystemExit.new(message)
end

#after_initializeObject



50
# File 'lib/main/program/instance_methods.rb', line 50

def after_initialize() :hook end

#after_parse_parametersObject



182
# File 'lib/main/program/instance_methods.rb', line 182

def after_parse_parameters() :hook end

#after_runObject



190
# File 'lib/main/program/instance_methods.rb', line 190

def after_run() :hook end

#before_initializeObject



43
# File 'lib/main/program/instance_methods.rb', line 43

def before_initialize() :hook end

#before_parse_parametersObject



170
# File 'lib/main/program/instance_methods.rb', line 170

def before_parse_parameters() :hook end

#before_runObject



186
# File 'lib/main/program/instance_methods.rb', line 186

def before_run() :hook end

#config(&block) ⇒ Object



293
294
295
# File 'lib/main/program/instance_methods.rb', line 293

def config(&block)
  self.class.config(&block)
end

#daemonObject



305
306
307
# File 'lib/main/program/instance_methods.rb', line 305

def daemon
  @daemon ||= Main::Daemon.new(self)
end

#db(&block) ⇒ Object



289
290
291
# File 'lib/main/program/instance_methods.rb', line 289

def db(&block)
  self.class.db(&block)
end

#fcall(object, method, *argv, &block) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/main/program/instance_methods.rb', line 251

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

#finalizeObject



61
62
63
64
# File 'lib/main/program/instance_methods.rb', line 61

def finalize
  @finalizers ||= []
  while((f = @finalizers.pop)); f.call; end
end

#handle_exception(e) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/main/program/instance_methods.rb', line 218

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.message.respond_to?('abort')) or # see main/stdext.rb
              (SystemExit === e and e.message == 'exit'))
      stderr.puts e.message 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



247
248
249
# File 'lib/main/program/instance_methods.rb', line 247

def handle_exit(status)
  exit(( Integer(status) rescue 1 ))
end

#help!(status = 0) ⇒ Object



198
199
200
201
# File 'lib/main/program/instance_methods.rb', line 198

def help!(status = 0)
  print usage.to_s
  exit(status)
end

#help?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/main/program/instance_methods.rb', line 203

def help?
  (params['help'] and params['help'].given?) or argv.first == 'help'
end

#initializeObject



49
# File 'lib/main/program/instance_methods.rb', line 49

def initialize() :hook end

#inputObject



297
298
299
# File 'lib/main/program/instance_methods.rb', line 297

def input
  @input ||= params[:input].value if params[:input]
end

#instance_eval_block(*argv, &block) ⇒ Object



271
272
273
274
275
276
277
278
# File 'lib/main/program/instance_methods.rb', line 271

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/main/program/instance_methods.rb', line 77

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



280
281
282
# File 'lib/main/program/instance_methods.rb', line 280

def main_env(*args, &block)
  self.class.main_env(*args, &block)
end

#main_initializeObject



44
45
46
47
48
# File 'lib/main/program/instance_methods.rb', line 44

def main_initialize()
  setup_finalizers
  setup_io_redirection
  setup_logging
end

#modesObject



194
195
196
# File 'lib/main/program/instance_methods.rb', line 194

def modes
  self.class.modes
end

#outputObject



301
302
303
# File 'lib/main/program/instance_methods.rb', line 301

def output
  @output ||= params[:output].value if params[:output]
end

#parse_parametersObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/main/program/instance_methods.rb', line 171

def parse_parameters
  pre_parse_parameters
  before_parse_parameters

  self.class.parameters.parse(self)
  @params = Parameter::Table.new
  self.class.parameters.each{|p| @params[p.name.to_s] = p}

  after_parse_parameters
  post_parse_parameters
end

#post_initializeObject



51
# File 'lib/main/program/instance_methods.rb', line 51

def post_initialize() :hook end

#post_parse_parametersObject



183
# File 'lib/main/program/instance_methods.rb', line 183

def post_parse_parameters() :hook end

#post_runObject



191
# File 'lib/main/program/instance_methods.rb', line 191

def post_run() :hook end

#pre_initializeObject



42
# File 'lib/main/program/instance_methods.rb', line 42

def pre_initialize() :hook end

#pre_parse_parametersObject



169
# File 'lib/main/program/instance_methods.rb', line 169

def pre_parse_parameters() :hook end

#pre_runObject



185
# File 'lib/main/program/instance_methods.rb', line 185

def pre_run() :hook end

#runObject

Raises:

  • (NotImplementedError)


187
188
189
# File 'lib/main/program/instance_methods.rb', line 187

def run
  raise NotImplementedError, 'run not defined'
end

#setup_finalizersObject



53
54
55
56
57
58
59
# File 'lib/main/program/instance_methods.rb', line 53

def setup_finalizers
  @finalizers ||= []
  finalizers = @finalizers
  ObjectSpace.define_finalizer(self) do
    while((f = finalizers.pop)); f.call; end
  end
end

#setup_io_redirectionObject



66
67
68
69
70
# File 'lib/main/program/instance_methods.rb', line 66

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_restorationObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/main/program/instance_methods.rb', line 94

def setup_io_restoration
  @finalizers ||= []
  [STDIN, STDOUT, STDERR].each do |io|
    dup = io.dup
    @finalizers.push(
      lambda do
        io.reopen(dup)
      end
    )
  end
end

#setup_loggingObject



72
73
74
75
# File 'lib/main/program/instance_methods.rb', line 72

def setup_logging
  log = self.class.logger || stderr
  self.logger = log
end

#shell!Object



207
208
209
210
211
212
# File 'lib/main/program/instance_methods.rb', line 207

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



284
285
286
# File 'lib/main/program/instance_methods.rb', line 284

def state_path(&block)
  self.class.state_path(&block)
end

#stderr=(io) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/main/program/instance_methods.rb', line 149

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/main/program/instance_methods.rb', line 107

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/main/program/instance_methods.rb', line 128

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