Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Broke core into mixins. Made logger load default automatically. Fixed…

… dependencies. Updated changelog.
  • Loading branch information...
commit 772e14eba713a51a375d2700d51266bfd4f7b43f 1 parent 29bf9dd
@rahmal authored
View
2  ChangeLog
@@ -1,7 +1,7 @@
0.3.3
=====
* Extend object to add short-hand access to config.
-* Moved constants, and class variables to separate modules to decrease core class size.
+* Break rconfig core into separate modules to decrease core class size.
* Fixed a syntax error in when clause of config_hash.rb for ruby 1.9.1 - From dvyjones/rconfig (github).
* Fixed raise on allow_reload method.
* Fixed unit test, rconfig_test.rb.
View
2  lib/rconfig.rb
@@ -40,8 +40,6 @@
require 'rconfig/logger'
require 'rconfig/properties_file_parser'
require 'rconfig/exceptions'
-require 'rconfig/constants'
-require 'rconfig/class_variables'
require 'rconfig/rconfig'
# Create global reference to RConfig instance
View
1  lib/rconfig/logger.rb
@@ -25,6 +25,7 @@ def create_logger options = {}
end
def logger
+ create_logger if self.default_logger.nil?
self.default_logger
end
View
8 lib/rconfig/mixins.rb
@@ -1,8 +1,10 @@
##
# Copyright (c) 2009 Rahmal Conda <rahmal@gmail.com>
#
-require 'rconfig/mixins/constants'
+require 'rconfig/mixins/callbacks'
require 'rconfig/mixins/class_variables'
-require 'rconfig/mixins/config_path'
+require 'rconfig/mixins/config_paths'
+require 'rconfig/mixins/constants'
+require 'rconfig/mixins/loader'
require 'rconfig/mixins/overlay'
-require 'rconfig/mixins/load'
+require 'rconfig/mixins/utils'
View
51 lib/rconfig/mixins/callbacks.rb
@@ -0,0 +1,51 @@
+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
+ # name :ANY will register a callback for any config file change.
+ #
+ # Example:
+ #
+ # class MyClass
+ # @@my_config = { }
+ # RConfig.on_load(:cache) do
+ # @@my_config = { }
+ # end
+ # def my_config
+ # @@my_config ||= something_expensive_thing_on_config(RConfig.cache.memory_limit)
+ # end
+ # end
+ #
+ 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 |
+ name = name.to_s
+ (@@on_load[name] ||= [ ]) << proc
+ end
+ end
+
+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)
+ callbacks.uniq!
+ logger.debug "fire_on_load(#{name.inspect}): callbacks[#{callbacks.inspect}]" unless callbacks.empty?
+ callbacks.each{|cb| cb.call()}
+ end
+
+ end
+end
View
66 lib/rconfig/mixins/load.rb → lib/rconfig/mixins/loader.rb
@@ -1,45 +1,5 @@
module Mixins
- module Load
-
- ##
- # 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
- # name :ANY will register a callback for any config file change.
- #
- # Example:
- #
- # class MyClass
- # @@my_config = { }
- # RConfig.on_load(:cache) do
- # @@my_config = { }
- # end
- # def my_config
- # @@my_config ||= something_expensive_thing_on_config(RConfig.cache.memory_limit)
- # end
- # end
- #
- 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 |
- name = name.to_s
- (@@on_load[name] ||= [ ]) << proc
- end
- end
-
-
- ##
- # Sets the flag indicating whether or not reload should be executed.
- def self.allow_reload=(reload)
- raise ArgumentError, 'Argument must be true or false.' unless [true, false].include?(reload)
- @@reload_disabled = (not reload)
- end
-
+ module Loader
##
# Flag indicating whether or not reload should be executed.
@@ -47,7 +7,6 @@ def self.reload?
!@@reload_disabled
end
-
##
# Sets the number of seconds between reloading of config files
# and automatic reload checks. Defaults to 5 minutes.
@@ -70,6 +29,14 @@ def self.reload(force = false)
nil
end
+ ##
+ # Sets the flag indicating whether or not reload should be executed.
+ def self.allow_reload=(reload)
+ raise ArgumentError, 'Argument must be true or false.' unless [true, false].include?(reload)
+ @@reload_disabled = (not reload)
+ end
+
+
##
# Disables any reloading of config,
# executes &block,
@@ -89,20 +56,5 @@ def self.disable_reload(&block)
result
end
-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)
- callbacks.uniq!
- logger.debug "fire_on_load(#{name.inspect}): callbacks[#{callbacks.inspect}]" unless callbacks.empty?
- callbacks.each{|cb| cb.call()}
- end
-
end
end
View
107 lib/rconfig/mixins/utils.rb
@@ -0,0 +1,107 @@
+module Mixins
+ module Utils
+
+ ##
+ # Helper method for white-box testing and debugging.
+ # Sets the flag indicating whether or not to log
+ # errors and application run-time information.
+ def self.log_level=(x)
+ logger.level = level unless level.nil?
+ end
+
+ def self.log_level
+ logger.level
+ end
+
+ ##
+ # Helper method for white-box testing and debugging.
+ # Sets a hash of each file that has been loaded.
+ def self.config_file_loaded=(x)
+ @@config_file_loaded = x
+ end
+
+
+ ##
+ # Helper method for white-box testing and debugging.
+ # Returns a hash of each file that has been loaded.
+ def self.config_file_loaded
+ @@config_file_loaded
+ end
+
+protected
+
+ ##
+ # Parses file based on file type.
+ #
+ def self.parse_file(conf_file, ext)
+ hash = case ext
+ when *YML_FILE_TYPES
+ YAML::load(conf_file)
+ when *XML_FILE_TYPES
+ Hash.from_xml(conf_file)
+ when *CNF_FILE_TYPES
+ PropertiesFileParser.parse(conf_file)
+ else
+ raise ConfigError, "Unknown File type:#{ext}"
+ end
+ hash.freeze
+ end
+
+ ##
+ # Returns a merge of hashes.
+ #
+ def self.merge_hashes(hashes)
+ hashes.inject({ }) { | n, h | n.weave(h, true) }
+ end
+
+
+ ##
+ # Recursively makes hashes into frozen IndifferentAccess ConfigFakerHash
+ # Arrays are also traversed and frozen.
+ #
+ def self.make_indifferent(x)
+ case x
+ when Hash
+ unless x.frozen?
+ x.each_pair do | k, v |
+ x[k] = make_indifferent(v)
+ end
+ x = ConfigHash.new.merge!(x).freeze
+ end
+ logger.debug "make_indefferent: x = #{x.inspect}:#{x.class}"
+ when Array
+ unless x.frozen?
+ x.collect! do | v |
+ make_indifferent(v)
+ end
+ x.freeze
+ end
+ # Freeze Strings.
+ when String
+ x.freeze
+ end
+ x
+ end
+
+
+ ##
+ # Flushes cached config data. This should avoided in production
+ # environments, if possible.
+ def self.flush_cache
+ @@suffixes = { }
+ @@cache = { }
+ @@cache_files = { }
+ @@cache_hash = { }
+ @@last_auto_check = { }
+ self
+ end
+
+ ##
+ # Get complete file name, including file path for the given config name
+ # and directory.
+ def self.filename_for_name(name, dir = config_paths[0], ext = :yml)
+ File.join(dir, "#{name}.#{ext}")
+ end
+
+ end
+end
View
141 lib/rconfig/rconfig.rb
@@ -61,15 +61,19 @@
class RConfig
include Singleton,
Mixins::Constants, Mixins::ClassVariables,
- Mixins::ConfigPaths, Mixins::Overlay
+ Mixins::ConfigPaths, Mixins::Overlay,
+ Mixins::Loader, Mixins::Callbacks, Mixins::Utils
- create_logger
-
##
# Convenience method to initialize necessary fields including,
# config path(s), overlay, allow_reload, and log_level, all at
# one time.
+ # Examples:
+ # RConfig.initialize(:config, 'en_US', true, :warn)
+ # - or -
+ # RConfig.initialize(:paths => ['config', 'var/config'], :reload => false)
def self.initialize(*args)
+ logger.info{"Initialing RConfig"}
case args[0]
when Hash
params = args[0].symbolize_keys
@@ -80,10 +84,12 @@ def self.initialize(*args)
else
paths, overlay, reload, loglvl = *args
end
+ logger.debug{"PATHS: #{paths}\nOVERLAY: #{overlay}\nRELOAD: #{reload}\nLOG_LEVEL: #{loglvl}"}
self.config_paths = paths
self.overlay = overlay
self.allow_reload = reload
self.log_level = loglvl
+ true
end
##
@@ -167,24 +173,6 @@ def self.load_config_files(name, force=false)
end
- ##
- # Parses file based on file type.
- #
- def self.parse_file(conf_file, ext)
- hash = case ext
- when *YML_FILE_TYPES
- YAML::load(conf_file)
- when *XML_FILE_TYPES
- Hash.from_xml(conf_file)
- when *CNF_FILE_TYPES
- PropertiesFileParser.parse(conf_file)
- else
- raise ConfigError, "Unknown File type:#{ext}"
- end
- hash.freeze
- end
-
-
##
# Returns a list of all relevant config files as specified
# by _suffixes list.
@@ -215,7 +203,6 @@ def self.get_config_files(name)
files
end
-
##
# Return the config file information for the given config name.
#
@@ -304,43 +291,6 @@ def self.config_has_changed?(name)
end
- ##
- # Returns a merge of hashes.
- #
- def self.merge_hashes(hashes)
- hashes.inject({ }) { | n, h | n.weave(h, true) }
- end
-
-
- ##
- # Recursively makes hashes into frozen IndifferentAccess ConfigFakerHash
- # Arrays are also traversed and frozen.
- #
- def self.make_indifferent(x)
- case x
- when Hash
- unless x.frozen?
- x.each_pair do | k, v |
- x[k] = make_indifferent(v)
- end
- x = ConfigHash.new.merge!(x).freeze
- end
- logger.debug "make_indefferent: x = #{x.inspect}:#{x.class}"
- when Array
- unless x.frozen?
- x.collect! do | v |
- make_indifferent(v)
- end
- x.freeze
- end
- # Freeze Strings.
- when String
- x.freeze
- end
-
- x
- end
-
##
# This method provides shorthand to retrieve confiuration data that
@@ -374,7 +324,6 @@ def self.[](key, file=:application)
self.get_config_file(file)[key] || ENV[key.to_s.upcase]
end
-
##
# Get the value specified by the args, in the file specified by th name
#
@@ -394,7 +343,6 @@ def self.with_file(name, *args)
# logger.debug "with_file(#{name.inspect}, #{args.inspect}) => #{result.inspect}"; result
end
-
##
# Get the merged config hash.
# Will auto check every 5 minutes, for longer running apps.
@@ -415,15 +363,12 @@ def self.get_config_file(name)
result
end
-
-
##
# Creates a dottable hash for all Hash objects, recursively.
def self.create_dottable_hash(value)
make_indifferent(value)
end
-
##
# Short-hand access to config file by its name.
#
@@ -438,7 +383,6 @@ def self.method_missing(method, *args)
value
end
-
##
# Creating an instance isn't required. But if you just have to a reference to RConfig
# you can get it using RConfig.instance. It's a singleton, so there's never more than
@@ -456,7 +400,6 @@ def method_missing(method, *args)
self.class.method_missing(method, *args)
end
-
##
# Creating an instance isn't required. But if you just have to a reference to RConfig
# you can get it using RConfig.instance. It's a singleton, so there's never more than
@@ -473,70 +416,4 @@ def [](key)
self.class[key]
end
-
- ##
- # Helper method for white-box testing and debugging.
- # Sets the flag indicating whether or not to log
- # errors and application run-time information.
- def self.log_level=(x)
- logger.level = level unless level.nil?
- end
-
- def self.log_level
- logger.level
- end
-
- ##
- # Helper method for white-box testing and debugging.
- # Sets a hash of each file that has been loaded.
- def self.config_file_loaded=(x)
- @@config_file_loaded = x
- end
-
-
- ##
- # Helper method for white-box testing and debugging.
- # Returns a hash of each file that has been loaded.
- def self.config_file_loaded
- @@config_file_loaded
- end
-
-
-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)
- callbacks.uniq!
- logger.debug "fire_on_load(#{name.inspect}): callbacks[#{callbacks.inspect}]" unless callbacks.empty?
- callbacks.each{|cb| cb.call()}
- end
-
-
- ##
- # Flushes cached config data. This should avoided in production
- # environments, if possible.
- def self.flush_cache
- @@suffixes = { }
- @@cache = { }
- @@cache_files = { }
- @@cache_hash = { }
- @@last_auto_check = { }
- self
- end
-
-
- ##
- # Get complete file name, including file path for the given config name
- # and directory.
- def self.filename_for_name(name, dir = config_paths[0], ext = :yml)
- File.join(dir, "#{name}.#{ext}")
- end
-
end # class RConfig
Please sign in to comment.
Something went wrong with that request. Please try again.