Included Modules

Files

RConfig::CoreMethods

Public Instance Methods

[](key, file=:application) click to toggle source

This method provides shorthand to retrieve configuration data that is global in scope, and used on an application or environment-wide level. The default location that it checks is the application file. The application config file is a special config file that should be used for config data that is broad in scope and used throughout the application. Since RConfig gives special regard to the application config file, thought should be given to whatever config information is placed there.

Most config data will be specific to particular part of the application (i.e. database, web service), and should therefore be placed in its own specific config file, such as database.yml, or services.xml

This method also acts as a wrapper for ENV. If no value is returned from the application config, it will also check ENV for a value matching the specified key.

Ex.1 RConfig[:test_mode] =>

       RConfig.application[:test_mode] ||
       RConfig.application.test_mode

Ex.2 RConfig[:web_app_root] => ENV[‘WEB_APP_ROOT’]

NOTE: The application config file can be in any of

      the supported formats (yml, xml, conf, etc.)
     # File lib/rconfig/core_methods.rb, line 224
224:     def [](key, file=:application)
225:       with_file(file, key) || ENV[key.to_s.upcase]
226:     end
check_for_changes(name=nil) click to toggle source

If name is specified, checks that file for changes and reloads it if there are. Otherwise, checks all files in the cache, reloading the changed files.

     # File lib/rconfig/core_methods.rb, line 164
164:     def check_for_changes(name=nil)
165:       changed = []
166:       if name == nil
167:         self.cache_hash.keys.dup.each do |name|
168:           if reload_on_change(name)
169:             changed << name
170:           end
171:         end
172:       else
173:         name = name.to_s
174:         if reload_on_change(name)
175:           changed << name
176:         end
177:       end
178:       logger.debug "check_for_changes(#{name.inspect}) => #{changed.inspect}"
179:       changed
180:     end
config_changed?(name) click to toggle source

Returns whether or not the config for the given config name has changed since it was last loaded.

Returns true if any files for config have changes since last load.

     # File lib/rconfig/core_methods.rb, line 131
131:     def config_changed?(name)
132:       logger.debug "config_changed?(#{name.inspect})"
133:       name = name.to_s
134:       !(self.cache_files[name] === get_config_files(name))
135:     end
config_files(name) click to toggle source

Return the config file information for the given config name.

     # File lib/rconfig/core_methods.rb, line 120
120:     def config_files(name)
121:       self.cache_files[name] ||= get_config_files(name)
122:     end
config_for(name) click to toggle source

Get a hash of merged config data. Will auto check every 5 minutes, for longer running apps, unless reload is disabled.

     # File lib/rconfig/core_methods.rb, line 252
252:     def config_for(name)
253:       name = name.to_s
254:       check_for_changes(name) if auto_check?(name)
255:       data = get_config_data(name)
256:       logger.debug "config_for(#{name.inspect}) => #{data.inspect}"
257:       data
258:     end
get_config_data(name) click to toggle source

Get the merged config hash for the named file. Returns a cached indifferent access faker hash merged from all config files for a name.

     # File lib/rconfig/core_methods.rb, line 143
143:     def get_config_data(name)
144:       logger.debug "get_config_data(#{name.inspect})"
145: 
146:       name = name.to_s
147:       unless result = self.cache_hash[name]
148:         result = self.cache_hash[name] =
149:             make_indifferent(
150:                 merge_hashes(
151:                     load_config_files(name)
152:                 )
153:             )
154:         logger.debug "get_config_data(#{name.inspect}): reloaded"
155:       end
156: 
157:       result
158:     end
get_config_files(name) click to toggle source

Returns a list of all relevant config files as specified by suffixes list. Each element is an Array, containing:

  [ 
    "server",              # The base name of the 
    "server_production",   # The suffixed name
    "/path/to/server.yml", # The absolute path to the file 
    <Wed Apr 09 08:53:14>  # The last modified time of the file or nil, if it doesn't exist.
  ]
     # File lib/rconfig/core_methods.rb, line 95
 95:     def get_config_files(name)
 96:       files = []
 97: 
 98:       self.load_paths.reverse.each do |directory|
 99:         # splatting *suffix allows us to deal with multipart suffixes
100:         name_no_overlay, suffixes = suffixes_for(name)
101:         suffixes.map { |suffix| [name_no_overlay, *suffix].compact.join('_') }.each do |name_with_suffix|
102:           self.file_types.each do |ext|
103:             filename = filename_for_name(name_with_suffix, directory, ext)
104:             if File.exists?(filename)
105:               modified_time = File.stat(filename).mtime
106:               files << [name, name_with_suffix, filename, ext, modified_time]
107:             end
108:           end
109:         end
110:       end
111: 
112:       logger.debug "get_config_files(#{name}) => #{files.select { |x| x[3] }.inspect}"
113: 
114:       files
115:     end
load_config_files(name, force=false) click to toggle source

