activesupport 3.2.22.5 → 4.0.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 (214) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +325 -136
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +4 -2
  5. data/lib/active_support.rb +8 -21
  6. data/lib/active_support/backtrace_cleaner.rb +33 -25
  7. data/lib/active_support/basic_object.rb +7 -17
  8. data/lib/active_support/benchmarkable.rb +19 -15
  9. data/lib/active_support/buffered_logger.rb +9 -113
  10. data/lib/active_support/cache.rb +203 -171
  11. data/lib/active_support/cache/file_store.rb +12 -12
  12. data/lib/active_support/cache/mem_cache_store.rb +24 -30
  13. data/lib/active_support/cache/memory_store.rb +2 -0
  14. data/lib/active_support/callbacks.rb +195 -247
  15. data/lib/active_support/concern.rb +16 -23
  16. data/lib/active_support/concurrency/latch.rb +27 -0
  17. data/lib/active_support/configurable.rb +69 -12
  18. data/lib/active_support/core_ext.rb +1 -0
  19. data/lib/active_support/core_ext/array.rb +0 -1
  20. data/lib/active_support/core_ext/array/access.rb +17 -9
  21. data/lib/active_support/core_ext/array/conversions.rb +113 -55
  22. data/lib/active_support/core_ext/array/extract_options.rb +2 -2
  23. data/lib/active_support/core_ext/array/grouping.rb +21 -22
  24. data/lib/active_support/core_ext/array/uniq_by.rb +12 -9
  25. data/lib/active_support/core_ext/array/wrap.rb +11 -14
  26. data/lib/active_support/core_ext/big_decimal/conversions.rb +7 -24
  27. data/lib/active_support/core_ext/class/attribute.rb +12 -8
  28. data/lib/active_support/core_ext/class/attribute_accessors.rb +14 -12
  29. data/lib/active_support/core_ext/class/delegating_attributes.rb +15 -19
  30. data/lib/active_support/core_ext/class/subclasses.rb +11 -5
  31. data/lib/active_support/core_ext/date.rb +6 -0
  32. data/lib/active_support/core_ext/date/calculations.rb +34 -188
  33. data/lib/active_support/core_ext/date/conversions.rb +16 -38
  34. data/lib/active_support/core_ext/date/infinite_comparable.rb +5 -0
  35. data/lib/active_support/core_ext/date/zones.rb +25 -2
  36. data/lib/active_support/core_ext/date_and_time/calculations.rb +232 -0
  37. data/lib/active_support/core_ext/date_time.rb +5 -0
  38. data/lib/active_support/core_ext/date_time/acts_like.rb +0 -1
  39. data/lib/active_support/core_ext/date_time/calculations.rb +73 -65
  40. data/lib/active_support/core_ext/date_time/conversions.rb +21 -33
  41. data/lib/active_support/core_ext/date_time/infinite_comparable.rb +5 -0
  42. data/lib/active_support/core_ext/date_time/zones.rb +11 -8
  43. data/lib/active_support/core_ext/enumerable.rb +26 -73
  44. data/lib/active_support/core_ext/file.rb +0 -1
  45. data/lib/active_support/core_ext/file/atomic.rb +27 -11
  46. data/lib/active_support/core_ext/hash.rb +0 -1
  47. data/lib/active_support/core_ext/hash/conversions.rb +145 -79
  48. data/lib/active_support/core_ext/hash/deep_merge.rb +14 -8
  49. data/lib/active_support/core_ext/hash/diff.rb +5 -4
  50. data/lib/active_support/core_ext/hash/except.rb +1 -9
  51. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -5
  52. data/lib/active_support/core_ext/hash/keys.rb +108 -24
  53. data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
  54. data/lib/active_support/core_ext/hash/slice.rb +12 -12
  55. data/lib/active_support/core_ext/infinite_comparable.rb +35 -0
  56. data/lib/active_support/core_ext/integer/inflections.rb +13 -1
  57. data/lib/active_support/core_ext/integer/time.rb +17 -12
  58. data/lib/active_support/core_ext/kernel/debugger.rb +2 -2
  59. data/lib/active_support/core_ext/kernel/reporting.rb +36 -22
  60. data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
  61. data/lib/active_support/core_ext/load_error.rb +7 -5
  62. data/lib/active_support/core_ext/logger.rb +7 -23
  63. data/lib/active_support/core_ext/marshal.rb +19 -0
  64. data/lib/active_support/core_ext/module.rb +1 -3
  65. data/lib/active_support/core_ext/module/aliasing.rb +8 -9
  66. data/lib/active_support/core_ext/module/anonymous.rb +2 -7
  67. data/lib/active_support/core_ext/module/attr_internal.rb +0 -1
  68. data/lib/active_support/core_ext/module/attribute_accessors.rb +12 -10
  69. data/lib/active_support/core_ext/module/delegation.rb +57 -40
  70. data/lib/active_support/core_ext/module/deprecation.rb +19 -3
  71. data/lib/active_support/core_ext/module/introspection.rb +17 -27
  72. data/lib/active_support/core_ext/module/qualified_const.rb +8 -20
  73. data/lib/active_support/core_ext/module/remove_method.rb +1 -5
  74. data/lib/active_support/core_ext/numeric.rb +2 -0
  75. data/lib/active_support/core_ext/numeric/conversions.rb +135 -0
  76. data/lib/active_support/core_ext/numeric/infinite_comparable.rb +9 -0
  77. data/lib/active_support/core_ext/numeric/time.rb +6 -6
  78. data/lib/active_support/core_ext/object.rb +1 -0
  79. data/lib/active_support/core_ext/object/acts_like.rb +4 -4
  80. data/lib/active_support/core_ext/object/blank.rb +7 -23
  81. data/lib/active_support/core_ext/object/deep_dup.rb +46 -0
  82. data/lib/active_support/core_ext/object/duplicable.rb +1 -30
  83. data/lib/active_support/core_ext/object/inclusion.rb +6 -6
  84. data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
  85. data/lib/active_support/core_ext/object/to_json.rb +8 -0
  86. data/lib/active_support/core_ext/object/to_param.rb +5 -2
  87. data/lib/active_support/core_ext/object/try.rb +46 -25
  88. data/lib/active_support/core_ext/object/with_options.rb +7 -8
  89. data/lib/active_support/core_ext/proc.rb +3 -0
  90. data/lib/active_support/core_ext/range.rb +0 -2
  91. data/lib/active_support/core_ext/range/conversions.rb +0 -2
  92. data/lib/active_support/core_ext/range/include_range.rb +1 -1
  93. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  94. data/lib/active_support/core_ext/string.rb +2 -2
  95. data/lib/active_support/core_ext/string/access.rb +95 -90
  96. data/lib/active_support/core_ext/string/conversions.rb +29 -38
  97. data/lib/active_support/core_ext/string/encoding.rb +6 -9
  98. data/lib/active_support/core_ext/string/filters.rb +24 -18
  99. data/lib/active_support/core_ext/string/indent.rb +43 -0
  100. data/lib/active_support/core_ext/string/inflections.rb +70 -60
  101. data/lib/active_support/core_ext/string/inquiry.rb +2 -2
  102. data/lib/active_support/core_ext/string/multibyte.rb +41 -64
  103. data/lib/active_support/core_ext/string/output_safety.rb +59 -51
  104. data/lib/active_support/core_ext/string/zones.rb +13 -0
  105. data/lib/active_support/core_ext/struct.rb +6 -0
  106. data/lib/active_support/core_ext/thread.rb +74 -0
  107. data/lib/active_support/core_ext/time.rb +6 -0
  108. data/lib/active_support/core_ext/time/calculations.rb +105 -193
  109. data/lib/active_support/core_ext/time/conversions.rb +27 -51
  110. data/lib/active_support/core_ext/time/infinite_comparable.rb +5 -0
  111. data/lib/active_support/core_ext/time/marshal.rb +0 -27
  112. data/lib/active_support/core_ext/time/zones.rb +27 -17
  113. data/lib/active_support/core_ext/uri.rb +13 -17
  114. data/lib/active_support/dependencies.rb +160 -141
  115. data/lib/active_support/dependencies/autoload.rb +47 -20
  116. data/lib/active_support/deprecation.rb +39 -14
  117. data/lib/active_support/deprecation/behaviors.rb +44 -30
  118. data/lib/active_support/deprecation/instance_delegator.rb +24 -0
  119. data/lib/active_support/deprecation/method_wrappers.rb +33 -18
  120. data/lib/active_support/deprecation/proxy_wrappers.rb +58 -13
  121. data/lib/active_support/deprecation/reporting.rb +40 -11
  122. data/lib/active_support/descendants_tracker.rb +34 -19
  123. data/lib/active_support/duration.rb +6 -8
  124. data/lib/active_support/file_update_checker.rb +63 -47
  125. data/lib/active_support/gzip.rb +11 -5
  126. data/lib/active_support/hash_with_indifferent_access.rb +112 -37
  127. data/lib/active_support/i18n.rb +4 -0
  128. data/lib/active_support/i18n_railtie.rb +5 -22
  129. data/lib/active_support/inflections.rb +14 -12
  130. data/lib/active_support/inflector/inflections.rb +108 -71
  131. data/lib/active_support/inflector/methods.rb +181 -160
  132. data/lib/active_support/inflector/transliterate.rb +16 -17
  133. data/lib/active_support/json/decoding.rb +18 -17
  134. data/lib/active_support/json/encoding.rb +93 -39
  135. data/lib/active_support/json/variable.rb +10 -1
  136. data/lib/active_support/key_generator.rb +75 -0
  137. data/lib/active_support/lazy_load_hooks.rb +21 -19
  138. data/lib/active_support/locale/en.yml +100 -3
  139. data/lib/active_support/log_subscriber.rb +56 -36
  140. data/lib/active_support/log_subscriber/test_helper.rb +18 -15
  141. data/lib/active_support/logger.rb +57 -0
  142. data/lib/active_support/logger_silence.rb +24 -0
  143. data/lib/active_support/message_encryptor.rb +32 -29
  144. data/lib/active_support/message_verifier.rb +8 -14
  145. data/lib/active_support/multibyte.rb +5 -28
  146. data/lib/active_support/multibyte/chars.rb +80 -333
  147. data/lib/active_support/multibyte/unicode.rb +74 -64
  148. data/lib/active_support/notifications.rb +57 -25
  149. data/lib/active_support/notifications/fanout.rb +105 -18
  150. data/lib/active_support/notifications/instrumenter.rb +32 -13
  151. data/lib/active_support/number_helper.rb +636 -0
  152. data/lib/active_support/ordered_hash.rb +8 -190
  153. data/lib/active_support/ordered_options.rb +21 -23
  154. data/lib/active_support/proxy_object.rb +13 -0
  155. data/lib/active_support/rails.rb +27 -0
  156. data/lib/active_support/railtie.rb +12 -32
  157. data/lib/active_support/rescuable.rb +9 -4
  158. data/lib/active_support/string_inquirer.rb +13 -8
  159. data/lib/active_support/tagged_logging.rb +51 -73
  160. data/lib/active_support/test_case.rb +46 -17
  161. data/lib/active_support/testing/assertions.rb +56 -26
  162. data/lib/active_support/testing/autorun.rb +5 -0
  163. data/lib/active_support/testing/constant_lookup.rb +52 -0
  164. data/lib/active_support/testing/declarative.rb +1 -1
  165. data/lib/active_support/testing/deprecation.rb +0 -19
  166. data/lib/active_support/testing/isolation.rb +25 -58
  167. data/lib/active_support/testing/pending.rb +5 -43
  168. data/lib/active_support/testing/setup_and_teardown.rb +6 -92
  169. data/lib/active_support/testing/tagged_logging.rb +25 -0
  170. data/lib/active_support/time.rb +6 -21
  171. data/lib/active_support/time_with_zone.rb +78 -43
  172. data/lib/active_support/values/time_zone.rb +77 -58
  173. data/lib/active_support/values/unicode_tables.dat +0 -0
  174. data/lib/active_support/version.rb +4 -4
  175. data/lib/active_support/xml_mini.rb +35 -17
  176. data/lib/active_support/xml_mini/jdom.rb +9 -17
  177. data/lib/active_support/xml_mini/libxml.rb +1 -2
  178. data/lib/active_support/xml_mini/libxmlsax.rb +1 -2
  179. data/lib/active_support/xml_mini/nokogiri.rb +1 -2
  180. data/lib/active_support/xml_mini/nokogirisax.rb +1 -2
  181. data/lib/active_support/xml_mini/rexml.rb +6 -8
  182. metadata +107 -77
  183. data/lib/active_support/base64.rb +0 -54
  184. data/lib/active_support/core_ext/array/random_access.rb +0 -30
  185. data/lib/active_support/core_ext/date/freeze.rb +0 -33
  186. data/lib/active_support/core_ext/exception.rb +0 -3
  187. data/lib/active_support/core_ext/file/path.rb +0 -5
  188. data/lib/active_support/core_ext/float.rb +0 -1
  189. data/lib/active_support/core_ext/float/rounding.rb +0 -19
  190. data/lib/active_support/core_ext/hash/deep_dup.rb +0 -18
  191. data/lib/active_support/core_ext/io.rb +0 -15
  192. data/lib/active_support/core_ext/module/method_names.rb +0 -14
  193. data/lib/active_support/core_ext/module/synchronization.rb +0 -45
  194. data/lib/active_support/core_ext/process.rb +0 -1
  195. data/lib/active_support/core_ext/process/daemon.rb +0 -23
  196. data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
  197. data/lib/active_support/core_ext/range/cover.rb +0 -3
  198. data/lib/active_support/core_ext/rexml.rb +0 -46
  199. data/lib/active_support/core_ext/string/interpolation.rb +0 -2
  200. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
  201. data/lib/active_support/memoizable.rb +0 -116
  202. data/lib/active_support/multibyte/exceptions.rb +0 -8
  203. data/lib/active_support/multibyte/utils.rb +0 -60
  204. data/lib/active_support/ruby/shim.rb +0 -22
  205. data/lib/active_support/security_utils.rb +0 -27
  206. data/lib/active_support/testing/mochaing.rb +0 -7
  207. data/lib/active_support/testing/performance.rb +0 -317
  208. data/lib/active_support/testing/performance/jruby.rb +0 -115
  209. data/lib/active_support/testing/performance/rubinius.rb +0 -113
  210. data/lib/active_support/testing/performance/ruby.rb +0 -152
  211. data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
  212. data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
  213. data/lib/active_support/time/autoload.rb +0 -5
  214. data/lib/active_support/whiny_nil.rb +0 -24
