activesupport 3.2.22.5 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +364 -135
- data/MIT-LICENSE +1 -1
- data/README.rdoc +6 -4
- 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/file_store.rb +14 -14
- 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/cache/strategy/local_cache.rb +48 -37
- data/lib/active_support/cache.rb +228 -183
- data/lib/active_support/callbacks.rb +201 -253
- 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/array/access.rb +17 -9
- data/lib/active_support/core_ext/array/conversions.rb +115 -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 +13 -16
- data/lib/active_support/core_ext/array.rb +0 -1
- data/lib/active_support/core_ext/benchmark.rb +7 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -24
- data/lib/active_support/core_ext/class/attribute.rb +40 -32
- 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/calculations.rb +44 -187
- data/lib/active_support/core_ext/date/conversions.rb +16 -38
- data/lib/active_support/core_ext/date/zones.rb +25 -2
- data/lib/active_support/core_ext/date.rb +5 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +232 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +0 -1
- data/lib/active_support/core_ext/date_time/calculations.rb +77 -62
- data/lib/active_support/core_ext/date_time/conversions.rb +21 -33
- data/lib/active_support/core_ext/date_time/zones.rb +11 -8
- data/lib/active_support/core_ext/date_time.rb +4 -0
- data/lib/active_support/core_ext/enumerable.rb +26 -73
- data/lib/active_support/core_ext/file/atomic.rb +27 -11
- data/lib/active_support/core_ext/file.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/hash.rb +0 -1
- 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 +21 -0
- 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 +92 -49
- 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/module.rb +1 -3
- data/lib/active_support/core_ext/numeric/conversions.rb +135 -0
- data/lib/active_support/core_ext/numeric/time.rb +6 -6
- data/lib/active_support/core_ext/numeric.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 +8 -7
- 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/object.rb +1 -0
- data/lib/active_support/core_ext/proc.rb +3 -0
- data/lib/active_support/core_ext/range/conversions.rb +0 -2
- data/lib/active_support/core_ext/range/include_range.rb +3 -1
- data/lib/active_support/core_ext/range/overlaps.rb +1 -1
- data/lib/active_support/core_ext/range.rb +0 -2
- data/lib/active_support/core_ext/string/access.rb +95 -90
- data/lib/active_support/core_ext/string/conversions.rb +27 -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/string.rb +2 -3
- 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/calculations.rb +108 -184
- data/lib/active_support/core_ext/time/conversions.rb +27 -51
- 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/time.rb +5 -0
- data/lib/active_support/core_ext/uri.rb +13 -17
- data/lib/active_support/core_ext.rb +3 -2
- data/lib/active_support/dependencies/autoload.rb +47 -20
- data/lib/active_support/dependencies.rb +160 -141
- 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/deprecation.rb +39 -14
- 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 +127 -42
- 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/test_helper.rb +18 -15
- data/lib/active_support/log_subscriber.rb +35 -52
- data/lib/active_support/logger.rb +57 -0
- data/lib/active_support/logger_silence.rb +24 -0
- data/lib/active_support/message_encryptor.rb +38 -35
- data/lib/active_support/message_verifier.rb +9 -15
- data/lib/active_support/multibyte/chars.rb +80 -333
- data/lib/active_support/multibyte/unicode.rb +74 -64
- data/lib/active_support/multibyte.rb +5 -28
- data/lib/active_support/notifications/fanout.rb +112 -18
- data/lib/active_support/notifications/instrumenter.rb +33 -14
- data/lib/active_support/notifications.rb +79 -26
- data/lib/active_support/number_helper.rb +637 -0
- data/lib/active_support/ordered_hash.rb +8 -190
- data/lib/active_support/ordered_options.rb +21 -23
- data/lib/active_support/per_thread_registry.rb +52 -0
- 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/subscriber.rb +93 -0
- 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 +54 -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 +29 -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 +80 -43
- data/lib/active_support/values/time_zone.rb +81 -60
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +7 -6
- data/lib/active_support/xml_mini/jdom.rb +9 -11
- data/lib/active_support/xml_mini/libxml.rb +1 -2
- data/lib/active_support/xml_mini/libxmlsax.rb +2 -3
- data/lib/active_support/xml_mini/nokogiri.rb +1 -2
- data/lib/active_support/xml_mini/nokogirisax.rb +2 -3
- data/lib/active_support/xml_mini/rexml.rb +6 -8
- data/lib/active_support/xml_mini.rb +35 -17
- data/lib/active_support.rb +8 -21
- metadata +104 -72
- 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/rounding.rb +0 -19
- data/lib/active_support/core_ext/float.rb +0 -1
- 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/daemon.rb +0 -23
- data/lib/active_support/core_ext/process.rb +0 -1
- 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/string/xchar.rb +0 -18
- 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/jruby.rb +0 -115
- data/lib/active_support/testing/performance/rubinius.rb +0 -113
- 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/testing/performance/ruby.rb +0 -152
- data/lib/active_support/testing/performance.rb +0 -317
- data/lib/active_support/time/autoload.rb +0 -5
- data/lib/active_support/whiny_nil.rb +0 -24
|
@@ -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)
|
|
@@ -5,8 +5,6 @@ require 'active_support/core_ext/module/reachable'
|
|
|
5
5
|
require 'active_support/core_ext/module/attribute_accessors'
|
|
6
6
|
require 'active_support/core_ext/module/attr_internal'
|
|
7
7
|
require 'active_support/core_ext/module/delegation'
|
|
8
|
-
require 'active_support/core_ext/module/synchronization'
|
|
9
8
|
require 'active_support/core_ext/module/deprecation'
|
|
10
9
|
require 'active_support/core_ext/module/remove_method'
|
|
11
|
-
require 'active_support/core_ext/module/
|
|
12
|
-
require 'active_support/core_ext/module/qualified_const'
|
|
10
|
+
require 'active_support/core_ext/module/qualified_const'
|
|
@@ -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,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
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'active_support/core_ext/object/duplicable'
|
|
2
|
+
|
|
3
|
+
class Object
|
|
4
|
+
# Returns a deep copy of object if it's duplicable. If it's
|
|
5
|
+
# not duplicable, returns +self+.
|
|
6
|
+
#
|
|
7
|
+
# object = Object.new
|
|
8
|
+
# dup = object.deep_dup
|
|
9
|
+
# dup.instance_variable_set(:@a, 1)
|
|
10
|
+
#
|
|
11
|
+
# object.instance_variable_defined?(:@a) #=> false
|
|
12
|
+
# dup.instance_variable_defined?(:@a) #=> true
|
|
13
|
+
def deep_dup
|
|
14
|
+
duplicable? ? dup : self
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class Array
|
|
19
|
+
# Returns a deep copy of array.
|
|
20
|
+
#
|
|
21
|
+
# array = [1, [2, 3]]
|
|
22
|
+
# dup = array.deep_dup
|
|
23
|
+
# dup[1][2] = 4
|
|
24
|
+
#
|
|
25
|
+
# array[1][2] #=> nil
|
|
26
|
+
# dup[1][2] #=> 4
|
|
27
|
+
def deep_dup
|
|
28
|
+
map { |it| it.deep_dup }
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class Hash
|
|
33
|
+
# Returns a deep copy of hash.
|
|
34
|
+
#
|
|
35
|
+
# hash = { a: { b: 'b' } }
|
|
36
|
+
# dup = hash.deep_dup
|
|
37
|
+
# dup[:a][:c] = 'c'
|
|
38
|
+
#
|
|
39
|
+
# hash[:a][:c] #=> nil
|
|
40
|
+
# dup[:a][:c] #=> "c"
|
|
41
|
+
def deep_dup
|
|
42
|
+
each_with_object(dup) do |(key, value), hash|
|
|
43
|
+
hash[key.deep_dup] = value.deep_dup
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
class Object
|
|
20
20
|
# Can you safely dup this object?
|
|
21
21
|
#
|
|
22
|
-
# False for +nil+, +false+, +true+,
|
|
22
|
+
# False for +nil+, +false+, +true+, symbol, and number objects;
|
|
23
23
|
# true otherwise.
|
|
24
24
|
def duplicable?
|
|
25
25
|
true
|
|
@@ -31,7 +31,6 @@ class NilClass
|
|
|
31
31
|
#
|
|
32
32
|
# nil.duplicable? # => false
|
|
33
33
|
# nil.dup # => TypeError: can't dup NilClass
|
|
34
|
-
#
|
|
35
34
|
def duplicable?
|
|
36
35
|
false
|
|
37
36
|
end
|
|
@@ -42,7 +41,6 @@ class FalseClass
|
|
|
42
41
|
#
|
|
43
42
|
# false.duplicable? # => false
|
|
44
43
|
# false.dup # => TypeError: can't dup FalseClass
|
|
45
|
-
#
|
|
46
44
|
def duplicable?
|
|
47
45
|
false
|
|
48
46
|
end
|
|
@@ -53,7 +51,6 @@ class TrueClass
|
|
|
53
51
|
#
|
|
54
52
|
# true.duplicable? # => false
|
|
55
53
|
# true.dup # => TypeError: can't dup TrueClass
|
|
56
|
-
#
|
|
57
54
|
def duplicable?
|
|
58
55
|
false
|
|
59
56
|
end
|
|
@@ -64,7 +61,6 @@ class Symbol
|
|
|
64
61
|
#
|
|
65
62
|
# :my_symbol.duplicable? # => false
|
|
66
63
|
# :my_symbol.dup # => TypeError: can't dup Symbol
|
|
67
|
-
#
|
|
68
64
|
def duplicable?
|
|
69
65
|
false
|
|
70
66
|
end
|
|
@@ -75,31 +71,6 @@ class Numeric
|
|
|
75
71
|
#
|
|
76
72
|
# 3.duplicable? # => false
|
|
77
73
|
# 3.dup # => TypeError: can't dup Fixnum
|
|
78
|
-
#
|
|
79
|
-
def duplicable?
|
|
80
|
-
false
|
|
81
|
-
end
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
class Class
|
|
85
|
-
# Classes are not duplicable:
|
|
86
|
-
#
|
|
87
|
-
# c = Class.new # => #<Class:0x10328fd80>
|
|
88
|
-
# c.dup # => #<Class:0x10328fd80>
|
|
89
|
-
#
|
|
90
|
-
# Note +dup+ returned the same class object.
|
|
91
|
-
def duplicable?
|
|
92
|
-
false
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
class Module
|
|
97
|
-
# Modules are not duplicable:
|
|
98
|
-
#
|
|
99
|
-
# m = Module.new # => #<Module:0x10328b6e0>
|
|
100
|
-
# m.dup # => #<Module:0x10328b6e0>
|
|
101
|
-
#
|
|
102
|
-
# Note +dup+ returned the same module object.
|
|
103
74
|
def duplicable?
|
|
104
75
|
false
|
|
105
76
|
end
|
|
@@ -1,24 +1,25 @@
|
|
|
1
|
+
require 'active_support/deprecation'
|
|
2
|
+
|
|
1
3
|
class Object
|
|
2
|
-
# Returns true if this object is included in the argument
|
|
3
|
-
# any object which responds to +#include
|
|
4
|
+
# Returns true if this object is included in the argument. Argument must be
|
|
5
|
+
# any object which responds to +#include?+. Usage:
|
|
4
6
|
#
|
|
5
7
|
# characters = ["Konata", "Kagami", "Tsukasa"]
|
|
6
8
|
# "Konata".in?(characters) # => true
|
|
7
|
-
#
|
|
8
|
-
# character = "Konata"
|
|
9
|
-
# character.in?("Konata", "Kagami", "Tsukasa") # => true
|
|
10
9
|
#
|
|
11
|
-
# This will throw an ArgumentError if
|
|
10
|
+
# This will throw an ArgumentError if the argument doesn't respond
|
|
12
11
|
# to +#include?+.
|
|
13
12
|
def in?(*args)
|
|
14
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."
|
|
15
16
|
args.include? self
|
|
16
17
|
else
|
|
17
18
|
another_object = args.first
|
|
18
19
|
if another_object.respond_to? :include?
|
|
19
20
|
another_object.include? self
|
|
20
21
|
else
|
|
21
|
-
raise ArgumentError.new
|
|
22
|
+
raise ArgumentError.new 'The single parameter passed to #in? must respond to #include?'
|
|
22
23
|
end
|
|
23
24
|
end
|
|
24
25
|
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
class Object
|
|
2
|
-
# Returns a hash that maps instance variable names without "@" to their
|
|
3
|
-
# corresponding values.
|
|
2
|
+
# Returns a hash with string keys that maps instance variable names without "@" to their
|
|
3
|
+
# corresponding values.
|
|
4
4
|
#
|
|
5
5
|
# class C
|
|
6
6
|
# def initialize(x, y)
|
|
@@ -9,12 +9,11 @@ class Object
|
|
|
9
9
|
# end
|
|
10
10
|
#
|
|
11
11
|
# C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}
|
|
12
|
-
def instance_values
|
|
13
|
-
Hash[instance_variables.map { |name| [name
|
|
12
|
+
def instance_values
|
|
13
|
+
Hash[instance_variables.map { |name| [name[1..-1], instance_variable_get(name)] }]
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
# Returns an array of instance variable names including "@".
|
|
17
|
-
# both in Ruby 1.8 and 1.9.
|
|
16
|
+
# Returns an array of instance variable names as strings including "@".
|
|
18
17
|
#
|
|
19
18
|
# class C
|
|
20
19
|
# def initialize(x, y)
|
|
@@ -23,11 +22,7 @@ class Object
|
|
|
23
22
|
# end
|
|
24
23
|
#
|
|
25
24
|
# C.new(0, 1).instance_variable_names # => ["@y", "@x"]
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
instance_variables.map { |var| var.to_s }
|
|
29
|
-
end
|
|
30
|
-
else
|
|
31
|
-
alias_method :instance_variable_names, :instance_variables
|
|
25
|
+
def instance_variable_names
|
|
26
|
+
instance_variables.map { |var| var.to_s }
|
|
32
27
|
end
|
|
33
28
|
end
|
|
@@ -6,18 +6,21 @@ class Object
|
|
|
6
6
|
end
|
|
7
7
|
|
|
8
8
|
class NilClass
|
|
9
|
+
# Returns +self+.
|
|
9
10
|
def to_param
|
|
10
11
|
self
|
|
11
12
|
end
|
|
12
13
|
end
|
|
13
14
|
|
|
14
15
|
class TrueClass
|
|
16
|
+
# Returns +self+.
|
|
15
17
|
def to_param
|
|
16
18
|
self
|
|
17
19
|
end
|
|
18
20
|
end
|
|
19
21
|
|
|
20
22
|
class FalseClass
|
|
23
|
+
# Returns +self+.
|
|
21
24
|
def to_param
|
|
22
25
|
self
|
|
23
26
|
end
|
|
@@ -35,12 +38,12 @@ class Hash
|
|
|
35
38
|
# Returns a string representation of the receiver suitable for use as a URL
|
|
36
39
|
# query string:
|
|
37
40
|
#
|
|
38
|
-
# {:
|
|
41
|
+
# {name: 'David', nationality: 'Danish'}.to_param
|
|
39
42
|
# # => "name=David&nationality=Danish"
|
|
40
43
|
#
|
|
41
44
|
# An optional namespace can be passed to enclose the param names:
|
|
42
45
|
#
|
|
43
|
-
# {:
|
|
46
|
+
# {name: 'David', nationality: 'Danish'}.to_param('user')
|
|
44
47
|
# # => "user[name]=David&user[nationality]=Danish"
|
|
45
48
|
#
|
|
46
49
|
# The string pairs "key=value" that conform the query string
|