activesupport 4.0.13 → 4.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +283 -508
  3. data/README.rdoc +1 -1
  4. data/lib/active_support.rb +7 -1
  5. data/lib/active_support/backtrace_cleaner.rb +5 -5
  6. data/lib/active_support/benchmarkable.rb +0 -10
  7. data/lib/active_support/cache.rb +62 -26
  8. data/lib/active_support/cache/file_store.rb +27 -22
  9. data/lib/active_support/cache/mem_cache_store.rb +2 -2
  10. data/lib/active_support/cache/memory_store.rb +1 -0
  11. data/lib/active_support/cache/strategy/local_cache.rb +3 -0
  12. data/lib/active_support/callbacks.rb +416 -245
  13. data/lib/active_support/concern.rb +13 -5
  14. data/lib/active_support/core_ext.rb +0 -1
  15. data/lib/active_support/core_ext/array.rb +0 -1
  16. data/lib/active_support/core_ext/array/access.rb +2 -0
  17. data/lib/active_support/core_ext/array/conversions.rb +2 -17
  18. data/lib/active_support/core_ext/array/grouping.rb +24 -12
  19. data/lib/active_support/core_ext/array/prepend_and_append.rb +2 -2
  20. data/lib/active_support/core_ext/class.rb +0 -1
  21. data/lib/active_support/core_ext/class/attribute.rb +1 -2
  22. data/lib/active_support/core_ext/class/attribute_accessors.rb +5 -169
  23. data/lib/active_support/core_ext/date/calculations.rb +10 -0
  24. data/lib/active_support/core_ext/date/conversions.rb +5 -6
  25. data/lib/active_support/core_ext/date/zones.rb +2 -33
  26. data/lib/active_support/core_ext/date_and_time/calculations.rb +30 -11
  27. data/lib/active_support/core_ext/date_and_time/zones.rb +41 -0
  28. data/lib/active_support/core_ext/date_time/calculations.rb +12 -25
  29. data/lib/active_support/core_ext/date_time/conversions.rb +2 -0
  30. data/lib/active_support/core_ext/date_time/zones.rb +3 -21
  31. data/lib/active_support/core_ext/hash.rb +0 -1
  32. data/lib/active_support/core_ext/hash/conversions.rb +6 -3
  33. data/lib/active_support/core_ext/hash/deep_merge.rb +11 -22
  34. data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -0
  35. data/lib/active_support/core_ext/hash/keys.rb +27 -47
  36. data/lib/active_support/core_ext/kernel/reporting.rb +2 -6
  37. data/lib/active_support/core_ext/module.rb +1 -0
  38. data/lib/active_support/core_ext/module/attribute_accessors.rb +160 -14
  39. data/lib/active_support/core_ext/module/concerning.rb +135 -0
  40. data/lib/active_support/core_ext/module/delegation.rb +14 -4
  41. data/lib/active_support/core_ext/module/deprecation.rb +0 -2
  42. data/lib/active_support/core_ext/module/introspection.rb +0 -16
  43. data/lib/active_support/core_ext/module/method_transplanting.rb +11 -0
  44. data/lib/active_support/core_ext/numeric/time.rb +8 -0
  45. data/lib/active_support/core_ext/object.rb +1 -1
  46. data/lib/active_support/core_ext/object/blank.rb +1 -1
  47. data/lib/active_support/core_ext/object/deep_dup.rb +6 -6
  48. data/lib/active_support/core_ext/object/inclusion.rb +4 -15
  49. data/lib/active_support/core_ext/object/json.rb +197 -0
  50. data/lib/active_support/core_ext/object/to_json.rb +4 -26
  51. data/lib/active_support/core_ext/object/to_param.rb +58 -1
  52. data/lib/active_support/core_ext/object/to_query.rb +7 -56
  53. data/lib/active_support/core_ext/object/try.rb +1 -1
  54. data/lib/active_support/core_ext/range/each.rb +2 -1
  55. data/lib/active_support/core_ext/string/access.rb +31 -31
  56. data/lib/active_support/core_ext/string/conversions.rb +9 -8
  57. data/lib/active_support/core_ext/string/exclude.rb +3 -3
  58. data/lib/active_support/core_ext/string/filters.rb +14 -4
  59. data/lib/active_support/core_ext/string/inflections.rb +11 -9
  60. data/lib/active_support/core_ext/string/output_safety.rb +65 -24
  61. data/lib/active_support/core_ext/string/zones.rb +1 -0
  62. data/lib/active_support/core_ext/thread.rb +4 -4
  63. data/lib/active_support/core_ext/time/calculations.rb +10 -57
  64. data/lib/active_support/core_ext/time/conversions.rb +3 -1
  65. data/lib/active_support/core_ext/time/zones.rb +2 -21
  66. data/lib/active_support/dependencies.rb +29 -13
  67. data/lib/active_support/deprecation.rb +4 -4
  68. data/lib/active_support/deprecation/behaviors.rb +3 -3
  69. data/lib/active_support/duration.rb +5 -7
  70. data/lib/active_support/file_update_checker.rb +1 -1
  71. data/lib/active_support/hash_with_indifferent_access.rb +4 -9
  72. data/lib/active_support/i18n.rb +4 -4
  73. data/lib/active_support/i18n_railtie.rb +2 -6
  74. data/lib/active_support/inflections.rb +0 -1
  75. data/lib/active_support/inflector/inflections.rb +17 -17
  76. data/lib/active_support/inflector/methods.rb +34 -17
  77. data/lib/active_support/json/decoding.rb +14 -21
  78. data/lib/active_support/json/encoding.rb +113 -285
  79. data/lib/active_support/key_generator.rb +1 -1
  80. data/lib/active_support/lazy_load_hooks.rb +1 -1
  81. data/lib/active_support/log_subscriber/test_helper.rb +1 -1
  82. data/lib/active_support/logger.rb +1 -1
  83. data/lib/active_support/message_encryptor.rb +3 -3
  84. data/lib/active_support/message_verifier.rb +6 -1
  85. data/lib/active_support/multibyte/chars.rb +1 -2
  86. data/lib/active_support/multibyte/unicode.rb +27 -39
  87. data/lib/active_support/notifications.rb +3 -3
  88. data/lib/active_support/notifications/instrumenter.rb +2 -1
  89. data/lib/active_support/number_helper.rb +20 -311
  90. data/lib/active_support/number_helper/number_converter.rb +182 -0
  91. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  92. data/lib/active_support/number_helper/number_to_delimited_converter.rb +21 -0
  93. data/lib/active_support/number_helper/number_to_human_converter.rb +66 -0
  94. data/lib/active_support/number_helper/number_to_human_size_converter.rb +58 -0
  95. data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
  96. data/lib/active_support/number_helper/number_to_phone_converter.rb +49 -0
  97. data/lib/active_support/number_helper/number_to_rounded_converter.rb +62 -0
  98. data/lib/active_support/option_merger.rb +1 -1
  99. data/lib/active_support/ordered_hash.rb +0 -8
  100. data/lib/active_support/ordered_options.rb +8 -0
  101. data/lib/active_support/per_thread_registry.rb +9 -8
  102. data/lib/active_support/subscriber.rb +26 -3
  103. data/lib/active_support/test_case.rb +9 -10
  104. data/lib/active_support/testing/assertions.rb +0 -30
  105. data/lib/active_support/testing/autorun.rb +2 -2
  106. data/lib/active_support/testing/declarative.rb +18 -8
  107. data/lib/active_support/testing/isolation.rb +13 -65
  108. data/lib/active_support/testing/setup_and_teardown.rb +17 -2
  109. data/lib/active_support/testing/tagged_logging.rb +1 -1
  110. data/lib/active_support/testing/time_helpers.rb +55 -0
  111. data/lib/active_support/time_with_zone.rb +4 -4
  112. data/lib/active_support/values/time_zone.rb +18 -15
  113. data/lib/active_support/version.rb +1 -1
  114. data/lib/active_support/xml_mini.rb +2 -4
  115. metadata +71 -61
  116. data/lib/active_support/basic_object.rb +0 -11
  117. data/lib/active_support/buffered_logger.rb +0 -21
  118. data/lib/active_support/core_ext/array/uniq_by.rb +0 -19
  119. data/lib/active_support/core_ext/hash/diff.rb +0 -14
  120. data/lib/active_support/core_ext/logger.rb +0 -67
  121. data/lib/active_support/core_ext/proc.rb +0 -17
  122. data/lib/active_support/core_ext/string/encoding.rb +0 -8
  123. data/lib/active_support/json/variable.rb +0 -18
  124. data/lib/active_support/testing/pending.rb +0 -14