@@ -1,8 +1,24 @@
1
+ require 'active_support/deprecation/method_wrappers'
2
+
1
3
  class Module
2
- # Declare that a method has been deprecated.
3
4
  # deprecate :foo
4
- # deprecate :bar => 'message'
5
- # deprecate :foo, :bar, :baz => 'warning!', :qux => 'gone!'
5
+ # deprecate bar: 'message'
6
+ # deprecate :foo, :bar, baz: 'warning!', qux: 'gone!'
7
+ #
8
+ # You can also use custom deprecator instance:
9
+ #
10
+ # deprecate :foo, deprecator: MyLib::Deprecator.new
11
+ # deprecate :foo, bar: "warning!", deprecator: MyLib::Deprecator.new
12
+ #
13
+ # \Custom deprecators must respond to <tt>deprecation_warning(deprecated_method_name, message, caller_backtrace)</tt>
14
+ # method where you can implement your custom warning behavior.
15
+ #
16
+ # class MyLib::Deprecator
17
+ # def deprecation_warning(deprecated_method_name, message, caller_backtrace)
18
+ # message = "#{method_name} is deprecated and will be removed from MyLibrary | #{message}"
19
+ # Kernel.warn message
20
+ # end
21
+ # end
6
22
  def deprecate(*method_names)
