Class: ZTK::Base
- Inherits:
-
Object
- Object
- ZTK::Base
- Defined in:
- lib/ztk/base.rb
Overview
Base Class
This is the base class inherited by most of the other classes in this library. It provides a standard set of features to control STDOUT, STDERR and STDIN, a configuration mechanism and logging mechanism.
You should never interact with this class directly; you should inherit it and extend functionality as appropriate.
Direct Known Subclasses
Background, Command, GoogleChart::Base, Parallel, Report, SSH, TCPSocketCheck, UI
Class Method Summary (collapse)
-
+ (Object) build_config(config = {}, override = {})
Builds Configuration Object.
-
+ (Object) hash_config(config = {})
Hash Configuration.
-
+ (Object) log_and_raise(logger, exception, message, shift = 1)
Logs an exception and then raises it.
Instance Method Summary (collapse)
-
- (OpenStruct) config {|config| ... }
Configuration OpenStruct accessor method.
-
- (Object) direct_log(log_level) { ... }
Direct logging method.
-
- (Base) initialize(config = {}, override = {})
constructor
A new instance of Base.
-
- (Object) log_and_raise(exception, message, shift = 2)
Logs an exception and then raises it.
Constructor Details
- (Base) initialize(config = {}, override = {})
Returns a new instance of Base
75 76 77 |
# File 'lib/ztk/base.rb', line 75 def initialize(config={}, override={}) @config = Base.build_config(config, override) end |
Class Method Details
+ (Object) build_config(config = {}, override = {})
Builds Configuration Object
Builds an OpenStruct backed configuration object.
32 33 34 35 36 37 38 39 40 |
# File 'lib/ztk/base.rb', line 32 def build_config(config={}, override={}) config = OpenStruct.new({ :ui => ::ZTK::UI.new }.merge(hash_config(config)).merge(hash_config(override))) config.ui.logger.debug { "config=#{config.send(:table).inspect}" } config end |
+ (Object) hash_config(config = {})
Hash Configuration
Ensure a configuration is of object type Hash. Since we use OpenStructs we need to convert back to hash from time to time.
46 47 48 49 50 51 52 |
# File 'lib/ztk/base.rb', line 46 def hash_config(config={}) if config.is_a?(OpenStruct) config.send(:table) else config end end |
+ (Object) log_and_raise(logger, exception, message, shift = 1)
Logs an exception and then raises it.
62 63 64 65 66 67 68 69 |
# File 'lib/ztk/base.rb', line 62 def log_and_raise(logger, exception, , shift=1) if logger.is_a?(ZTK::Logger) logger.shift(:fatal, shift) { "EXCEPTION: #{exception.inspect} - #{.inspect}" } else logger.fatal { "EXCEPTION: #{exception.inspect} - #{.inspect}" } end raise exception, end |
Instance Method Details
- (OpenStruct) config {|config| ... }
Configuration OpenStruct accessor method.
If no block is given, the method will return the configuration OpenStruct object. If a block is given, the block is yielded with the configuration OpenStruct object.
87 88 89 90 91 92 93 |
# File 'lib/ztk/base.rb', line 87 def config(&block) if block_given? block.call(@config) else @config end end |
- (Object) direct_log(log_level) { ... }
Direct logging method.
This method provides direct writing of data to the current log device. This is mainly used for pushing STDOUT and STDERR into the log file in ZTK::SSH and ZTK::Command, but could easily be used by other classes.
The value returned in the block is passed down to the logger specified in the classes configuration.
119 120 121 122 123 124 125 126 127 |
# File 'lib/ztk/base.rb', line 119 def direct_log(log_level, &blocK) @config.ui.logger.nil? and raise BaseError, "You must supply a logger for direct logging support!" if !block_given? log_and_raise(BaseError, "You must supply a block to the log method!") elsif (@config.ui.logger.level <= ::Logger.const_get(log_level.to_s.upcase)) @config.ui.logger << ZTK::ANSI.uncolor(yield) end end |
- (Object) log_and_raise(exception, message, shift = 2)
Logs an exception and then raises it.
103 104 105 |
# File 'lib/ztk/base.rb', line 103 def log_and_raise(exception, , shift=2) Base.log_and_raise(config.ui.logger, exception, , shift) end |