Class Hash
In: lib/mack-facets/extensions/hash.rb
Parent: Object

Methods

-   join   to_params  

Public Instance methods

Deletes the key(s) passed in from the hash.

[Source]

    # File lib/mack-facets/extensions/hash.rb, line 14
14:   def -(ars)
15:     [ars].flatten.each {|a| self.delete(a)}
16:     self
17:   end

[Source]

    # File lib/mack-facets/extensions/hash.rb, line 4
 4:   def join(pair_string, join_string)
 5:     output = []
 6:     sorted = self.sort {|a,b| a[0].to_s <=> b[0].to_s}
 7:     sorted.each do |ar|
 8:       output << sprintf(pair_string, ar[0], ar[1])
 9:     end
10:     output.join(join_string)
11:   end

Converts a hash to query string parameters. An optional boolean escapes the values if true, which is the default.

[Source]

    # File lib/mack-facets/extensions/hash.rb, line 21
21:   def to_params(escape = true)
22:     params = ''
23:     stack = []
24:     
25:     each do |k, v|
26:       if v.is_a?(Hash)
27:         stack << [k,v]
28:       else
29:         v = v.to_s.uri_escape if escape
30:         params << "#{k}=#{v}&"
31:       end
32:     end
33:     
34:     stack.each do |parent, hash|
35:       hash.each do |k, v|
36:         if v.is_a?(Hash)
37:           stack << ["#{parent}[#{k}]", v]
38:         else
39:           v = v.to_s.uri_escape if escape
40:           params << "#{parent}[#{k}]=#{v}&"
41:         end
42:       end
43:     end
44:     
45:     params.chop! # trailing &
46:     params.split("&").sort.join("&")
47:   end

[Validate]