Permalink
Browse files

Reformatted code

  • Loading branch information...
1 parent 772e14e commit 9f9d13c98ab489517f2ced3d4e162c1954e9a414 @rahmal committed Apr 29, 2010
@@ -38,20 +38,20 @@ def dup
# Depending on what kind of parameter the method being mocked out is going
# to be called with, define in the YAML file either a string or a symbol.
# This also works inside the composite array keys.
- def method_missing(method, *args)
+ def method_missing(method, * args)
method = method.to_s
value = self[method]
- case args.size
- when 0
- # e.g.: RConfig.application.method
- ;
- when 1
- # e.g.: RConfig.application.method(one_arg)
- value = value.send(args[0])
- else
- # e.g.: RConfig.application.method(arg_one, args_two, ...)
- value = value[args]
- end
+ case args.size
+ when 0
+ # e.g.: RConfig.application.method
+ ;
+ when 1
+ # e.g.: RConfig.application.method(one_arg)
+ value = value.send(args[0])
+ else
+ # e.g.: RConfig.application.method(arg_one, args_two, ...)
+ value = value[args]
+ end
# value = convert_value(value)
value
@@ -72,7 +72,7 @@ def [](key)
##
# Allow hash.default => hash['default']
# without breaking Hash's usage of default(key)
- @@no_key = [ :no_key ] # magically unique value.
+ @@no_key = [:no_key] # magically unique value.
def default(key = @@no_key)
key = key.to_s if key.is_a?(Symbol)
key == @@no_key ? self['default'] : default_Hash(key == @@no_key ? nil : key)
@@ -103,5 +103,5 @@ def convert_value(value)
# STDERR.puts "convert_value(#{value.inspect}:#{value.class})"
value.class == Hash ? self.class.new(value).freeze : value
end
-
+
end # class ConfigHash
@@ -23,68 +23,68 @@ def weave(other_hash, clobber = false)
self_dup[key] =
- if self_node = self_dup[key]
+ if self_node = self_dup[key]
- case self_node
- when Hash
+ case self_node
+ when Hash
- # hash1, hash2 => hash3 (recursive +)
- if other_node.is_a?(Hash)
+ # hash1, hash2 => hash3 (recursive +)
+ if other_node.is_a?(Hash)
- self_node.weave(other_node, clobber)
+ self_node.weave(other_node, clobber)
- # hash, array => error (Can't weave'em, must clobber.)
- elsif other_node.is_a?(Array) && !clobber
+ # hash, array => error (Can't weave'em, must clobber.)
+ elsif other_node.is_a?(Array) && !clobber
- raise(ArgumentError, "RConfig: (Hash#weave) Can't weave Hash and Array")
+ raise(ArgumentError, "RConfig: (Hash#weave) Can't weave Hash and Array")
- # hash, array => hash[key] = array
- # hash, value => hash[key] = value
- else
- other_node
- end
+ # hash, array => hash[key] = array
+ # hash, value => hash[key] = value
+ else
+ other_node
+ end
- when Array
-
- # array, hash => array << hash
- # array1, array2 => array1 + array2
- # array, value => array << value
- unless clobber
- case other_node
- when Hash
- self_node << other_node
- when Array
- self_node + other_node
- else
- self_node << other_node
- end
-
- # array, hash => hash
- # array1, array2 => array2
- # array, value => value
- else
- other_node
- end
+ when Array
- else
+ # array, hash => array << hash
+ # array1, array2 => array1 + array2
+ # array, value => array << value
+ unless clobber
+ case other_node
+ when Hash
+ self_node << other_node
+ when Array
+ self_node + other_node
+ else
+ self_node << other_node
+ end
- # value, array => array.unshift(value)
- if other_node.is_a?(Array) && !clobber
- other_node.unshift(self_node)
+ # array, hash => hash
+ # array1, array2 => array2
+ # array, value => value
+ else
+ other_node
+ end
- # value1, value2 => value2
- else
- other_node
- end
+ else
- end # case self_node
+ # value, array => array.unshift(value)
+ if other_node.is_a?(Array) && !clobber
+ other_node.unshift(self_node)
- # Target hash didn't have a node matching the key,
- # so just add it from the source hash.
- # !self_dup.has_key?(key) => self_dup.add(key, other_node)
- else
- other_node
- end
+ # value1, value2 => value2
+ else
+ other_node
+ end
+
+ end # case self_node
+
+ # Target hash didn't have a node matching the key,
+ # so just add it from the source hash.
+ # !self_dup.has_key?(key) => self_dup.add(key, other_node)
+ else
+ other_node
+ end
} # other_hash.each
@@ -6,8 +6,8 @@ class Object
# @people[:email] if @people
# - vs -
# @person.try(:name)
- def try(method, *args)
- result = send(method, *args) if respond_to?(method)
+ def try(method, * args)
+ result = send(method, * args) if respond_to?(method)
result = send(:[], method) if result.nil? && respond_to?(:[])
result
end
@@ -39,7 +39,7 @@ def try(method, *args)
# the class (RConfig).
#
def config
- this_config = $config.send(self.class.name.underscore.to_sym)
+ this_config = $config.send(self.class.name.underscore.to_sym)
this_config.blank? ? $config : this_config
end
@@ -5,8 +5,10 @@
#
#--
# General error in config initialization or operation.
-class ConfigError < StandardError; end
+class ConfigError < StandardError;
+end
# Config path(s) are not set, don't exist, or Invalid in some manner
-class InvalidConfigPathError < ConfigError; end
+class InvalidConfigPathError < ConfigError;
+end
View
@@ -7,20 +7,20 @@
#
module DefaultLogger
- module ClassMethods
-
+ module ClassMethods
+
def create_logger options = {}
class_inheritable_accessor :default_logger, :options
self.options = options || {}
- logger = Logger.new( check_options(:file, STDOUT) )
- logger.level = check_options(:level, ENV['LOG_LEVEL'] || Logger::INFO)
+ logger = Logger.new(check_options(:file, STDOUT))
+ logger.level = check_options(:level, ENV['LOG_LEVEL'] || Logger::INFO)
logger.datetime_format = check_options(:format, "%Y-%m-%d %H:%M:%S")
logger.progname = check_options(:app_name, 'RConfig')
-
+
self.default_logger = logger
-
+
include DefaultLogger::InstanceMethods
end
@@ -35,13 +35,13 @@ def check_options key, default_value=nil
end
end
-
+
module InstanceMethods
- def logger
- self.class.logger
- end
+ def logger
+ self.class.logger
+ end
end
-
+
end
Object.class_eval { extend DefaultLogger::ClassMethods }
@@ -1,6 +1,6 @@
module Mixins
module Callbacks
-
+
##
# Register a callback when a config has been reloaded. If no config name
# is specified, the callback will be registered under the name :ANY. The
@@ -18,33 +18,33 @@ module Callbacks
# end
# end
#
- def self.on_load(*args, &blk)
+ def self.on_load(* args, & blk)
args << :ANY if args.empty?
proc = blk.to_proc
# Call proc on registration.
proc.call()
# Register callback proc.
- args.each do | name |
+ args.each do |name|
name = name.to_s
- (@@on_load[name] ||= [ ]) << proc
+ (@@on_load[name] ||= []) << proc
end
end
-protected
+ protected
##
# Executes all of the reload callbacks registered to the specified config name,
# and all of the callbacks registered to run on any config, as specified by the
# :ANY symbol.
def self.fire_on_load(name)
callbacks =
- (@@on_load['ANY'] || EMPTY_ARRAY) +
- (@@on_load[name] || EMPTY_ARRAY)
+ (@@on_load['ANY'] || EMPTY_ARRAY) +
+ (@@on_load[name] || EMPTY_ARRAY)
callbacks.uniq!
- logger.debug "fire_on_load(#{name.inspect}): callbacks[#{callbacks.inspect}]" unless callbacks.empty?
- callbacks.each{|cb| cb.call()}
+ logger.debug "fire_on_load(#{name.inspect}): callbacks[#{callbacks.inspect}]" unless callbacks.empty?
+ callbacks.each { |cb| cb.call() }
end
end
@@ -16,24 +16,27 @@ def self.config_paths=(paths)
paths = paths.split(/#{path_sep}+/)
end
unless paths.is_a? Array
- raise ArgumentError,
- "Path(s) must be a String or an Array [#{paths.inspect}]"
+ raise ArgumentError,
+ "Path(s) must be a String or an Array [#{paths.inspect}]"
end
if paths.empty?
- raise ArgumentError,
- "Must provide at least one paths: [#{paths.inspect}]"
+ raise ArgumentError,
+ "Must provide at least one paths: [#{paths.inspect}]"
end
paths.all? do |dir|
dir = CONFIG_ROOT if dir == 'CONFIG_ROOT'
unless File.directory?(dir)
- raise InvalidConfigPathError,
- "This directory is invalid: [#{dir.inspect}]"
+ raise InvalidConfigPathError,
+ "This directory is invalid: [#{dir.inspect}]"
end
end
reload
@@config_paths = paths
end
- class << self; alias_method :set_config_paths, :config_paths= end
+
+ class << self;
+ alias_method :set_config_paths, :config_paths=
+ end
##
# Adds the specified path to the list of directories to search for
@@ -42,19 +45,22 @@ class << self; alias_method :set_config_paths, :config_paths= end
# If reload is disabled, it can onle be set once.
def self.set_config_path path
return if @@reload_disabled && config_paths_set?
- return unless path.is_a?(String) # only allow string argument
- path_sep = (path =~ /;/) ? ';' : ':' # if string contains multiple paths
- path = path.split(/#{path_sep}+/)[0] # only accept first one.
+ return unless path.is_a?(String) # only allow string argument
+ path_sep = (path =~ /;/) ? ';' : ':' # if string contains multiple paths
+ path = path.split(/#{path_sep}+/)[0] # only accept first one.
- if @@config_paths.blank?
+ if @@config_paths.blank?
set_config_paths(path)
- else
+ else
config_paths << path if File.directory?(path)
reload
@@config_paths
- end
+ end
+ end
+
+ class << self;
+ alias_method :add_config_path, :set_config_path
end
- class << self; alias_method :add_config_path, :set_config_path end
##
# Returns a list of directories to search for
@@ -76,26 +82,26 @@ def self.config_paths
begin
config_paths = ENV['CONFIG_PATH']
rescue
- logger.error {
+ logger.error {
"Forget something? No config paths! ENV['CONFIG_PATH'] is not set.\n" +
- "Hint: Use config_paths= or set_config_path."
+ "Hint: Use config_paths= or set_config_path."
}
end
begin
- config_paths = [CONFIG_ROOT]
+ config_paths = [CONFIG_ROOT]
rescue
- logger.error {
+ logger.error {
"Forget something? No config paths! CONFIG_ROOT is not set.\n" +
- "Hint: Use config_paths= or set_config_path."
+ "Hint: Use config_paths= or set_config_path."
}
end
if @@config_paths.blank?
- raise InvalidConfigPathError,
- "Forget something? No config paths!\n" +
- "Niether ENV['CONFIG_PATH'] or CONFIG_ROOT is set.\n" +
- "Hint: Use config_paths= or set_config_path."
+ raise InvalidConfigPathError,
+ "Forget something? No config paths!\n" +
+ "Niether ENV['CONFIG_PATH'] or CONFIG_ROOT is set.\n" +
+ "Hint: Use config_paths= or set_config_path."
end
@@config_paths
Oops, something went wrong.

0 comments on commit 9f9d13c

Please sign in to comment.