Class: Af::Application

Inherits:
Object
  • Object
show all
Includes:
Deprecated, Logging, OptionParser
Defined in:
lib/fiksu-af/application.rb,
lib/fiksu-af/application/component.rb

Overview

Abstract superclass for implementing command line applications.

Provides:

* Command line option parsing.
* Logging with Log4r.
* Pre and post option processes hooks.
* Proxy support access to application frame functionality from other classes

Subclasses must implement:

* work

Subclasses can implement:

* pre_work

Defined Under Namespace

Modules: Component, Proxy, SafeProxy

Constant Summary collapse

@@singleton =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deprecated

included, #update_opts

Methods included from Logging

included, #logger, #logging_configurator

Methods included from OptionParser

add_option_types, included

Methods included from OptionParser::Interface

#opt, #opt_check, #opt_error, #opt_group, #opt_select, #process_command_line_options, #usage

Instance Attribute Details

#has_errorsObject


*** Attributes *** +++++++++++++++++++



84
85
86
# File 'lib/fiksu-af/application.rb', line 84

def has_errors
  @has_errors
end

Class Method Details

._run(*arguments) ⇒ Object

Run this application with the provided arguments that must adhere to configured command line switches. It rewrites ARGV with these values.

Example

instance._run("-v", "--file", "foo.log")

Arguments

* arguments - list of command line option strings


125
126
127
128
129
# File 'lib/fiksu-af/application.rb', line 125

def self._run(*arguments)
  # this ARGV hack is here for test specs to add script arguments
  ARGV[0..-1] = arguments if arguments.length > 0
  self.new._run
end

.run(*arguments) ⇒ Object

Instantiate and run the application.

Arguments

- arguments - ????


96
97
98
99
# File 'lib/fiksu-af/application.rb', line 96

def self.run(*arguments)
  application = self.new._run(*arguments)
  application._work
end

.singleton(safe = false) ⇒ Object

Return the single allowable instance of this class.

Arguments

* safe - defaults to false, instantiates instance if it doesn't exist


105
106
107
108
109
110
111
112
113
114
# File 'lib/fiksu-af/application.rb', line 105

def self.singleton(safe = false)
  if @@singleton.nil?
    if safe
      @@singleton = new
    else
      fail("Application @@singleton not initialized! Maybe you are using a Proxy before creating an instance? or use SafeProxy")
    end
  end
  return @@singleton
end

Instance Method Details

#_runObject

Run the application, fetching and parsing options from the command line.

Arguments

* usage - string describing usage (optional)
* options - hash of options, containing ???


141
142
143
144
145
146
# File 'lib/fiksu-af/application.rb', line 141

def _run
  process_command_line_options(af_opt_class_path)
  post_command_line_parsing
  pre_work
  return self
end

#_workObject

Execute the actual work of the application upon execution.

This method is used to wrap the actual run code with whatever specific code we are looking to maintain the execution context.

one can imagine overlaoding this function with something call initiates a profiler or debugger



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fiksu-af/application.rb', line 156

def _work
  begin
    work
  rescue SystemExit => se
    # we do nothing here
    if se.status != 0
      logger.error "exit called with error: #{se.message}"
      logger.warn se
      exit se.status
    end
  rescue SignalException => e
    logger.error "receiving signal: #{e.message}"
    logger.warn e
    @has_errors = true
  rescue Exception => e
    # RSpec occasionally uses a special exception when running tests to verify test
    # results. To ensure that exception passes through to RSpec, we catch it
    # specifically and re-raise it if needed.
    raise e if e.class.name == "RSpec::Mocks::MockExpectationError"

    # catching Exception cause some programs and libraries suck
    logger.error "fatal error during work: #{e.message}"
    logger.warn e
    @has_errors = true
  end

  if @gc_profiler
    logger("GC::Profiler").info GC::Profiler.result
  end

  exit @has_errors ? 1 : 0
end

#af_nameObject

Accessor for the af name set on the instance’s class.



190
191
192
# File 'lib/fiksu-af/application.rb', line 190

def af_name
  return self.class.name
end

#af_opt_class_pathObject

override if you wish to include other class’s opt/opt_group



195
196
197
# File 'lib/fiksu-af/application.rb', line 195

def af_opt_class_path
  return [self.class]
end