Skip to content

Commit

Permalink
monkey patch array to support wrap so we can have filters like: [{:pa…
Browse files Browse the repository at this point in the history
…ge_path.contains => 'NYC'}, [{:city.eql => 'New York City'}, {:state.eql => 'New York'}]] which prevents us from using flatten; move support stuff around and into core_ext and namespace to Garb, do not use AS constant in our extensions even though it duplicates that code
  • Loading branch information
tpitale committed Sep 16, 2011
1 parent 486c773 commit 994458a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/garb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ def symbol_operator_slugs
# probably just support open_timeout
end

require 'support'
require 'garb/support'
16 changes: 16 additions & 0 deletions lib/garb/core_ext/array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
unless Object.const_defined?("ActiveSupport")
puts "Array extension."
class Array
def self.wrap(object)
if object.nil?
[]
elsif object.respond_to?(:to_ary)
object.to_ary
else
[object]
end
end
end
else
puts "ActiveSupport is suddenly defined!"
end
10 changes: 5 additions & 5 deletions lib/support/string.rb → lib/garb/core_ext/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
class String
def camelize(first_letter = :upper)
case first_letter
when :upper then ActiveSupport::Inflector.camelize(self, true)
when :lower then ActiveSupport::Inflector.camelize(self, false)
when :upper then Garb::Inflector.camelize(self, true)
when :lower then Garb::Inflector.camelize(self, false)
end
end
alias_method :camelcase, :camelize

def underscore
ActiveSupport::Inflector.underscore(self)
Garb::Inflector.underscore(self)
end

def demodulize
ActiveSupport::Inflector.demodulize(self)
Garb::Inflector.demodulize(self)
end
end


module ActiveSupport
module Garb
module Inflector
extend self

Expand Down
4 changes: 0 additions & 4 deletions lib/support.rb → lib/garb/core_ext/symbol.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
unless Object.const_defined?("ActiveSupport")
require File.expand_path("./lib/support/string.rb")
end

module SymbolOperatorMethods
def to_google_analytics
operators = {
Expand Down
26 changes: 18 additions & 8 deletions lib/garb/filter_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,29 @@ class FilterParameters
attr_accessor :parameters

def initialize(parameters)
self.parameters = ([parameters].flatten || []).compact
self.parameters = (Array.wrap(parameters) || []).compact
end

[{}, [{}, {}]]
def to_params
value = self.parameters.map do |param|
param.map do |k,v|
next unless k.is_a?(SymbolOperator)
escaped_v = v.to_s.gsub(/([,;\\])/) {|c| '\\'+c}
"#{URI.encode(k.to_google_analytics, /[=<>]/)}#{CGI::escape(escaped_v)}"
end.join('%3B') # Hash AND (no duplicate keys), escape char for ';' fixes oauth
end.join(',') # Array OR
value = array_to_params(self.parameters)

value.empty? ? {} : {'filters' => value}
end

private
def array_to_params(arr)
arr.map do |param|
param.is_a?(Hash) ? hash_to_params(param) : array_to_params(param)
end.join(',') # Array OR
end

def hash_to_params(hsh)
hsh.map do |k,v|
next unless k.is_a?(SymbolOperator)
escaped_v = v.to_s.gsub(/([,;\\])/) {|c| '\\'+c}
"#{URI.encode(k.to_google_analytics, /[=<>]/)}#{CGI::escape(escaped_v)}"
end.join('%3B') # Hash AND (no duplicate keys), escape char for ';' fixes oauth
end
end
end
6 changes: 6 additions & 0 deletions lib/garb/support.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
unless Object.const_defined?("ActiveSupport")
require File.expand_path("core_ext/string", File.dirname(__FILE__))
require File.expand_path("core_ext/array", File.dirname(__FILE__))
end

require File.expand_path("core_ext/symbol", File.dirname(__FILE__))
7 changes: 7 additions & 0 deletions test/unit/garb/filter_parameters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class FilterParametersTest < MiniTest::Unit::TestCase
params = {'filters' => 'ga:url%3D%3Dthis%5C%3Bthat%5C%2Cthing%5C%5Cother'}
assert_equal params, filters.to_params
end

should "handle nested arrays mixed with hashes" do
filters = FilterParameters.new([{:page_path.contains => 'NYC'}, [{:city.eql => 'New York City'}, {:state.eql => 'New York'}]])

params = ['ga:pagePath%3D~NYC,ga:city%3D%3DNew+York+City,ga:state%3D%3DNew+York']
assert_equal params, filters.to_params['filters'].split('%3B').sort
end
end
end
end
Expand Down

0 comments on commit 994458a

Please sign in to comment.