Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow vars to use other vars instead of a block

  • Loading branch information...
commit ee53700f8c1b98d2bbef3425383b5bfc52104f9a 1 parent b962cc2
@ace ace authored
Showing with 25 additions and 16 deletions.
  1. +14 −5 lib/omniture_client/base.rb
  2. +11 −11 lib/omniture_client/meta_var.rb
View
19 lib/omniture_client/base.rb
@@ -3,7 +3,7 @@ class Base
include OmnitureClient::ClassLevelInheritableAttributes
inheritable_attributes :meta_vars, :js_vars, :js_events
-
+
DEFAULT_OPTIONS = { :delimiter => ',',
:unique => nil,
:expires_in => 0 }
@@ -14,8 +14,8 @@ def var(name, options={}, &block)
options = DEFAULT_OPTIONS.merge(options)
@meta_vars ||= []
- meta_var = instance_eval("@#{name} ||= OmnitureClient::MetaVar.new('#{name}', '#{options[:delimiter]}', '#{options[:unique]}', #{options[:expires_in]})")
- meta_var.add_var(block)
+ meta_var = instance_eval("@#{name} ||= OmnitureClient::MetaVar.new(name, options)")
+ meta_var.add_var(block || options[:copy])
meta_vars << meta_var unless meta_vars.include?(meta_var)
meta_var
end
@@ -24,7 +24,7 @@ def js_var(&block)
@js_vars ||= []
@js_vars << yield
end
-
+
def event(&block)
@js_events ||= []
@js_events << yield
@@ -39,6 +39,10 @@ def clear_meta_vars
end
end
+ def find_var(name)
+ meta_vars.find{ |meta| meta.name == name }
+ end
+
def for_action(name, &block)
RAILS_DEFAULT_LOGGER.info("name = #{name}")
yield
@@ -63,11 +67,16 @@ def printer
def vars
meta_vars = self.class.meta_vars || []
@vars ||= meta_vars.inject([]) do |vars, meta_var|
- vars << meta_var.value(controller) if meta_var
+ vars << meta_var.value(controller, self) if meta_var
vars
end
end
+ def eval_var(name)
+ meta_var = self.class.find_var(name)
+ meta_var.value(controller, self).value if meta_var
+ end
+
def js_vars
self.class.js_vars || []
end
View
22 lib/omniture_client/meta_var.rb
@@ -3,12 +3,12 @@ class MetaVar
attr_reader :name, :delimiter, :cache_key, :expires_in
attr_accessor :value_procs
- def initialize(name, delimiter, unique, expires_in)
+ def initialize(name, options = {})
@name = name
@value_procs = []
- @delimiter = delimiter
- @cache_key = "omniture/#{name}/#{unique}"
- @expires_in = expires_in
+ @delimiter = options[:delimiter]
+ @cache_key = "omniture/#{name}/#{options[:unique]}"
+ @expires_in = options[:expires_in]
end
def add_var(value_proc)
@@ -16,19 +16,19 @@ def add_var(value_proc)
end
# wrap up the value in a Var object and cache if needed
- def value(scope)
+ def value(scope, reporter)
if @expires_in > 0
Rails.cache.fetch(@cache_key, :expires_in => @expires_in) do
- return_var(scope)
+ return_var(scope, reporter)
end
else
- return_var(scope)
+ return_var(scope, reporter)
end
end
-
- def return_var(scope)
- Var.new(name, value_procs.map{ |p| scope.instance_eval(&p) }.flatten.uniq.join(delimiter))
+
+ def return_var(scope, reporter)
+ Var.new(name, value_procs.map{ |p| p.is_a?(Symbol) ? reporter.eval_var(p) : scope.instance_eval(&p) }.flatten.uniq.join(delimiter))
end
-
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.