Class: URL::ParamsHash

Inherits:
Mash
  • Object
show all
Defined in:
lib/url/helper_classes.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Mash

#[], #[]=

Class Method Details

.from_string(str) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/url/helper_classes.rb', line 69

def from_string str
  params = URL::ParamsHash.new
  str.split('&').each do |myp|
    key,value = myp.split('=')
    value = CGI.unescape(value) if value
    params[key.to_sym] = value if key
  end
  params
end

Instance Method Details

#reverse_merge!(other) ⇒ Object



27
28
29
# File 'lib/url/helper_classes.rb', line 27

def reverse_merge! other
  replace self|other
end

#to_s(questionmark = true) ⇒ Object

Merges the array into a parameter string of the form ?key=value&foo=bar



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/url/helper_classes.rb', line 32

def to_s(questionmark=true)
  return '' if empty?
  str = questionmark ? '?' : ''
  str << to_a.inject(Array.new) do |ret,param|
    key = param[0].to_s
    val = param[1]
    
    if param && val
      if val.is_a?(Hash)
        # TODO: Make this recusrive
        val.each do |param_key,param_val|
          param_key = CGI.escape("#{key}[#{param_key}]")
          param_val = CGI.escape(param_val.to_s)
          ret << %Q{#{param_key}=#{param_val}}
        end
      elsif val.is_a?(Array)
        # TODO: Make this recusrive
        val.each_with_index do |param_val,i|
          param_key = CGI.escape("#{key}[]")
          param_val = CGI.escape(param_val.to_s)
          ret << %Q{#{param_key}=#{param_val}}
        end
      else
        val = val.to_s

        val = CGI.escape(val)# if val =~ /(\/|\?|\s)/
        ret << %{#{param[0].to_s}=#{val}}
      end
    elsif param
      ret << param[0].to_s
    end
    ret
  end.join('&')
  str
end

#|(other) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/url/helper_classes.rb', line 19

def | other
  unless other.is_a? ParamsHash
    other = other.to_hash if other.respond_to?(:to_hash)
    other = ParamsHash[other]
  end
  other.merge(self)
end