7
23
  ActiveSupport::Deprecation.deprecate_methods(self, *method_names)
8
24
  end
@@ -5,10 +5,11 @@ class Module
5
5
  #
6
6
  # M::N.parent_name # => "M"
7
7
  def parent_name
8
- unless defined? @parent_name
8
+ if defined? @parent_name
9
+ @parent_name
10
+ else
9
11
  @parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
10
12
  end
11
- @parent_name
12
13
  end
13
14
 
14
15
  # Returns the module which contains this one according to its name.
@@ -26,7 +27,6 @@ class Module
26
27
  #
27
28
  # M.parent # => Object
28
29
  # Module.new.parent # => Object
29
- #
30
30
  def parent
31
31
  parent_name ? ActiveSupport::Inflector.constantize(parent_name) : Object
32
32
  end
@@ -43,7 +43,6 @@ class Module
43
43
  # M.parents # => [Object]
44
44
  # M::N.parents # => [M, Object]
45
45
  # X.parents # => [M, Object]
46
- #
47
46
  def parents
48
47
  parents = []
49
48
  if parent_name
@@ -57,32 +56,23 @@ class Module
57
56
  parents
58
57
  end
59
58
 
60
- if RUBY_VERSION < '1.9'
61
- # Returns the constants that have been defined locally by this object and
62
- # not in an ancestor. This method is exact if running under Ruby 1.9. In
63
- # previous versions it may miss some constants if their definition in some
64
- # ancestor is identical to their definition in the receiver.
65
- def local_constants
66
- inherited = {}
67
-
68
- ancestors.each do |anc|
69
- next if anc == self
70
- anc.constants.each { |const| inherited[const] = anc.const_get(const) }
71
- end
72
-
73
- constants.select do |const|
74
- !inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
75
- end
76
- end
77
- else
78
- def local_constants #:nodoc:
79
- constants(false)
80
- end
59
+ def local_constants #:nodoc:
60
+ constants(false)
81
61
  end