Get each config file’s yaml hash for the given config name, to be merged later. Files will only be loaded if they have not been loaded before or the files have changed within the last five minutes, or force is explicitly set to true.

    # File lib/rconfig/core_methods.rb, line 14
14:     def load_config_files(name, force=false)
15:       name = name.to_s
16: 
17:       # Return last config file hash list loaded,
18:       # if reload is disabled and files have already been loaded.
19:       return self.cache_config_files[name] if self.reload_disabled? && self.cache_config_files[name]
20: 
21:       logger.info "Loading config files for: #{name}"
22:       logger.debug "load_config_files(#{name.inspect})"
23: 
24: 
25:       now = Time.now
26: 
27:       # Get array of all the existing files file the config name.
28:       config_files = self.get_config_files(name)
29: 
30:       # Get all the data from all yaml files into as configs
31:       configs = config_files.collect do |f|
32:         name, name_with_suffix, filename, ext, modified_time = * f
33: 
34:         # Get the cached file info the specific file, if
35:         # it's been loaded before.
36:         config_data, last_modified, last_loaded = self.cache[filename]
37: 
38:         logger.debug "f = #{f.inspect}\n" +
39:           "cache #{name_with_suffix} filename      = #{filename.inspect}\n" +
40:           "cache #{name_with_suffix} config_data   = #{config_data.inspect}\n" +
41:           "cache #{name_with_suffix} last_modified = #{last_modified.inspect}\n" +
42:           "cache #{name_with_suffix} last_loaded   = #{last_loaded.inspect}\n"
43: 
44:         # Load the file if its never been loaded or its been more than
45:         # so many minutes since last load attempt. (default: 5 minutes)
46:         if config_data.blank? || (now - last_loaded > self.reload_interval)
47:           if force || config_data.blank? || modified_time != last_modified
48: 
49:             logger.debug "modified_time #{name.inspect} #{filename.inspect} " +
50:               "changed #{modified_time != last_modified} : #{modified_time.inspect} #{last_modified.inspect}"
51: 
52:             logger.debug "RConfig: loading #{filename.inspect}"
53: 
54:             config_data = read(filename, name, ext)  # Get contents from config file
55: 
56:             logger.debug "RConfig: loaded #{filename.inspect} => #{config_data.inspect}"
57: 
58:             (self.config_loaded ||= {})[name] = config_files  # add files to the loaded files cache
59: 
60:             self.cache[filename] = [config_data, modified_time, now]  # Save cached config file contents, and modified_time.
61: 
62:             logger.debug "cache[#{filename.inspect}] = #{self.cache[filename].inspect}"
63: 
64:             self.cache_hash[name] = nil # Flush merged hash cache.
65: 
66:             self.cache_files[name] = config_files  # Config files changed or disappeared.
67: 
68:           end # if config_data == nil || (now - last_loaded > self.reload_interval)
69:         end # if force || config_data == nil || modified_time != last_modified
70: 
71:         config_data
72:       end # config_files.collect
73:       configs.compact!
74: 
75:       logger.debug "load_config_files(#{name.inspect}) => #{configs.inspect}"
76: 
77:       # Keep last loaded config files around in case self.reload_dsabled.
78:       self.cache_config_files[name] = configs #unless configs.empty?
79: 
80:       configs
81:     end
method_missing(method, * args) click to toggle source

Short-hand access to config file by its name.

Example:

  RConfig.provider(:foo) => RConfig.with_file(:provider).foo
  RConfig.provider.foo   => RConfig.with_file(:provider).foo
     # File lib/rconfig/core_methods.rb, line 268
268:     def method_missing(method, * args)
269:       value = with_file(method, * args)
270:       logger.debug "#{self}.method_missing(#{method.inspect}, #{args.inspect}) => #{value.inspect}"
271:       value
272:     end
reload_on_change(name) click to toggle source

If config files have changed, caches are flushed, on_load triggers are run.

     # File lib/rconfig/core_methods.rb, line 184
184:     def reload_on_change(name)
185:       logger.debug "reload_on_change(#{name.inspect}), reload_disabled=#{self.reload_disabled?}"
186:       if changed = config_changed?(name) && reload?
187:         if self.cache_hash[name]
188:           flush_cache(name)  # flush cached config values.
189:           fire_on_load(name) # force on_load triggers.
190:         end
191:       end
192:       changed
193:     end
with_file(name, *args) click to toggle source

Get the value specified by the args, in the file specified by th name

     # File lib/rconfig/core_methods.rb, line 231
231:     def with_file(name, *args)
232:       logger.debug "with_file(#{name.inspect}, #{args.inspect})"
233:       result = args.inject(config_for(name)) { |v, i|
234:         logger.debug "v = #{v.inspect}, i = #{i.inspect}"
235:         case v
236:           when Hash
237:             v[i.to_s]
238:           when Array
239:             i.is_a?(Integer) ? v[i] : nil
240:           else
241:             nil
242:         end
243:       }
244:       logger.debug "with_file(#{name.inspect}, #{args.inspect}) => #{result.inspect}"
245:       result
246:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.