Skip to content

Commit

Permalink
Refactored gem. Now it uses meta_vars gem
Browse files Browse the repository at this point in the history
  • Loading branch information
eLafo committed Oct 21, 2011
1 parent 92be13e commit 5f5a472
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 290 deletions.
31 changes: 0 additions & 31 deletions lib/class_level_inheritable_attributes.rb

This file was deleted.

5 changes: 1 addition & 4 deletions lib/omniturize.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
require 'meta_vars'
require "omniturize/version"
require File.dirname(__FILE__) + '/class_level_inheritable_attributes'
require File.dirname(__FILE__) + '/omniturize/printer'
require File.dirname(__FILE__) + '/omniturize/base'
require File.dirname(__FILE__) + '/omniturize/var'
require File.dirname(__FILE__) + '/omniturize/meta_js'
require File.dirname(__FILE__) + '/omniturize/meta_var'
require 'cgi'

module Omniturize
Expand Down
99 changes: 5 additions & 94 deletions lib/omniturize/base.rb
Original file line number Diff line number Diff line change
@@ -1,110 +1,21 @@
module Omniturize
class Base

include Omniturize::ClassLevelInheritableAttributes
include Omniturize::Printer
inheritable_attributes :meta_vars, :meta_js_events, :meta_js_snippets

attr_reader :controller
include MetaVars

@meta_vars = []
@meta_js_events = []
@meta_js_snippets = []
has_meta_var :var, :default_namespace => 'default', :inheritable => true
has_meta_var :event, :default_namespace => 'default', :inheritable => true
has_meta_var :js_snippet, :default_namespace => 'default', :inheritable => true

attr_reader :controller

class << self
attr_accessor :meta_vars, :meta_js_events, :meta_js_snippets
@@controller = nil

def var(name, options={}, &block)
meta_var = Omniturize::MetaVar.new(name.to_s, options)
meta_var.add_var(block)
meta_vars.insert(0, meta_var) unless meta_vars.include?(meta_var)
meta_var
end

def event(options = {}, &block)
meta_js(meta_js_events, block, options)
end

def js_snippet(options = {}, &block)
meta_js(meta_js_snippets, block, options)
end

def find_meta_vars(name, action = nil)
filter_collection_for_action(meta_vars.select{|x|x.matches_name?(name)}, action).uniq_by(:name)
end

def filter_collection_for_action(collection, action)
return collection if action.blank?
collection.select do |element|
element.passes_filter?(action)
end
end

protected

def meta_js(collection, block, options = {})
meta_js = Omniture::MetaJs.new(options)
meta_js.add_js(block)
collection.insert(0, meta_js)
meta_js
end

end

def initialize(controller)
@controller = controller
end

def vars(options = {})
meta_collection_to_values(self.class.meta_vars, options[:action]).uniq_by(:name)
end

def js_events(options = {})
meta_collection_to_values(self.class.meta_js_events, options[:action]).uniq
end

def js_snippets(options = {})
meta_collection_to_values(self.class.meta_js_snippets, options[:action]).uniq
end

def add_var(name, value)
self.class.var(name) do
value
end
end

def find_vars(name, options = {})
meta_vars_to_vars(self.class.find_meta_vars(name, options))
end

def find_var(name, options = {})
find_vars(name, options).first
end

def find_values(name, options = {})
find_vars(name, options).map(&:value)
end

def meta_vars_to_vars(meta_vars)
meta_vars.inject([]) do |vars, meta_var|
vars << (meta_var.value(controller) if meta_var) rescue nil
vars
end
end

protected

def build_js_collection(procs_collection)
procs_collection.inject([]) do |code, js|
code << controller.instance_eval(&js)
code
end
end

def meta_collection_to_values(collection, action = nil)
action.present? ? meta_vars_to_vars(self.class.filter_collection_for_action(collection, action)): meta_vars_to_vars(collection)
end
end
end
56 changes: 0 additions & 56 deletions lib/omniturize/meta_js.rb

This file was deleted.

67 changes: 0 additions & 67 deletions lib/omniturize/meta_var.rb

This file was deleted.

40 changes: 25 additions & 15 deletions lib/omniturize/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,48 @@ def js(options = {})
output = <<-JS
<script type="text/javascript" language="JavaScript" src="#{Omniturize::js_src}"></script>
<script type="text/javascript" language="JavaScript" #{html_options.join(' ')}>
#{js_vars(options)}
#{print_js_events(options)}
#{print_js_snippets(options)}
#{js_vars(options[:action])}
#{print_js_events(options[:action])}
#{print_js_snippets(options[:action])}
var s_code=s.t();if(s_code)document.write(s_code)
</script>
JS
end

def query(options = {})
vars(options).inject([]) do |query, var|
vars(controller).inject([]) do |query, var|
query << var_to_query(var) if var.value && var.value != ""
query
end.join('&')
end

def js_vars(options = {})
vars(options).inject([]) do |query, var|
query << var_to_js(var) if var.value && var.value != ""
def js_vars(action)
output = (find_meta_vars(action) + find_meta_vars).uniq_by(&:name).map{|x| x.to_var(controller)}.inject([]) do |query, var|
query << var_to_js(var) if var.value.present?
query
end.join(";\n") + ';'
end.join(";\n")

output.blank? ? nil : output + ';'
end

def raw_vars(options = {})
vars(options).inject([]) do |query, var|
query << { var.name.to_sym => var.value } if var.value && var.value != ""
query
end
end

def print_js_events(options = {})
values = js_events(options).join(',')
values.blank? ? nil : "s.events=\"#{values}\""
def print_js_events(action)
events = (find_meta_events(action) + find_meta_events).uniq_by(&:name).map{|event| event.to_var(controller)}.inject([]) do |values, event|
(values << event.value) if event.value.present?
end.join(',')
events.blank? ? nil : "s.events=\"#{events}\";"
end

def print_js_snippets(options = {})
js_snippets(options).join("\n")
def print_js_snippets(action)
(find_meta_js_snippets(action) + find_meta_js_snippets).uniq_by(&:name).map{|snippet| snippet.to_var(controller)}.inject([]) do |snippets, snippet|
(snippets << snippet.value) if snippet.value.present?
end.join("\n")
end

private
Expand All @@ -58,7 +64,11 @@ def var_to_query(var)
end

def var_to_js(var)
%Q{\t#{Omniturize::var_prefix + '.' if Omniturize::var_prefix}#{var.name}="#{var.value}"} if var
%Q{\t#{Omniturize::var_prefix + '.' if Omniturize::var_prefix}#{var_name(var)}="#{var.value}"} if var
end

def var_name(var)
Omniturize::aliases && Omniturize::aliases[var.name.to_s] ? Omniturize::aliases[var.name.to_s] : var.name
end
end
end
22 changes: 0 additions & 22 deletions lib/omniturize/var.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/omniturize/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Omniturize
VERSION = "0.0.4"
VERSION = "1.0.0"
end
2 changes: 2 additions & 0 deletions omniturize.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.requirements << 'meta_vars 1.0.0'
s.add_dependency('meta_vars', '1.0.0')
end

0 comments on commit 5f5a472

Please sign in to comment.