82
62
 
83
- # Returns the names of the constants defined locally rather than the
84
- # constants themselves. See <tt>local_constants</tt>.
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.
85
74
  def local_constant_names
75
+ ActiveSupport::Deprecation.warn 'Module#local_constant_names is deprecated, use Module#local_constants instead'
86
76
  local_constants.map { |c| c.to_s }
87
77
  end
88
78
  end
@@ -5,7 +5,7 @@ require 'active_support/core_ext/string/inflections'
5
5
  #++
6
6
  module QualifiedConstUtils
7
7
  def self.raise_if_absolute(path)
8
- raise NameError, "wrong constant name #$&" if path =~ /\A::[^:]+/
8
+ raise NameError.new("wrong constant name #$&") if path =~ /\A::[^:]+/
9
9
  end
10
10
 
11
11
  def self.names(path)
@@ -20,29 +20,17 @@ end
20
20
  #--
21
21
  # Qualified names are required to be relative because we are extending existing
22
22
  # methods that expect constant names, ie, relative paths of length 1. For example,
23
- # Object.const_get("::String") raises NameError and so does qualified_const_get.
23
+ # Object.const_get('::String') raises NameError and so does qualified_const_get.
24
24
  #++
25
25
  class Module
26
- if method(:const_defined?).arity == 1
27
- def qualified_const_defined?(path)
28
- QualifiedConstUtils.raise_if_absolute(path)
29
-
30
- QualifiedConstUtils.names(path).inject(self) do |mod, name|
31
- return unless mod.const_defined?(name)
32
- mod.const_get(name)
33
- end
34
- return true
35
- end
36
- else
37
- def qualified_const_defined?(path, search_parents=true)
38
- QualifiedConstUtils.raise_if_absolute(path)
26
+ def qualified_const_defined?(path, search_parents=true)
27
+ QualifiedConstUtils.raise_if_absolute(path)
39
28
 