@@ -59,20 +59,4 @@ class Module
59
59
  def local_constants #:nodoc:
60
60
  constants(false)
61
61
  end
62
-
63
- # *DEPRECATED*: Use +local_constants+ instead.
64
- #
65
- # Returns the names of the constants defined locally as strings.
66
- #
67
- # module M
68
- # X = 1
69
- # end
70
- # M.local_constant_names # => ["X"]
71
- #
72
- # This method is useful for forward compatibility, since Ruby 1.8 returns
73
- # constant names as strings, whereas 1.9 returns them as symbols.
74
- def local_constant_names
75
- ActiveSupport::Deprecation.warn 'Module#local_constant_names is deprecated, use Module#local_constants instead'
76
- local_constants.map { |c| c.to_s }
77
- end
78
62
  end
@@ -0,0 +1,11 @@
1
+ class Module
2
+ ###
3
+ # TODO: remove this after 1.9 support is dropped
4
+ def methods_transplantable? # :nodoc:
5
+ x = Module.new { def foo; end }
6
+ Module.new { define_method :bar, x.instance_method(:foo) }
7
+ true
8
+ rescue TypeError
9
+ false
10
+ end
11
+ end
@@ -63,6 +63,7 @@ class Numeric
63
63
 
64
64
  # Reads best without arguments: 10.minutes.ago
