lib/ztk/base.rb in ztk-1.15.0 vs lib/ztk/base.rb in ztk-1.15.1
- old
+ new
@@ -19,41 +19,38 @@
# @author Zachary Patten <zachary AT jovelabs DOT com>
class Base
class << self
- # @param [Hash] configuration Configuration options hash.
+ # Builds Configuration Object
+ #
+ # Builds an OpenStruct backed configuration object.
+ #
+ # @param [Hash] config Configuration options hash.
# @option config [ZTK::UI] :ui Instance of ZTK:UI to be used for
# console IO and logging.
- def build_config(configuration={})
- if configuration.is_a?(OpenStruct)
- configuration = configuration.send(:table)
- end
-
- # FIXME: this needs to be refactored into the UI class
+ # @param [Hash] override Override configuration hash.
+ def build_config(config={}, override={})
config = OpenStruct.new({
:ui => ::ZTK::UI.new
- }.merge(configuration))
+ }.merge(hash_config(config)).merge(hash_config(override)))
+ config.ui.logger.debug { "config=#{config.send(:table).inspect}" }
+
config
end
- # Removes all key-value pairs which are not core so values do not bleed
- # into classes they are not meant for.
+ # Hash Configuration
#
- # This method will leave :stdout, :stderr, :stdin and :logger key-values
- # intact, while removing all other key-value pairs.
- def sanitize_config(configuration={})
- if configuration.is_a?(OpenStruct)
- configuration = configuration.send(:table)
+ # Ensure a configuration is of object type Hash. Since we use OpenStructs
+ # we need to convert back to hash from time to time.
+ def hash_config(config={})
+ if config.is_a?(OpenStruct)
+ config.send(:table)
+ else
+ config
end
-
- config = configuration.reject do |key,value|
- !(%w(stdout stderr stdin logger).map(&:to_sym).include?(key))
- end
-
- config
end
# Logs an exception and then raises it.
#
# @param [Logger] logger An instance of a class based off the Ruby
@@ -71,17 +68,14 @@
raise exception, message
end
end
- # @param [Hash] config Configuration options hash.
- # @option config [IO] :stdout Instance of IO to be used for STDOUT.
- # @option config [IO] :stderr Instance of IO to be used for STDERR.
- # @option config [IO] :stdin Instance of IO to be used for STDIN.
- # @option config [Logger] :logger Instance of Logger to be used for logging.
- def initialize(config={})
- @config = Base.build_config(config)
+ # @param [Hash] config Initial configuration hash.
+ # @param [Hash] override Override configuration hash.
+ def initialize(config={}, override={})
+ @config = Base.build_config(config, override)
end
# Configuration OpenStruct accessor method.
#
# If no block is given, the method will return the configuration OpenStruct
@@ -125,10 +119,10 @@
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 <= ZTK::Logger.const_get(log_level.to_s.upcase))
+ elsif (@config.ui.logger.level <= ::Logger.const_get(log_level.to_s.upcase))
@config.ui.logger << ZTK::ANSI.uncolor(yield)
end
end
end