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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +325 -136
- data/MIT-LICENSE +1 -1
- data/README.rdoc +4 -2
- data/lib/active_support.rb +8 -21
- data/lib/active_support/backtrace_cleaner.rb +33 -25
- data/lib/active_support/basic_object.rb +7 -17
- data/lib/active_support/benchmarkable.rb +19 -15
- data/lib/active_support/buffered_logger.rb +9 -113
- data/lib/active_support/cache.rb +203 -171
- data/lib/active_support/cache/file_store.rb +12 -12
- data/lib/active_support/cache/mem_cache_store.rb +24 -30
- data/lib/active_support/cache/memory_store.rb +2 -0
- data/lib/active_support/callbacks.rb +195 -247
- data/lib/active_support/concern.rb +16 -23
- data/lib/active_support/concurrency/latch.rb +27 -0
- data/lib/active_support/configurable.rb +69 -12
- data/lib/active_support/core_ext.rb +1 -0
- data/lib/active_support/core_ext/array.rb +0 -1
- data/lib/active_support/core_ext/array/access.rb +17 -9
- data/lib/active_support/core_ext/array/conversions.rb +113 -55
- data/lib/active_support/core_ext/array/extract_options.rb +2 -2
- data/lib/active_support/core_ext/array/grouping.rb +21 -22
- data/lib/active_support/core_ext/array/uniq_by.rb +12 -9
- data/lib/active_support/core_ext/array/wrap.rb +11 -14
- data/lib/active_support/core_ext/big_decimal/conversions.rb +7 -24
- data/lib/active_support/core_ext/class/attribute.rb +12 -8
- data/lib/active_support/core_ext/class/attribute_accessors.rb +14 -12
- data/lib/active_support/core_ext/class/delegating_attributes.rb +15 -19
- data/lib/active_support/core_ext/class/subclasses.rb +11 -5
- data/lib/active_support/core_ext/date.rb +6 -0
- data/lib/active_support/core_ext/date/calculations.rb +34 -188
- data/lib/active_support/core_ext/date/conversions.rb +16 -38
- data/lib/active_support/core_ext/date/infinite_comparable.rb +5 -0
- data/lib/active_support/core_ext/date/zones.rb +25 -2
- data/lib/active_support/core_ext/date_and_time/calculations.rb +232 -0
- data/lib/active_support/core_ext/date_time.rb +5 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +0 -1
- data/lib/active_support/core_ext/date_time/calculations.rb +73 -65
- data/lib/active_support/core_ext/date_time/conversions.rb +21 -33
- data/lib/active_support/core_ext/date_time/infinite_comparable.rb +5 -0
- data/lib/active_support/core_ext/date_time/zones.rb +11 -8
- data/lib/active_support/core_ext/enumerable.rb +26 -73
- data/lib/active_support/core_ext/file.rb +0 -1
- data/lib/active_support/core_ext/file/atomic.rb +27 -11
- data/lib/active_support/core_ext/hash.rb +0 -1
- data/lib/active_support/core_ext/hash/conversions.rb +145 -79
- data/lib/active_support/core_ext/hash/deep_merge.rb +14 -8
- data/lib/active_support/core_ext/hash/diff.rb +5 -4
- data/lib/active_support/core_ext/hash/except.rb +1 -9
- data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -5
- data/lib/active_support/core_ext/hash/keys.rb +108 -24
- data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
- data/lib/active_support/core_ext/hash/slice.rb +12 -12
- data/lib/active_support/core_ext/infinite_comparable.rb +35 -0
- data/lib/active_support/core_ext/integer/inflections.rb +13 -1
- data/lib/active_support/core_ext/integer/time.rb +17 -12
- data/lib/active_support/core_ext/kernel/debugger.rb +2 -2
- data/lib/active_support/core_ext/kernel/reporting.rb +36 -22
- data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
- data/lib/active_support/core_ext/load_error.rb +7 -5
- data/lib/active_support/core_ext/logger.rb +7 -23
- data/lib/active_support/core_ext/marshal.rb +19 -0
- data/lib/active_support/core_ext/module.rb +1 -3
- data/lib/active_support/core_ext/module/aliasing.rb +8 -9
- data/lib/active_support/core_ext/module/anonymous.rb +2 -7
- data/lib/active_support/core_ext/module/attr_internal.rb +0 -1
- data/lib/active_support/core_ext/module/attribute_accessors.rb +12 -10
- data/lib/active_support/core_ext/module/delegation.rb +57 -40
- data/lib/active_support/core_ext/module/deprecation.rb +19 -3
- data/lib/active_support/core_ext/module/introspection.rb +17 -27
- data/lib/active_support/core_ext/module/qualified_const.rb +8 -20
- data/lib/active_support/core_ext/module/remove_method.rb +1 -5
- data/lib/active_support/core_ext/numeric.rb +2 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +135 -0
- data/lib/active_support/core_ext/numeric/infinite_comparable.rb +9 -0
- data/lib/active_support/core_ext/numeric/time.rb +6 -6
- data/lib/active_support/core_ext/object.rb +1 -0
- data/lib/active_support/core_ext/object/acts_like.rb +4 -4
- data/lib/active_support/core_ext/object/blank.rb +7 -23
- data/lib/active_support/core_ext/object/deep_dup.rb +46 -0
- data/lib/active_support/core_ext/object/duplicable.rb +1 -30
- data/lib/active_support/core_ext/object/inclusion.rb +6 -6
- data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
- data/lib/active_support/core_ext/object/to_json.rb +8 -0
- data/lib/active_support/core_ext/object/to_param.rb +5 -2
- data/lib/active_support/core_ext/object/try.rb +46 -25
- data/lib/active_support/core_ext/object/with_options.rb +7 -8
- data/lib/active_support/core_ext/proc.rb +3 -0
- data/lib/active_support/core_ext/range.rb +0 -2
- data/lib/active_support/core_ext/range/conversions.rb +0 -2
- data/lib/active_support/core_ext/range/include_range.rb +1 -1
- data/lib/active_support/core_ext/range/overlaps.rb +1 -1
- data/lib/active_support/core_ext/string.rb +2 -2
- data/lib/active_support/core_ext/string/access.rb +95 -90
- data/lib/active_support/core_ext/string/conversions.rb +29 -38
- data/lib/active_support/core_ext/string/encoding.rb +6 -9
- data/lib/active_support/core_ext/string/filters.rb +24 -18
- data/lib/active_support/core_ext/string/indent.rb +43 -0
- data/lib/active_support/core_ext/string/inflections.rb +70 -60
- data/lib/active_support/core_ext/string/inquiry.rb +2 -2
- data/lib/active_support/core_ext/string/multibyte.rb +41 -64
- data/lib/active_support/core_ext/string/output_safety.rb +59 -51
- data/lib/active_support/core_ext/string/zones.rb +13 -0
- data/lib/active_support/core_ext/struct.rb +6 -0
- data/lib/active_support/core_ext/thread.rb +74 -0
- data/lib/active_support/core_ext/time.rb +6 -0
- data/lib/active_support/core_ext/time/calculations.rb +105 -193
- data/lib/active_support/core_ext/time/conversions.rb +27 -51
- data/lib/active_support/core_ext/time/infinite_comparable.rb +5 -0
- data/lib/active_support/core_ext/time/marshal.rb +0 -27
- data/lib/active_support/core_ext/time/zones.rb +27 -17
- data/lib/active_support/core_ext/uri.rb +13 -17
- data/lib/active_support/dependencies.rb +160 -141
- data/lib/active_support/dependencies/autoload.rb +47 -20
- data/lib/active_support/deprecation.rb +39 -14
- data/lib/active_support/deprecation/behaviors.rb +44 -30
- data/lib/active_support/deprecation/instance_delegator.rb +24 -0
- data/lib/active_support/deprecation/method_wrappers.rb +33 -18
- data/lib/active_support/deprecation/proxy_wrappers.rb +58 -13
- data/lib/active_support/deprecation/reporting.rb +40 -11
- data/lib/active_support/descendants_tracker.rb +34 -19
- data/lib/active_support/duration.rb +6 -8
- data/lib/active_support/file_update_checker.rb +63 -47
- data/lib/active_support/gzip.rb +11 -5
- data/lib/active_support/hash_with_indifferent_access.rb +112 -37
- data/lib/active_support/i18n.rb +4 -0
- data/lib/active_support/i18n_railtie.rb +5 -22
- data/lib/active_support/inflections.rb +14 -12
- data/lib/active_support/inflector/inflections.rb +108 -71
- data/lib/active_support/inflector/methods.rb +181 -160
- data/lib/active_support/inflector/transliterate.rb +16 -17
- data/lib/active_support/json/decoding.rb +18 -17
- data/lib/active_support/json/encoding.rb +93 -39
- data/lib/active_support/json/variable.rb +10 -1
- data/lib/active_support/key_generator.rb +75 -0
- data/lib/active_support/lazy_load_hooks.rb +21 -19
- data/lib/active_support/locale/en.yml +100 -3
- data/lib/active_support/log_subscriber.rb +56 -36
- data/lib/active_support/log_subscriber/test_helper.rb +18 -15
- data/lib/active_support/logger.rb +57 -0
- data/lib/active_support/logger_silence.rb +24 -0
- data/lib/active_support/message_encryptor.rb +32 -29
- data/lib/active_support/message_verifier.rb +8 -14
- data/lib/active_support/multibyte.rb +5 -28
- data/lib/active_support/multibyte/chars.rb +80 -333
- data/lib/active_support/multibyte/unicode.rb +74 -64
- data/lib/active_support/notifications.rb +57 -25
- data/lib/active_support/notifications/fanout.rb +105 -18
- data/lib/active_support/notifications/instrumenter.rb +32 -13
- data/lib/active_support/number_helper.rb +636 -0
- data/lib/active_support/ordered_hash.rb +8 -190
- data/lib/active_support/ordered_options.rb +21 -23
- data/lib/active_support/proxy_object.rb +13 -0
- data/lib/active_support/rails.rb +27 -0
- data/lib/active_support/railtie.rb +12 -32
- data/lib/active_support/rescuable.rb +9 -4
- data/lib/active_support/string_inquirer.rb +13 -8
- data/lib/active_support/tagged_logging.rb +51 -73
- data/lib/active_support/test_case.rb +46 -17
- data/lib/active_support/testing/assertions.rb +56 -26
- data/lib/active_support/testing/autorun.rb +5 -0
- data/lib/active_support/testing/constant_lookup.rb +52 -0
- data/lib/active_support/testing/declarative.rb +1 -1
- data/lib/active_support/testing/deprecation.rb +0 -19
- data/lib/active_support/testing/isolation.rb +25 -58
- data/lib/active_support/testing/pending.rb +5 -43
- data/lib/active_support/testing/setup_and_teardown.rb +6 -92
- data/lib/active_support/testing/tagged_logging.rb +25 -0
- data/lib/active_support/time.rb +6 -21
- data/lib/active_support/time_with_zone.rb +78 -43
- data/lib/active_support/values/time_zone.rb +77 -58
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +4 -4
- data/lib/active_support/xml_mini.rb +35 -17
- data/lib/active_support/xml_mini/jdom.rb +9 -17
- data/lib/active_support/xml_mini/libxml.rb +1 -2
- data/lib/active_support/xml_mini/libxmlsax.rb +1 -2
- data/lib/active_support/xml_mini/nokogiri.rb +1 -2
- data/lib/active_support/xml_mini/nokogirisax.rb +1 -2
- data/lib/active_support/xml_mini/rexml.rb +6 -8
- metadata +107 -77
- data/lib/active_support/base64.rb +0 -54
- data/lib/active_support/core_ext/array/random_access.rb +0 -30
- data/lib/active_support/core_ext/date/freeze.rb +0 -33
- data/lib/active_support/core_ext/exception.rb +0 -3
- data/lib/active_support/core_ext/file/path.rb +0 -5
- data/lib/active_support/core_ext/float.rb +0 -1
- data/lib/active_support/core_ext/float/rounding.rb +0 -19
- data/lib/active_support/core_ext/hash/deep_dup.rb +0 -18
- data/lib/active_support/core_ext/io.rb +0 -15
- data/lib/active_support/core_ext/module/method_names.rb +0 -14
- data/lib/active_support/core_ext/module/synchronization.rb +0 -45
- data/lib/active_support/core_ext/process.rb +0 -1
- data/lib/active_support/core_ext/process/daemon.rb +0 -23
- data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
- data/lib/active_support/core_ext/range/cover.rb +0 -3
- data/lib/active_support/core_ext/rexml.rb +0 -46
- data/lib/active_support/core_ext/string/interpolation.rb +0 -2
- data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
- data/lib/active_support/memoizable.rb +0 -116
- data/lib/active_support/multibyte/exceptions.rb +0 -8
- data/lib/active_support/multibyte/utils.rb +0 -60
- data/lib/active_support/ruby/shim.rb +0 -22
- data/lib/active_support/security_utils.rb +0 -27
- data/lib/active_support/testing/mochaing.rb +0 -7
- data/lib/active_support/testing/performance.rb +0 -317
- data/lib/active_support/testing/performance/jruby.rb +0 -115
- data/lib/active_support/testing/performance/rubinius.rb +0 -113
- data/lib/active_support/testing/performance/ruby.rb +0 -152
- data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
- data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
- data/lib/active_support/time/autoload.rb +0 -5
- 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 :
|
5
|
-
# deprecate :foo, :bar, :
|
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
|
-
|
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
|
-
|
61
|
-
|
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
|
-
#
|
84
|
-
#
|
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
|
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(
|
23
|
+
# Object.const_get('::String') raises NameError and so does qualified_const_get.
|
24
24
|
#++
|
25
25
|
class Module
|
26
|
-
|
27
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
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)
|
@@ -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: '£', separator: ',', delimiter: '')
|
34
|
+
# # => £1234567890,50
|
35
|
+
# 1234567890.50.to_s(:currency, unit: '£', separator: ',', delimiter: '', format: '%n %u')
|
36
|
+
# # => 1234567890,50 £
|
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
|
@@ -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.
|
11
|
+
# # equivalent to Time.current.advance(months: 1)
|
12
12
|
# 1.month.from_now
|
13
13
|
#
|
14
|
-
# # equivalent to Time.
|
14
|
+
# # equivalent to Time.current.advance(years: 2)
|
15
15
|
# 2.years.from_now
|
16
16
|
#
|
17
|
-
# # equivalent to Time.
|
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://
|
32
|
-
# Time[http://
|
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
|
4
|
-
# acts_like_time
|
5
|
-
#
|
6
|
-
# we want to act like Time simply need to define an acts_like_time
|
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,
|
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
|
-
# {:
|
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
|
-
#
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
88
|
+
# ''.blank? # => true
|
89
|
+
# ' '.blank? # => true
|
90
|
+
# ' '.blank? # => true
|
91
|
+
# ' something here '.blank? # => false
|
102
92
|
def blank?
|
103
|
-
|
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
|