Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Handle missing module names #37

Merged
merged 7 commits into from almost 2 years ago

3 participants

André Arko Kevin Menard Joshua Krall
André Arko

This pull request adds a rescue block to the code that loads modules based on the config. If a module doesn't exist, it explains why there is an error, and suggests that you fix your analytical.yml file.

I meant to just fix this to handle missing module names better, but I wound up accidentally rewriting the config file loading code. If you want them as separate pull requests, just let me know.

Joshua Krall jkrall merged commit 58aa38d into from
Joshua Krall jkrall closed this
Kevin Menard

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 32 additions and 22 deletions. Show diff stats Hide diff stats

  1. +21 19 lib/analytical.rb
  2. +11 3 lib/analytical/api.rb
40 lib/analytical.rb
@@ -6,27 +6,29 @@
6 6
7 7 module Analytical
8 8
9   - def analytical(options={})
10   - self.analytical_options = options
  9 + def analytical(options = {})
  10 + config = Analytical.config(options[:config])
  11 + self.analytical_options = options.reverse_merge(config)
  12 + end
11 13
12   - config_options = { :modules => [] }
13   - File.open("#{::Rails.root}/config/analytical.yml") do |f|
14   - file_options = YAML::load(ERB.new(f.read).result).symbolize_keys
15   - env = (::Rails.env || :production).to_sym
16   - file_options = file_options[env] if file_options.has_key?(env)
17   - file_options.each do |k, v|
18   - if v.respond_to?(:symbolize_keys)
19   - # module configuration
20   - config_options[k.to_sym] = v.symbolize_keys
21   - config_options[:modules] << k.to_sym unless options && options[:modules]
22   - else
23   - # regular option
24   - config_options[k.to_sym] = v
25   - end
26   - end if file_options
27   - end if File.exists?("#{::Rails.root}/config/analytical.yml")
  14 + def self.config(path = nil)
  15 + path = Pathname.new(path || ::Rails.root.join("config/analytical.yml"))
  16 + return {} unless path.exist?
  17 +
  18 + # Only read the config from any given file one time
  19 + @configs ||= {}
  20 + @configs[path] ||= begin
  21 + # Read the config out of the file
  22 + config = YAML.load(path.read).with_indifferent_access
28 23
29   - self.analytical_options = self.analytical_options.reverse_merge config_options
  24 + # Pull out the correct environment (or toplevel if there isn't an env)
  25 + env = ::Rails.env || :production
  26 + config = config[env] if config.has_key?(env)
  27 +
  28 + # List the modules that were configured
  29 + config = (config || {}).reverse_merge(:modules => [])
  30 + config.each{|k, v| config[:modules] << k.to_sym if v.is_a?(Hash) }
  31 + end
30 32 end
31 33
32 34 module InstanceMethods
14 lib/analytical/api.rb
@@ -11,16 +11,24 @@ class Api
11 11
12 12 def initialize(options={})
13 13 @options = options
14   - @modules = @options[:modules].inject(ActiveSupport::OrderedHash.new) do |h, m|
  14 + @modules = ActiveSupport::OrderedHash.new
  15 + @options[:modules].each do |m|
15 16 module_options = @options.merge(@options[m] || {})
16 17 module_options.delete(:modules)
17 18 module_options[:session_store] = Analytical::SessionCommandStore.new(@options[:session], m) if @options[:session]
18   - h[m] = "Analytical::Modules::#{m.to_s.camelize}".constantize.new(module_options)
19   - h
  19 + @modules[m] = get_mod(m).new(module_options)
20 20 end
21 21 @dummy_module = Analytical::Modules::DummyModule.new
22 22 end
23 23
  24 + def get_mod(name)
  25 + name = name.to_s.camelize
  26 + "Analytical::Modules::#{name}".constantize
  27 + rescue NameError
  28 + raise "You're trying to configure a module named '#{name}', but " +
  29 + "Analytical doesn't have one. Check your analytical.yml file for typos."
  30 + end
  31 +
24 32 #
25 33 # Catch commands such as :track, :identify and send them on to all of the modules.
26 34 # Or... if a module name is passed, return that module so it can be used directly, ie:

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.