40
- QualifiedConstUtils.names(path).inject(self) do |mod, name|
41
- return unless mod.const_defined?(name, search_parents)
42
- mod.const_get(name)
43
- end
44
- return true
29
+ QualifiedConstUtils.names(path).inject(self) do |mod, name|
30
+ return unless mod.const_defined?(name, search_parents)
31
+ mod.const_get(name)
45
32
  end
33
+ return true
46
34
  end
47
35
 
48
36
  def qualified_const_get(path)
@@ -1,12 +1,8 @@
1
1
  class Module
2
2
  def remove_possible_method(method)
3
3
  if method_defined?(method) || private_method_defined?(method)
4
- remove_method(method)
4
+ undef_method(method)
5
5
  end
6
- rescue NameError
7
- # If the requested method is defined on a superclass or included module,
8
- # method_defined? returns true but remove_method throws a NameError.
9
- # Ignore this.
10
6
  end
11
7
 
12
8
  def redefine_method(method, &block)
@@ -1,2 +1,4 @@
1
1
  require 'active_support/core_ext/numeric/bytes'
2
2
  require 'active_support/core_ext/numeric/time'
3
+ require 'active_support/core_ext/numeric/conversions'
4
+ require 'active_support/core_ext/numeric/infinite_comparable'
@@ -0,0 +1,135 @@
1
+ require 'active_support/core_ext/big_decimal/conversions'
2
+ require 'active_support/number_helper'
3
+
4
+ class Numeric
5
+
6
+ # Provides options for converting numbers into formatted strings.
7
+ # Options are provided for phone numbers, currency, percentage,
8
+ # precision, positional notation, file size and pretty printing.
9
+ #
10
+ # ==== Options
11
+ #
12
+ # For details on which formats use which options, see ActiveSupport::NumberHelper
13
+ #
14
+ # ==== Examples
15
+ #
16
+ # Phone Numbers:
17
+ # 5551234.to_s(:phone) # => 555-1234
18
+ # 1235551234.to_s(:phone) # => 123-555-1234
19
+ # 1235551234.to_s(:phone, area_code: true) # => (123) 555-1234
20
+ # 1235551234.to_s(:phone, delimiter: ' ') # => 123 555 1234
21
+ # 1235551234.to_s(:phone, area_code: true, extension: 555) # => (123) 555-1234 x 555
22
+ # 1235551234.to_s(:phone, country_code: 1) # => +1-123-555-1234
23
+ # 1235551234.to_s(:phone, country_code: 1, extension: 1343, delimiter: '.')
24
+ # # => +1.123.555.1234 x 1343
25
+ #
26
+ # Currency:
27
+ # 1234567890.50.to_s(:currency) # => $1,234,567,890.50
28
+ # 1234567890.506.to_s(:currency) # => $1,234,567,890.51
29
+ # 1234567890.506.to_s(:currency, precision: 3) # => $1,234,567,890.506
30
+ # 1234567890.506.to_s(:currency, locale: :fr) # => 1 234 567 890,51 €
31
+ # -1234567890.50.to_s(:currency, negative_format: '(%u%n)')
32
+ # # => ($1,234,567,890.50)
33
+ # 1234567890.50.to_s(:currency, unit: '&pound;', separator: ',', delimiter: '')
34
+ # # => &pound;1234567890,50
35
+ # 1234567890.50.to_s(:currency, unit: '&pound;', separator: ',', delimiter: '', format: '%n %u')
36
+ # # => 1234567890,50 &pound;
37
+ #
38
+ # Percentage:
39
+ # 100.to_s(:percentage) # => 100.000%
40
+ # 100.to_s(:percentage, precision: 0) # => 100%
41
+ # 1000.to_s(:percentage, delimiter: '.', separator: ',') # => 1.000,000%
42
+ # 302.24398923423.to_s(:percentage, precision: 5) # => 302.24399%
43
+ # 1000.to_s(:percentage, locale: :fr) # => 1 000,000%
44
+ # 100.to_s(:percentage, format: '%n %') # => 100 %
45
+ #
46
+ # Delimited:
47
+ # 12345678.to_s(:delimited) # => 12,345,678
48
+ # 12345678.05.to_s(:delimited) # => 12,345,678.05
49
+ # 12345678.to_s(:delimited, delimiter: '.') # => 12.345.678
50
+ # 12345678.to_s(:delimited, delimiter: ',') # => 12,345,678
51
+ # 12345678.05.to_s(:delimited, separator: ' ') # => 12,345,678 05
52
+ # 12345678.05.to_s(:delimited, locale: :fr) # => 12 345 678,05
53
+ # 98765432.98.to_s(:delimited, delimiter: ' ', separator: ',')
54
+ # # => 98 765 432,98
55
+ #
56
+ # Rounded:
57
+ # 111.2345.to_s(:rounded) # => 111.235
58
+ # 111.2345.to_s(:rounded, precision: 2) # => 111.23
59
+ # 13.to_s(:rounded, precision: 5) # => 13.00000
60
+ # 389.32314.to_s(:rounded, precision: 0) # => 389
61
+ # 111.2345.to_s(:rounded, significant: true) # => 111
62
+ # 111.2345.to_s(:rounded, precision: 1, significant: true) # => 100
63
+ # 13.to_s(:rounded, precision: 5, significant: true) # => 13.000
64
+ # 111.234.to_s(:rounded, locale: :fr) # => 111,234
65
+ # 13.to_s(:rounded, precision: 5, significant: true, strip_insignificant_zeros: true)
66
+ # # => 13
67
+ # 389.32314.to_s(:rounded, precision: 4, significant: true) # => 389.3
68
+ # 1111.2345.to_s(:rounded, precision: 2, separator: ',', delimiter: '.')
69
+ # # => 1.111,23
70
+ #
71
+ # Human-friendly size in Bytes:
72
+ # 123.to_s(:human_size) # => 123 Bytes
73
+ # 1234.to_s(:human_size) # => 1.21 KB
74
+ # 12345.to_s(:human_size) # => 12.1 KB
75
+ # 1234567.to_s(:human_size) # => 1.18 MB
76
+ # 1234567890.to_s(:human_size) # => 1.15 GB
77
+ # 1234567890123.to_s(:human_size) # => 1.12 TB
78
+ # 1234567.to_s(:human_size, precision: 2) # => 1.2 MB
79
+ # 483989.to_s(:human_size, precision: 2) # => 470 KB
80
+ # 1234567.to_s(:human_size, precision: 2, separator: ',') # => 1,2 MB
81
+ # 1234567890123.to_s(:human_size, precision: 5) # => "1.1229 TB"
82
+ # 524288000.to_s(:human_size, precision: 5) # => "500 MB"
83
+ #
84
+ # Human-friendly format:
85
+ # 123.to_s(:human) # => "123"
86
+ # 1234.to_s(:human) # => "1.23 Thousand"
87
+ # 12345.to_s(:human) # => "12.3 Thousand"
88
+ # 1234567.to_s(:human) # => "1.23 Million"
89
+ # 1234567890.to_s(:human) # => "1.23 Billion"
90
+ # 1234567890123.to_s(:human) # => "1.23 Trillion"
91
+ # 1234567890123456.to_s(:human) # => "1.23 Quadrillion"
92
+ # 1234567890123456789.to_s(:human) # => "1230 Quadrillion"
93
+ # 489939.to_s(:human, precision: 2) # => "490 Thousand"
94
+ # 489939.to_s(:human, precision: 4) # => "489.9 Thousand"
95
+ # 1234567.to_s(:human, precision: 4,
96
+ # significant: false) # => "1.2346 Million"
97
+ # 1234567.to_s(:human, precision: 1,
98
+ # separator: ',',
99
+ # significant: false) # => "1,2 Million"
100
+ def to_formatted_s(format = :default, options = {})
101
+ case format
102
+ when :phone
103
+ return ActiveSupport::NumberHelper.number_to_phone(self, options)
104
+ when :currency
105
+ return ActiveSupport::NumberHelper.number_to_currency(self, options)
106
+ when :percentage
107
+ return ActiveSupport::NumberHelper.number_to_percentage(self, options)
108
+ when :delimited
109
+ return ActiveSupport::NumberHelper.number_to_delimited(self, options)
110
+ when :rounded
111
+ return ActiveSupport::NumberHelper.number_to_rounded(self, options)
112
+ when :human
113
+ return ActiveSupport::NumberHelper.number_to_human(self, options)
114
+ when :human_size
115
+ return ActiveSupport::NumberHelper.number_to_human_size(self, options)
116
+ else
117
+ self.to_default_s
118
+ end
119
+ end
120
+
121
+ [Float, Fixnum, Bignum, BigDecimal].each do |klass|
122
+ klass.send(:alias_method, :to_default_s, :to_s)
123
+
124
+ klass.send(:define_method, :to_s) do |*args|
125
+ if args[0].is_a?(Symbol)
126
+ format = args[0]
127
+ options = args[1] || {}
128
+
129
+ self.to_formatted_s(format, options)
130
+ else
131
+ to_default_s(*args)
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_support/core_ext/infinite_comparable'
2
+
3
+ class Float
4
+ include InfiniteComparable
5
+ end
6
+
7
+ class BigDecimal
8
+ include InfiniteComparable
9
+ end
@@ -8,13 +8,13 @@ class Numeric
8
8
  # These methods use Time#advance for precise date calculations when using from_now, ago, etc.
