Permalink
Browse files

Retooling of how the session-store stuff works...

so that it actually works.  
And it does.
Awesome.
  • Loading branch information...
1 parent c77d5c7 commit fcd4ab21150bbefa064e79e483718864de7ea754 @jkrall committed Sep 11, 2010
View
@@ -121,6 +121,7 @@ Gem::Specification.new do |s|
"spec/analytical_spec.rb",
"spec/spec_helper.rb"
]
+ # s.add_dependency('')
if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
View
@@ -40,6 +40,7 @@ def analytical
if options[:disable_if].call(self)
options[:modules] = options[:development_modules]
end
+ options[:session] = session if options[:use_session_store]
if analytical_is_robot?(request.user_agent)
options[:modules] = []
end
View
@@ -1,4 +1,10 @@
module Analytical
+ module Modules
+ class DummyModule
+ include Analytical::Modules::Base
+ def method_missing(method, *args, &block); nil; end
+ end
+ end
class Api
attr_accessor :options, :modules
@@ -12,6 +18,7 @@ def initialize(options={})
h[m] = "Analytical::Modules::#{m.to_s.camelize}".constantize.new(module_options)
h
end
+ @dummy_module = Analytical::Modules::DummyModule.new
end
#
@@ -20,10 +27,13 @@ def initialize(options={})
# analytical.console.go 'make', :some=>:cookies
#
def method_missing(method, *args, &block)
- if @modules.keys.include?(method.to_sym)
- @modules[method.to_sym]
+ method = method.to_sym
+ if @modules.keys.include?(method)
+ @modules[method]
+ elsif available_modules.include?(method)
+ @dummy_module
else
- process_command method.to_sym, *args
+ process_command method, *args
end
end
@@ -95,6 +105,13 @@ def init_javascript(location)
m.init_javascript(location)
end.compact.join("\n")
end
+
+ def available_modules
+ Dir.glob(File.dirname(__FILE__)+'/modules/*.rb').collect do |f|
+ File.basename(f).sub(/.rb/,'').to_sym
+ end - [:base]
+ end
+
end
end
@@ -1,6 +1,6 @@
module Analytical
class CommandStore
- attr_reader :commands
+ attr_accessor :commands
def initialize(initial_list=nil)
@commands = initial_list || []
@@ -2,13 +2,13 @@ module Analytical
module Modules
module Base
attr_reader :tracking_command_location, :options, :initialized
- attr_accessor :commands
+ attr_reader :command_store
def initialize(_options={})
@options = _options
@tracking_command_location = :body_prepend
@initialized = false
- @commands = @options[:session_store] || Analytical::CommandStore.new
+ @command_store = @options[:session_store] || Analytical::CommandStore.new
end
#
@@ -39,16 +39,16 @@ def initialize(_options={})
def queue(*args)
if args.first==:identify
- @commands.unshift args
+ @command_store.unshift args
else
- @commands << args
+ @command_store << args
end
end
def process_queued_commands
- command_strings = @commands.collect do |c|
+ command_strings = @command_store.collect do |c|
send(*c) if respond_to?(c.first)
end.compact
- @commands = []
+ @command_store.flush
command_strings
end
@@ -20,13 +20,13 @@ def init_javascript(location)
HTML
identify_commands = []
- @commands.each do |c|
+ @command_store.commands.each do |c|
if c[0] == :identify
identify_commands << identify(*c[1..-1])
end
end
js = identify_commands.join("\n") + "\n" + js
- @commands = @commands.delete_if {|c| c[0] == :identify }
+ @command_store.commands = @command_store.commands.delete_if {|c| c[0] == :identify }
js
end
@@ -5,16 +5,19 @@ class SessionCommandStore
def initialize(session, module_key, initial_list=nil)
@session = session
@module_key = module_key
- ensure_session_array!(initial_list)
+ @session_key = ('analytical_'+module_key.to_s).to_sym
+ ensure_session_setup!(initial_list)
+ end
+
+ def assign(v)
+ self.commands = v
end
def commands
- ensure_session_setup!
- @session[:analytical][@module_key]
+ @session[@session_key]
end
def commands=(v)
- ensure_session_setup!
- @session[:analytical][@module_key] = v
+ @session[@session_key] = v
end
def flush
@@ -23,18 +26,13 @@ def flush
# Pass any array methods on to the internal array
def method_missing(method, *args, &block)
- ensure_session_array!
commands.send(method, *args, &block)
end
private
- def ensure_session_setup!
- @session[:analytical] ||= {}
- end
-
- def ensure_session_array!(initial_list=nil)
- self.commands ||= initial_list || []
+ def ensure_session_setup!(initial_list=nil)
+ self.commands ||= (initial_list || [])
end
end
@@ -9,26 +9,26 @@ class BaseApiDummy
describe '#queue' do
before(:each) do
@api = BaseApiDummy.new(:parent=>mock('parent'))
- @api.commands = [:a, :b, :c]
+ @api.command_store.commands = [:a, :b, :c]
end
describe 'with an identify command' do
it 'should store it at the head of the command list' do
@api.queue :identify, 'someone', {:some=>:args}
- @api.commands.should == [[:identify, 'someone', {:some=>:args}], :a, :b, :c]
+ @api.command_store.commands.should == [[:identify, 'someone', {:some=>:args}], :a, :b, :c]
end
end
describe 'with any other command' do
it 'should store it at the end of the command list' do
@api.queue :other, {:some=>:args}
- @api.commands.should == [:a, :b, :c, [:other, {:some=>:args}]]
+ @api.command_store.commands.should == [:a, :b, :c, [:other, {:some=>:args}]]
end
end
end
describe '#process_queued_commands' do
before(:each) do
@api = BaseApiDummy.new(:parent=>mock('parent'))
- @api.commands = [[:a, 1, 2, 3], [:b, {:some=>:args}]]
+ @api.command_store.commands = [[:a, 1, 2, 3], [:b, {:some=>:args}]]
@api.stub!(:a).and_return('a')
@api.stub!(:b).and_return('b')
end
@@ -42,10 +42,10 @@ class BaseApiDummy
end
it 'should clear the commands list' do
@api.process_queued_commands
- @api.commands == []
+ @api.command_store.commands == []
end
it "should not store an unrecognized command" do
- @api.commands << [:c, 1]
+ @api.command_store.commands << [:c, 1]
@api.process_queued_commands.should == ['a','b']
end
end
@@ -104,7 +104,7 @@ class BaseApiDummy
end
it 'should use the session_store' do
@api = BaseApiDummy.new :session_store=>@store
- @api.commands.should == @store
+ @api.command_store.should == @store
end
end
end
@@ -10,12 +10,12 @@
it 'should add elements' do
@store = Analytical::SessionCommandStore.new @session, :some_module, ['a']
@store << 'b'
- @session[:analytical][:some_module].should == ['a', 'b']
+ @session[:analytical_some_module].should == ['a', 'b']
end
it 'should unshift elements' do
@store = Analytical::SessionCommandStore.new @session, :some_module, ['b']
@store.unshift 'a'
- @session[:analytical][:some_module].should == ['a', 'b']
+ @session[:analytical_some_module].should == ['a', 'b']
end
it 'should iterate over elements' do
@store = Analytical::SessionCommandStore.new @session, :some_module, ['a', 'b']
@@ -30,7 +30,7 @@
it 'should set up the :analytical session hash' do
@store = Analytical::SessionCommandStore.new @session, :some_module, ['a', 'b']
- @session[:analytical].should_not be_nil
+ @session[:analytical_some_module].should_not be_nil
end
describe 'when flushing' do
@@ -42,7 +42,7 @@
it 'should empty the session key' do
@store = Analytical::SessionCommandStore.new @session, :some_module, ['a', 'b']
@store.flush
- @session[:analytical][:some_module].should == []
+ @session[:analytical_some_module].should == []
end
end
end

0 comments on commit fcd4ab2

Please sign in to comment.