65
65
  def ago(time = ::Time.current)
66
+ ActiveSupport::Deprecation.warn "Calling #ago or #until on a number (e.g. 5.ago) is deprecated and will be removed in the future, use 5.seconds.ago instead"
66
67
  time - self
67
68
  end
68
69
 
@@ -71,9 +72,16 @@ class Numeric
71
72
 
72
73
  # Reads best with argument: 10.minutes.since(time)
73
74
  def since(time = ::Time.current)
75
+ ActiveSupport::Deprecation.warn "Calling #since or #from_now on a number (e.g. 5.since) is deprecated and will be removed in the future, use 5.seconds.since instead"
74
76
  time + self
75
77
  end
76
78
 
77
79
  # Reads best without arguments: 10.minutes.from_now
78
80
  alias :from_now :since
81
+
82
+ # Used with the standard time durations, like 1.hour.in_milliseconds --
83
+ # so we can feed them to JavaScript functions like getTime().
84
+ def in_milliseconds
85
+ self * 1000
86
+ end
79
87
  end
@@ -8,7 +8,7 @@ require 'active_support/core_ext/object/inclusion'
8
8
  require 'active_support/core_ext/object/conversions'
9
9
  require 'active_support/core_ext/object/instance_variables'
10
10
 
11
- require 'active_support/core_ext/object/to_json'
11
+ require 'active_support/core_ext/object/json'
12
12
  require 'active_support/core_ext/object/to_param'
13
13
  require 'active_support/core_ext/object/to_query'
14
14
  require 'active_support/core_ext/object/with_options'
@@ -90,7 +90,7 @@ class String
90
90
  # ' '.blank? # => true
91
91
  # ' something here '.blank? # => false
92
92
  def blank?
93
- self !~ /[^[:space:]]/
93
+ self =~ /\A[[:space:]]*\z/
94
94
  end
95
95
  end
96
96
 
@@ -8,8 +8,8 @@ class Object
8
8
  # dup = object.deep_dup
9
9
  # dup.instance_variable_set(:@a, 1)
10
10
  #
11
- # object.instance_variable_defined?(:@a) #=> false
12
- # dup.instance_variable_defined?(:@a) #=> true
11
+ # object.instance_variable_defined?(:@a) # => false
12
+ # dup.instance_variable_defined?(:@a) # => true
13
13
  def deep_dup
14
14
  duplicable? ? dup : self
15
15
  end
@@ -22,8 +22,8 @@ class Array
22
22
  # dup = array.deep_dup
23
23
  # dup[1][2] = 4
24
24
  #
25
- # array[1][2] #=> nil
26
- # dup[1][2] #=> 4
25
+ # array[1][2] # => nil
26
+ # dup[1][2] # => 4
27
27
  def deep_dup
28
28
  map { |it| it.deep_dup }
29
29
  end
@@ -36,8 +36,8 @@ class Hash
36
36
  # dup = hash.deep_dup
37
37
  # dup[:a][:c] = 'c'
38
38
  #
39
- # hash[:a][:c] #=> nil
40
- # dup[:a][:c] #=> "c"
39
+ # hash[:a][:c] # => nil
40
+ # dup[:a][:c] # => "c"
41
41
  def deep_dup
42
42
  each_with_object(dup) do |(key, value), hash|
43
43
  hash[key.deep_dup] = value.deep_dup
@@ -1,5 +1,3 @@
1
- require 'active_support/deprecation'
2
-
3
1
  class Object