9
9
  # as well as adding or subtracting their results from a Time object. For example:
10
10
  #
11
- # # equivalent to Time.now.advance(:months => 1)
11
+ # # equivalent to Time.current.advance(months: 1)
12
12
  # 1.month.from_now
13
13
  #
14
- # # equivalent to Time.now.advance(:years => 2)
14
+ # # equivalent to Time.current.advance(years: 2)
15
15
  # 2.years.from_now
16
16
  #
17
- # # equivalent to Time.now.advance(:months => 4, :years => 5)
17
+ # # equivalent to Time.current.advance(months: 4, years: 5)
18
18
  # (4.months + 5.years).from_now
19
19
  #
20
20
  # While these methods provide precise calculation when used as in the examples above, care
@@ -28,9 +28,9 @@ class Numeric
28
28
  # 1.year.to_f.from_now
29
29
  #
30
30
  # In such cases, Ruby's core
31
- # Date[http://stdlib.rubyonrails.org/libdoc/date/rdoc/index.html] and
32
- # Time[http://stdlib.rubyonrails.org/libdoc/time/rdoc/index.html] should be used for precision
33
- # date and time arithmetic
31
+ # Date[http://ruby-doc.org/stdlib/libdoc/date/rdoc/Date.html] and
32
+ # Time[http://ruby-doc.org/stdlib/libdoc/time/rdoc/Time.html] should be used for precision
33
+ # date and time arithmetic.
34
34
  def seconds
