Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/red_query/ajax.rb,
lib/red_query/json.rb

Instance Method Summary collapse

Instance Method Details

#to_jsonObject



146
147
148
# File 'lib/red_query/json.rb', line 146

def to_json
  JSON.generate(self)
end

#to_query_string(base = '') ⇒ Object

call-seq:

hsh.to_query_string -> string

Returns a string representing hsh formatted as HTTP data.



30
31
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
# File 'lib/red_query/ajax.rb', line 30

def to_query_string(base = '')
  query_string = []
  self.each do |k,v|
    if `#{v} === false` 
      v = "false"
    end
    if `#{v} === true` 
      v = "true"
    end
    next if v.nil?
    k = base.empty? ? k.to_s : "%s[%s]" % [base,k]
    case v
    when Hash
      result = v.to_query_string(k)
    when Array
      qs = {}
      `for(var i=0,l=v.length;i<l;i++){#{qs[i] = v[i]}}`
      #v.each_with_index do |v,i|
      #  qs[i] = v
      #end
      result = qs.to_query_string(k)
    else
      result = "%s=%s" % [k, `$q(encodeURIComponent(v))`]
    end
    query_string.push(result)
  end
  return query_string.join('&')
end