Sha256: 4d621a51a6a039a9af71a2a797dca5915e44cf419c0a7c9a640c2b19f0cefb41

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

require 'cgi'
module MonkeyWrench
  module Hash
 
    # Takes a block that returns a [key, value] pair
    # and builds a new hash based on those pairs
    # Courtesy of http://snuxoll.com/post/2009/02/13/ruby-better-hashcollect
    def collect_kv
      result = {}
      each do |k,v|
        new_k, new_v = yield k, v
        result[new_k] = new_v
      end
      result
    end
    
    def collect_kv!(&blk)
      replace(self.collect_kv(&blk))
    end
    
    def escape_keys!
      collect_kv!{|k,v| [CGI.escape(k.to_s), v]}
    end

    def to_mailchimp(index = nil, parent_name = nil)
      result = self.collect_kv do |k,v|
        if v.is_a?(Array) && v.first.is_a?(Hash)
          i = 0
          v = v.inject({}) do |acc,hash|
            acc.merge!(hash.to_mailchimp(i, k))
            i += 1
            acc
          end
        elsif v.is_a?(Hash)
          if parent_name
            v = v.collect_kv do |key,val| 
              keyname = CGI.escape("#{parent_name.to_s}[#{key.to_s.upcase}]")
              [keyname, val.to_s]
            end
          else
            v = { k => v }.to_mailchimp(nil, k)
          end
        elsif v.is_a?(Array)
          results = {}
          i = 0
          v.each do |val|
            keyname = CGI.escape("#{k}[#{i}]")
            results[keyname] = val.to_s
            i += 1
          end
          v = results
        end
        k = k.to_s
        k = "[#{index}][#{k.upcase}]" if index
        k = [parent_name, k].join if k != parent_name.to_s
        [CGI.escape(k), v]
      end
      if result.detect{|k,v| v.is_a?(Hash)}
        result.values.first
      else
        result
      end
    end
  end
end

class Hash
  include MonkeyWrench::Hash
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
monkeywrench-0.1.7 lib/monkeywrench/hash.rb
monkeywrench-0.1.6 lib/monkeywrench/hash.rb
monkeywrench-0.1.5 lib/monkeywrench/hash.rb
monkey_wrench-0.1.4 lib/monkey_wrench/hash.rb
monkey_wrench-0.1.2 lib/monkey_wrench/hash.rb