1
- class Hash
2
- def to_query ( namespace = nil )
3
- collect do |key , value |
4
- value . to_query ( namespace ? "#{ namespace } [#{ key } ]" : key )
5
- end . sort * '&'
6
- end
7
-
8
- def symbolize_keys
9
- new_hash = { }
10
- self . each do |key , value |
11
- if [ Hash , Array ] . include? ( value . class )
12
- new_hash [ key . to_sym ] = value . symbolize_keys
13
- else
14
- new_hash [ key . to_sym ] = value
1
+ module Contentstack
2
+ module Utility
3
+ refine Hash do
4
+ def to_query ( namespace = nil )
5
+ collect do |key , value |
6
+ value . to_query ( namespace ? "#{ namespace } [#{ key } ]" : key )
7
+ end . sort * '&'
8
+ end
9
+
10
+ def symbolize_keys
11
+ new_hash = { }
12
+ self . each do |key , value |
13
+ if [ Hash , Array ] . include? ( value . class )
14
+ new_hash [ key . to_sym ] = value . symbolize_keys
15
+ else
16
+ new_hash [ key . to_sym ] = value
17
+ end
18
+ end
19
+ new_hash
20
+ end
21
+ end
22
+
23
+ refine Array do
24
+ def to_query ( key )
25
+ prefix = "#{ key } []"
26
+ collect { |value | value . to_query ( prefix ) } . join '&'
27
+ end
28
+
29
+ def symbolize_keys
30
+ collect do |entry |
31
+ if entry . class == Hash
32
+ entry . symbolize_keys
33
+ else
34
+ entry
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ refine String do
41
+ def to_query ( key )
42
+ require 'cgi' unless defined? ( CGI ) && defined? ( CGI ::escape )
43
+ "#{ CGI . escape ( key . to_param ) } =#{ CGI . escape ( to_param . to_s ) } "
44
+ end
45
+
46
+ def to_param
47
+ to_s
48
+ end
49
+ end
50
+
51
+ refine Symbol do
52
+ def to_query ( key )
53
+ to_s . to_query ( key )
54
+ end
55
+
56
+ def to_param
57
+ to_s
58
+ end
59
+ end
60
+
61
+ refine NilClass do
62
+ def to_query ( key )
63
+ to_s . to_query ( key )
64
+ end
65
+
66
+ def to_param
67
+ to_s
68
+ end
69
+ end
70
+
71
+ refine TrueClass do
72
+ def to_query ( key )
73
+ to_s . to_query ( key )
74
+ end
75
+
76
+ def to_query ( val )
77
+ "#{ CGI . escape ( val . to_param ) } =#{ CGI . escape ( to_s ) } "
15
78
end
16
79
end
17
- new_hash
18
- end
19
- end
20
-
21
- class Array
22
- def to_query ( key )
23
- prefix = "#{ key } []"
24
- collect { |value | value . to_query ( prefix ) } . join '&'
25
- end
26
-
27
- def symbolize_keys
28
- collect do |entry |
29
- if entry . class == Hash
30
- entry . symbolize_keys
31
- else
32
- entry
80
+
81
+ refine FalseClass do
82
+ def to_query ( key )
83
+ to_s . to_query ( key )
84
+ end
85
+
86
+ def to_query ( val )
87
+ "#{ CGI . escape ( val . to_param ) } =#{ CGI . escape ( to_s ) } "
33
88
end
34
89
end
35
- end
36
- end
37
-
38
- class String
39
- def to_query ( key )
40
- require 'cgi' unless defined? ( CGI ) && defined? ( CGI ::escape )
41
- "#{ CGI . escape ( key . to_param ) } =#{ CGI . escape ( to_param . to_s ) } "
42
- end
43
-
44
- def to_param
45
- to_s
46
- end
47
- end
48
-
49
- class Symbol
50
- def to_query ( key )
51
- to_s . to_query ( key )
52
- end
53
-
54
- def to_param
55
- to_s
56
- end
57
- end
58
-
59
- class NilClass
60
- def to_query ( key )
61
- to_s . to_query ( key )
62
- end
63
-
64
- def to_param
65
- to_s
66
- end
67
- end
68
-
69
- class TrueClass
70
- def to_query ( key )
71
- to_s . to_query ( key )
72
- end
73
-
74
- def to_query ( val )
75
- "#{ CGI . escape ( val . to_param ) } =#{ CGI . escape ( to_s ) } "
76
- end
77
- end
78
-
79
- class FalseClass
80
- def to_query ( key )
81
- to_s . to_query ( key )
82
- end
83
-
84
- def to_query ( val )
85
- "#{ CGI . escape ( val . to_param ) } =#{ CGI . escape ( to_s ) } "
86
- end
87
- end
88
-
89
- class Integer
90
- def to_query ( key )
91
- to_s . to_query ( key )
92
- end
93
-
94
- def to_query ( val )
95
- "#{ CGI . escape ( val . to_param ) } =#{ CGI . escape ( to_s ) } "
96
- end
97
- end
98
-
99
- class Numeric
100
- def to_query ( key )
101
- to_s . to_query ( key )
102
- end
103
-
104
- def to_query ( val )
105
- "#{ CGI . escape ( val . to_param ) } =#{ CGI . escape ( to_s ) } "
106
- end
90
+
91
+ refine Integer do
92
+ def to_query ( key )
93
+ to_s . to_query ( key )
94
+ end
95
+
96
+ def to_query ( val )
97
+ "#{ CGI . escape ( val . to_param ) } =#{ CGI . escape ( to_s ) } "
98
+ end
99
+ end
100
+
101
+ refine Numeric do
102
+ def to_query ( key )
103
+ to_s . to_query ( key )
104
+ end
105
+
106
+ def to_query ( val )
107
+ "#{ CGI . escape ( val . to_param ) } =#{ CGI . escape ( to_s ) } "
108
+ end
109
+ end
110
+ end
107
111
end
0 commit comments