35
35
  ActiveSupport::Duration.new(self, [[:seconds, self]])
36
36
  end
@@ -1,6 +1,7 @@
1
1
  require 'active_support/core_ext/object/acts_like'
2
2
  require 'active_support/core_ext/object/blank'
3
3
  require 'active_support/core_ext/object/duplicable'
4
+ require 'active_support/core_ext/object/deep_dup'
4
5
  require 'active_support/core_ext/object/try'
5
6
  require 'active_support/core_ext/object/inclusion'
6
7
 
@@ -1,9 +1,9 @@
1
1
  class Object
2
2
  # A duck-type assistant method. For example, Active Support extends Date
3
- # to define an acts_like_date? method, and extends Time to define
4
- # acts_like_time?. As a result, we can do "x.acts_like?(:time)" and
5
- # "x.acts_like?(:date)" to do duck-type-safe comparisons, since classes that
6
- # we want to act like Time simply need to define an acts_like_time? method.
3
+ # to define an <tt>acts_like_date?</tt> method, and extends Time to define
4
+ # <tt>acts_like_time?</tt>. As a result, we can do <tt>x.acts_like?(:time)</tt> and
5
+ # <tt>x.acts_like?(:date)</tt> to do duck-type-safe comparisons, since classes that
6
+ # we want to act like Time simply need to define an <tt>acts_like_time?</tt> method.
7
7
  def acts_like?(duck)