4
2
  # Returns true if this object is included in the argument. Argument must be
5
3
  # any object which responds to +#include?+. Usage:
@@ -9,18 +7,9 @@ class Object
9
7
  #
10
8
  # This will throw an ArgumentError if the argument doesn't respond
11
9
  # to +#include?+.
12
- def in?(*args)
13
- if args.length > 1
14
- ActiveSupport::Deprecation.warn "Calling #in? with multiple arguments is" \
15
- " deprecated, please pass in an object that responds to #include? instead."
16
- args.include? self
17
- else
18
- another_object = args.first
19
- if another_object.respond_to? :include?
20
- another_object.include? self
21
- else
22
- raise ArgumentError.new 'The single parameter passed to #in? must respond to #include?'
23
- end
24
- end
10
+ def in?(another_object)
11
+ another_object.include?(self)
12
+ rescue NoMethodError
13
+ raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
25
14
  end
26
15
  end
@@ -0,0 +1,197 @@
1
+ # Hack to load json gem first so we can overwrite its to_json.
2
+ require 'json'
3
+ require 'bigdecimal'
4
+ require 'active_support/core_ext/big_decimal/conversions' # for #to_s
5
+ require 'active_support/core_ext/hash/except'
6
+ require 'active_support/core_ext/hash/slice'
7
+ require 'active_support/core_ext/object/instance_variables'
8
+ require 'time'
9
+ require 'active_support/core_ext/time/conversions'
10
+ require 'active_support/core_ext/date_time/conversions'
11
+ require 'active_support/core_ext/date/conversions'
12
+ require 'active_support/core_ext/module/aliasing'
13
+
14
+ # The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
15
+ # their default behavior. That said, we need to define the basic to_json method in all of them,
16
+ # otherwise they will always use to_json gem implementation, which is backwards incompatible in
17
+ # several cases (for instance, the JSON implementation for Hash does not work) with inheritance
18
+ # and consequently classes as ActiveSupport::OrderedHash cannot be serialized to json.
19
+ #
20
+ # On the other hand, we should avoid conflict with ::JSON.{generate,dump}(obj). Unfortunately, the
21
+ # JSON gem's encoder relies on its own to_json implementation to encode objects. Since it always
22
+ # passes a ::JSON::State object as the only argument to to_json, we can detect that and forward the
23
+ # calls to the original to_json method.
24
+ #
25
+ # It should be noted that when using ::JSON.{generate,dump} directly, ActiveSupport's encoder is
26
+ # bypassed completely. This means that as_json won't be invoked and the JSON gem will simply
27
+ # ignore any options it does not natively understand. This also means that ::JSON.{generate,dump}
28
+ # should give exactly the same results with or without active support.
29
+ [Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
30
+ klass.class_eval do
31
+ def to_json_with_active_support_encoder(options = nil)
32
+ if options.is_a?(::JSON::State)
33
+ # Called from JSON.{generate,dump}, forward it to JSON gem's to_json
34
+ self.to_json_without_active_support_encoder(options)
35
+ else
36
+ # to_json is being invoked directly, use ActiveSupport's encoder
37
+ ActiveSupport::JSON.encode(self, options)
38
+ end
39
+ end
40
+
41
+ alias_method_chain :to_json, :active_support_encoder
42
+ end
43
+ end
44
+
45
+ class Object
46
+ def as_json(options = nil) #:nodoc:
47
+ if respond_to?(:to_hash)
48
+ to_hash.as_json(options)
49
+ else
50
+ instance_values.as_json(options)
51
+ end
52
+ end
53
+ end
54
+
55
+ class Struct #:nodoc:
56
+ def as_json(options = nil)
57
+ Hash[members.zip(values)].as_json(options)
58
+ end
59
+ end
60
+
61
+ class TrueClass
62
+ def as_json(options = nil) #:nodoc:
63
+ self
64
+ end
65
+ end
66
+
67
+ class FalseClass
68
+ def as_json(options = nil) #:nodoc:
69
+ self
70
+ end
71
+ end
72
+
73
+ class NilClass
74
+ def as_json(options = nil) #:nodoc:
75
+ self
76
+ end
77
+ end
78
+
79
+ class String
80
+ def as_json(options = nil) #:nodoc:
81
+ self
82
+ end
83
+ end
84
+
85
+ class Symbol
86
+ def as_json(options = nil) #:nodoc:
87
+ to_s
88
+ end
89
+ end
90
+
91
+ class Numeric
92
+ def as_json(options = nil) #:nodoc:
93
+ self
94
+ end
95
+ end
96
+
97
+ class Float
98
+ # Encoding Infinity or NaN to JSON should return "null". The default returns
99
+ # "Infinity" or "NaN" which are not valid JSON.
100
+ def as_json(options = nil) #:nodoc:
101
+ finite? ? self : nil
102
+ end
103
+ end
104
+
105
+ class BigDecimal
106
+ # A BigDecimal would be naturally represented as a JSON number. Most libraries,
107
+ # however, parse non-integer JSON numbers directly as floats. Clients using
108
+ # those libraries would get in general a wrong number and no way to recover
109
+ # other than manually inspecting the string with the JSON code itself.
110
+ #
111
+ # That's why a JSON string is returned. The JSON literal is not numeric, but
112
+ # if the other end knows by contract that the data is supposed to be a
113
+ # BigDecimal, it still has the chance to post-process the string and get the
114
+ # real value.
115
+ def as_json(options = nil) #:nodoc:
116
+ finite? ? to_s : nil
117
+ end
118
+ end
119
+
120
+ class Regexp
121
+ def as_json(options = nil) #:nodoc:
122
+ to_s
123
+ end
124
+ end
125
+
126
+ module Enumerable
127
+ def as_json(options = nil) #:nodoc:
128
+ to_a.as_json(options)
129
+ end
130
+ end
131
+
132
+ class Range
133
+ def as_json(options = nil) #:nodoc:
134
+ to_s
135
+ end
136
+ end
137
+
138
+ class Array
139
+ def as_json(options = nil) #:nodoc:
140
+ map { |v| options ? v.as_json(options.dup) : v.as_json }
141
+ end
142
+ end
143
+
144
+ class Hash
145
+ def as_json(options = nil) #:nodoc:
146
+ # create a subset of the hash by applying :only or :except
147
+ subset = if options
148
+ if attrs = options[:only]
149
+ slice(*Array(attrs))
150
+ elsif attrs = options[:except]
151
+ except(*Array(attrs))
152
+ else
153
+ self
154
+ end
155
+ else
156
+ self
157
+ end
158
+
159
+ Hash[subset.map { |k, v| [k.to_s, options ? v.as_json(options.dup) : v.as_json] }]
160
+ end
161
+ end
162
+
163
+ class Time
164
+ def as_json(options = nil) #:nodoc:
165
+ if ActiveSupport.use_standard_json_time_format
166
+ xmlschema(3)
167
+ else
168
+ %(#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
169
+ end
170
+ end
171
+ end
172
+
173
+ class Date
174
+ def as_json(options = nil) #:nodoc:
175
+ if ActiveSupport.use_standard_json_time_format
176
+ strftime("%Y-%m-%d")
177
+ else
178
+ strftime("%Y/%m/%d")
179
+ end
180
+ end
181
+ end
182
+
183
+ class DateTime
184
+ def as_json(options = nil) #:nodoc:
185
+ if ActiveSupport.use_standard_json_time_format
186
+ xmlschema(3)
187
+ else
188
+ strftime('%Y/%m/%d %H:%M:%S %z')
189
+ end
190
+ end
191
+ end
192
+
193
+ class Process::Status #:nodoc:
194
+ def as_json(options = nil)
195
+ { :exitstatus => exitstatus, :pid => pid }
196
+ end
197
+ end
@@ -1,27 +1,5 @@
1
- # Hack to load json gem first so we can overwrite its to_json.
2
- begin
3
- require 'json'
4
- rescue LoadError
5
- end
1
+ ActiveSupport::Deprecation.warn 'You have required `active_support/core_ext/object/to_json`. ' \
2
+ 'This file will be removed in Rails 4.2. You should require `active_support/core_ext/object/json` ' \
3
+ 'instead.'
6
4
 
7
- # The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
8
- # their default behavior. That said, we need to define the basic to_json method in all of them,
9
- # otherwise they will always use to_json gem implementation, which is backwards incompatible in
10
- # several cases (for instance, the JSON implementation for Hash does not work) with inheritance
11
- # and consequently classes as ActiveSupport::OrderedHash cannot be serialized to json.
12
- [Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
13
- klass.class_eval do
14
- # Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
15
- def to_json(options = nil)
16
- ActiveSupport::JSON.encode(self, options)
17
- end
18
- end
19
- end
20
-
21
- module Process
22
- class Status
23
- def as_json(options = nil)
24
- { :exitstatus => exitstatus, :pid => pid }
25
- end
26
- end
27
- end
5
+ require 'active_support/core_ext/object/json'
@@ -1 +1,58 @@
1
- require 'active_support/core_ext/object/to_query'
1
+ class Object
2
+ # Alias of <tt>to_s</tt>.
3
+ def to_param
4
+ to_s
5
+ end
6
+ end
7
+
8
+ class NilClass
9
+ # Returns +self+.
10
+ def to_param
11
+ self
12
+ end
13
+ end
14
+
15
+ class TrueClass
16
+ # Returns +self+.
17
+ def to_param
18
+ self
19
+ end
20
+ end
21
+
22
+ class FalseClass
23
+ # Returns +self+.
24
+ def to_param
25
+ self
26
+ end
27
+ end
28
+
29
+ class Array
30
+ # Calls <tt>to_param</tt> on all its elements and joins the result with
31
+ # slashes. This is used by <tt>url_for</tt> in Action Pack.
32
+ def to_param
33
+ collect { |e| e.to_param }.join '/'
34
+ end
35
+ end
36
+
37
+ class Hash
38
+ # Returns a string representation of the receiver suitable for use as a URL
39
+ # query string:
40
+ #
41
+ # {name: 'David', nationality: 'Danish'}.to_param
42
+ # # => "name=David&nationality=Danish"
43
+ #
44
+ # An optional namespace can be passed to enclose the param names:
45
+ #
46
+ # {name: 'David', nationality: 'Danish'}.to_param('user')
47
+ # # => "user[name]=David&user[nationality]=Danish"
48
+ #
49
+ # The string pairs "key=value" that conform the query string
50
+ # are sorted lexicographically in ascending order.
51
+ #
52
+ # This method is also aliased as +to_query+.
53
+ def to_param(namespace = nil)
54
+ collect do |key, value|
55
+ value.to_query(namespace ? "#{namespace}[#{key}]" : key)
56
+ end.sort! * '&'
57
+ end
58
+ end
@@ -1,45 +1,17 @@
1
- class Object
2
- # Alias of <tt>to_s</tt>.
3
- def to_param
4
- to_s
5
- end
1
+ require 'active_support/core_ext/object/to_param'
6
2
 
7
- # Converts an object into a string suitable for use as a URL query string,
8
- # using the given <tt>key</tt> as the param name.
3
+ class Object
4
+ # Converts an object into a string suitable for use as a URL query string, using the given <tt>key</tt> as the
5
+ # param name.
6
+ #
7
+ # Note: This method is defined as a default implementation for all Objects for Hash#to_query to work.
9
8
  def to_query(key)
10
9
  require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
11
10
  "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
12
11
  end
13
12
  end
14
13
 
15
- class NilClass
16
- # Returns +self+.
17
- def to_param
18
- self
19
- end
20
- end
21
-
22
- class TrueClass
23
- # Returns +self+.
24
- def to_param
25
- self
26
- end
27
- end
28
-
29
- class FalseClass
30
- # Returns +self+.
31
- def to_param
32
- self
33
- end
34
- end
35
-
36
14
  class Array
37
- # Calls <tt>to_param</tt> on all its elements and joins the result with
38
- # slashes. This is used by <tt>url_for</tt> in Action Pack.
39
- def to_param
40
- collect { |e| e.to_param }.join '/'
41
- end
42
-
43
15
  # Converts an array into a string suitable for use as a URL query string,
44
16
  # using the given +key+ as the param name.
45
17
  #
@@ -51,26 +23,5 @@ class Array
51
23
  end
52
24
 
53
25
  class Hash
54
- # Returns a string representation of the receiver suitable for use as a URL
55
- # query string:
56
- #
57
- # {name: 'David', nationality: 'Danish'}.to_query
58
- # # => "name=David&nationality=Danish"
59
- #
60
- # An optional namespace can be passed to enclose the param names:
61
- #
62
- # {name: 'David', nationality: 'Danish'}.to_query('user')
63
- # # => "user[name]=David&user[nationality]=Danish"
64
- #
65
- # The string pairs "key=value" that conform the query string
66
- # are sorted lexicographically in ascending order.
67
- #
68
- # This method is also aliased as +to_param+.
69
- def to_query(namespace = nil)
70
- collect do |key, value|
71
- value.to_query(namespace ? "#{namespace}[#{key}]" : key)
72
- end.sort * '&'
73
- end
74
-
75
- alias_method :to_param, :to_query
26
+ alias_method :to_query, :to_param
76
27
  end