Permalink
Browse files

only read the config file once, clean up a bit

  • Loading branch information...
indirect committed Jan 27, 2012
1 parent 0fcac3d commit 9b0833e86f0a75699fb2f68b85e98267996dcf66
Showing with 20 additions and 19 deletions.
  1. +20 −19 lib/analytical.rb
View
@@ -6,27 +6,28 @@
module Analytical
- def analytical(options={})
- self.analytical_options = options
+ def analytical(options = {})
+ config = Analytical.config
+ self.analytical_options = options.reverse_merge(config)
+ end
- config_options = { :modules => [] }
- File.open("#{::Rails.root}/config/analytical.yml") do |f|
- file_options = YAML::load(ERB.new(f.read).result).symbolize_keys
- env = (::Rails.env || :production).to_sym
- file_options = file_options[env] if file_options.has_key?(env)
- file_options.each do |k, v|
- if v.respond_to?(:symbolize_keys)
- # module configuration
- config_options[k.to_sym] = v.symbolize_keys
- config_options[:modules] << k.to_sym unless options && options[:modules]
- else
- # regular option
- config_options[k.to_sym] = v
- end
- end if file_options
- end if File.exists?("#{::Rails.root}/config/analytical.yml")
+ def self.config
+ path = ::Rails.root.join("config/analytical.yml")
+ return {} unless path.exist?
+
+ # Only read the config one time
+ @configs ||= begin
+ # Read the config out of the file
+ config = YAML.load(path.read).with_indifferent_access
@nirvdrum

nirvdrum Aug 19, 2012

Collaborator

FYI, this line broke the SessionCommandStore. Since this HashWithIndifferentAccess is the base config now and HashWithIndifferentAccess makes copies of any hash value on assignment, assigning @options[:session] to the session actually stores a disassociated, cloned copy.

- self.analytical_options = self.analytical_options.reverse_merge config_options
+ # Pull out the correct environment (or toplevel if there isn't an env)
+ env = ::Rails.env || :production
+ config = config[env] if config.has_key?(env)
+
+ # List the modules that were configured
+ config = config.reverse_merge(:modules => [])
+ config.each{|k, v| config[:modules] << k.to_sym if v.is_a?(Hash) }
+ end
end
module InstanceMethods

0 comments on commit 9b0833e

Please sign in to comment.