8
8
  respond_to? :"acts_like_#{duck}?"
9
9
  end
@@ -1,9 +1,8 @@
1
1
  # encoding: utf-8
2
- require 'active_support/core_ext/string/encoding'
3
2
 
4
3
  class Object
5
4
  # An object is blank if it's false, empty, or a whitespace string.
6
- # For example, "", " ", +nil+, [], and {} are all blank.
5
+ # For example, '', ' ', +nil+, [], and {} are all blank.
7
6
  #
8
7
  # This simplifies:
9
8
  #
@@ -44,7 +43,6 @@ class NilClass
44
43
  # +nil+ is blank:
45
44
  #
46
45
  # nil.blank? # => true
47
- #
48
46
  def blank?
49
47
  true
50
48
  end
@@ -54,7 +52,6 @@ class FalseClass
54
52
  # +false+ is blank:
55
53
  #
56
54
  # false.blank? # => true
57
- #
58
55
  def blank?
59
56
  true
60
57
  end
@@ -64,7 +61,6 @@ class TrueClass
64
61
  # +true+ is not blank:
65
62
  #
66
63
  # true.blank? # => false
67
- #
68
64
  def blank?
69
65
  false
70
66
  end
@@ -75,7 +71,6 @@ class Array
75
71
  #
76
72
  # [].blank? # => true
77
73
  # [1,2,3].blank? # => false
78
- #
79
74
  alias_method :blank?, :empty?
80
75
  end
81
76
 
@@ -83,29 +78,19 @@ class Hash
83
78
  # A hash is blank if it's empty:
84
79
  #
85
80
  # {}.blank? # => true
86
- # {:key => 'value'}.blank? # => false
87
- #
81
+ # { key: 'value' }.blank? # => false
88
82
  alias_method :blank?, :empty?
89
83
  end
90
84
 
91
85
  class String
92
- # 0x3000: fullwidth whitespace
93
- NON_WHITESPACE_REGEXP = %r![^\s#{[0x3000].pack("U")}]!
94
-
95
86
  # A string is blank if it's empty or contains whitespaces only:
96
87
  #
97
- # "".blank? # => true
98
- # " ".blank? # => true
99
- # " ".blank? # => true
100
- # " something here ".blank? # => false
101
- #
88
+ # ''.blank? # => true
89
+ # ' '.blank? # => true
90
+ # ' '.blank? # => true
91
+ # ' something here '.blank? # => false
102
92
  def blank?
103
- # 1.8 does not takes [:space:] properly
104
- if encoding_aware?
105
- self !~ /[^[:space:]]/
106
- else
107
- self !~ NON_WHITESPACE_REGEXP
108
- end
93
+ self !~ /[^[:space:]]/
109
94
  end
110
95
  end
111
96
 
@@ -114,7 +99,6 @@ class Numeric #:nodoc:
114
99
  #
115
100
  # 1.blank? # => false
116
101
  # 0.blank? # => false
117
- #
118
102
  def blank?
119
103
  false
120
104
  end