activesupport 4.1.15 → 4.2.11.3
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 +5 -5
- data/CHANGELOG.md +395 -574
- data/README.rdoc +7 -2
- data/lib/active_support.rb +19 -0
- data/lib/active_support/backtrace_cleaner.rb +4 -4
- data/lib/active_support/cache.rb +17 -19
- data/lib/active_support/cache/file_store.rb +5 -0
- data/lib/active_support/cache/mem_cache_store.rb +1 -1
- data/lib/active_support/cache/strategy/local_cache.rb +5 -4
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +5 -0
- data/lib/active_support/callbacks.rb +41 -33
- data/lib/active_support/concern.rb +10 -2
- data/lib/active_support/core_ext/array/access.rb +9 -1
- data/lib/active_support/core_ext/array/grouping.rb +5 -0
- data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +2 -0
- data/lib/active_support/core_ext/class/delegating_attributes.rb +4 -0
- data/lib/active_support/core_ext/class/subclasses.rb +0 -2
- data/lib/active_support/core_ext/date/conversions.rb +6 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +11 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +15 -0
- data/lib/active_support/core_ext/date_time.rb +1 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +34 -4
- data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +2 -2
- data/lib/active_support/core_ext/digest/uuid.rb +51 -0
- data/lib/active_support/core_ext/enumerable.rb +16 -0
- data/lib/active_support/core_ext/file/atomic.rb +1 -1
- data/lib/active_support/core_ext/hash.rb +1 -0
- data/lib/active_support/core_ext/hash/compact.rb +20 -16
- data/lib/active_support/core_ext/hash/conversions.rb +3 -5
- data/lib/active_support/core_ext/hash/except.rb +8 -2
- data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -1
- data/lib/active_support/core_ext/hash/keys.rb +10 -6
- data/lib/active_support/core_ext/hash/slice.rb +8 -2
- data/lib/active_support/core_ext/hash/transform_values.rb +23 -0
- data/lib/active_support/core_ext/integer/time.rb +0 -15
- data/lib/active_support/core_ext/kernel.rb +3 -2
- data/lib/active_support/core_ext/kernel/concern.rb +10 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +1 -1
- data/lib/active_support/core_ext/kernel/reporting.rb +15 -0
- data/lib/active_support/core_ext/load_error.rb +4 -1
- data/lib/active_support/core_ext/marshal.rb +8 -5
- data/lib/active_support/core_ext/module/aliasing.rb +2 -2
- data/lib/active_support/core_ext/module/delegation.rb +34 -18
- data/lib/active_support/core_ext/module/method_transplanting.rb +3 -1
- data/lib/active_support/core_ext/numeric/conversions.rb +11 -3
- data/lib/active_support/core_ext/numeric/time.rb +1 -34
- data/lib/active_support/core_ext/object.rb +1 -0
- data/lib/active_support/core_ext/object/blank.rb +2 -2
- data/lib/active_support/core_ext/object/duplicable.rb +62 -33
- data/lib/active_support/core_ext/object/itself.rb +15 -0
- data/lib/active_support/core_ext/object/json.rb +2 -2
- data/lib/active_support/core_ext/object/to_query.rb +2 -1
- data/lib/active_support/core_ext/object/try.rb +35 -13
- data/lib/active_support/core_ext/object/with_options.rb +30 -3
- data/lib/active_support/core_ext/string/access.rb +5 -5
- data/lib/active_support/core_ext/string/conversions.rb +1 -1
- data/lib/active_support/core_ext/string/filters.rb +44 -6
- data/lib/active_support/core_ext/string/inflections.rb +4 -1
- data/lib/active_support/core_ext/string/output_safety.rb +33 -14
- data/lib/active_support/core_ext/thread.rb +7 -0
- data/lib/active_support/core_ext/time.rb +1 -0
- data/lib/active_support/core_ext/time/calculations.rb +31 -7
- data/lib/active_support/core_ext/time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/time/conversions.rb +1 -1
- data/lib/active_support/dependencies.rb +32 -18
- data/lib/active_support/dependencies/autoload.rb +1 -1
- data/lib/active_support/deprecation.rb +1 -1
- data/lib/active_support/deprecation/behaviors.rb +1 -1
- data/lib/active_support/duration.rb +47 -5
- data/lib/active_support/gem_version.rb +4 -4
- data/lib/active_support/hash_with_indifferent_access.rb +35 -7
- data/lib/active_support/i18n_railtie.rb +1 -7
- data/lib/active_support/inflector/inflections.rb +2 -2
- data/lib/active_support/inflector/methods.rb +43 -19
- data/lib/active_support/json/decoding.rb +1 -1
- data/lib/active_support/json/encoding.rb +3 -2
- data/lib/active_support/logger.rb +36 -0
- data/lib/active_support/logger_silence.rb +4 -22
- data/lib/active_support/logger_thread_safe_level.rb +32 -0
- data/lib/active_support/message_encryptor.rb +10 -2
- data/lib/active_support/message_verifier.rb +11 -12
- data/lib/active_support/multibyte/chars.rb +1 -1
- data/lib/active_support/multibyte/unicode.rb +5 -4
- data/lib/active_support/notifications.rb +8 -3
- data/lib/active_support/notifications/fanout.rb +12 -7
- data/lib/active_support/number_helper.rb +12 -13
- data/lib/active_support/number_helper/number_to_currency_converter.rb +1 -1
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +1 -1
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +1 -1
- data/lib/active_support/per_thread_registry.rb +5 -3
- data/lib/active_support/test_case.rb +46 -12
- data/lib/active_support/testing/assertions.rb +1 -1
- data/lib/active_support/testing/constant_lookup.rb +1 -5
- data/lib/active_support/testing/declarative.rb +1 -25
- data/lib/active_support/testing/isolation.rb +16 -6
- data/lib/active_support/testing/tagged_logging.rb +1 -1
- data/lib/active_support/testing/time_helpers.rb +23 -16
- data/lib/active_support/time.rb +0 -2
- data/lib/active_support/time_with_zone.rb +48 -29
- data/lib/active_support/values/time_zone.rb +81 -75
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/xml_mini.rb +30 -15
- data/lib/active_support/xml_mini/libxml.rb +1 -3
- data/lib/active_support/xml_mini/libxmlsax.rb +1 -4
- data/lib/active_support/xml_mini/nokogiri.rb +1 -3
- data/lib/active_support/xml_mini/nokogirisax.rb +1 -3
- data/lib/active_support/xml_mini/rexml.rb +1 -3
- metadata +21 -36
- data/lib/active_support/core_ext/object/to_json.rb +0 -5
- data/lib/active_support/file_watcher.rb +0 -36
@@ -6,7 +6,8 @@ class Hash
|
|
6
6
|
# hash.transform_keys{ |key| key.to_s.upcase }
|
7
7
|
# # => {"NAME"=>"Rob", "AGE"=>"28"}
|
8
8
|
def transform_keys
|
9
|
-
|
9
|
+
return enum_for(:transform_keys) unless block_given?
|
10
|
+
result = self.class.new
|
10
11
|
each_key do |key|
|
11
12
|
result[yield(key)] = self[key]
|
12
13
|
end
|
@@ -16,6 +17,7 @@ class Hash
|
|
16
17
|
# Destructively convert all keys using the block operations.
|
17
18
|
# Same as transform_keys but modifies +self+.
|
18
19
|
def transform_keys!
|
20
|
+
return enum_for(:transform_keys!) unless block_given?
|
19
21
|
keys.each do |key|
|
20
22
|
self[yield(key)] = delete(key)
|
21
23
|
end
|
@@ -27,7 +29,7 @@ class Hash
|
|
27
29
|
# hash = { name: 'Rob', age: '28' }
|
28
30
|
#
|
29
31
|
# hash.stringify_keys
|
30
|
-
# # => {
|
32
|
+
# # => {"name"=>"Rob", "age"=>"28"}
|
31
33
|
def stringify_keys
|
32
34
|
transform_keys{ |key| key.to_s }
|
33
35
|
end
|
@@ -44,7 +46,7 @@ class Hash
|
|
44
46
|
# hash = { 'name' => 'Rob', 'age' => '28' }
|
45
47
|
#
|
46
48
|
# hash.symbolize_keys
|
47
|
-
# # => {
|
49
|
+
# # => {:name=>"Rob", :age=>"28"}
|
48
50
|
def symbolize_keys
|
49
51
|
transform_keys{ |key| key.to_sym rescue key }
|
50
52
|
end
|
@@ -57,9 +59,11 @@ class Hash
|
|
57
59
|
end
|
58
60
|
alias_method :to_options!, :symbolize_keys!
|
59
61
|
|
60
|
-
# Validate all keys in a hash match <tt>*valid_keys</tt>, raising
|
61
|
-
# on a mismatch.
|
62
|
-
#
|
62
|
+
# Validate all keys in a hash match <tt>*valid_keys</tt>, raising
|
63
|
+
# ArgumentError on a mismatch.
|
64
|
+
#
|
65
|
+
# Note that keys are treated differently than HashWithIndifferentAccess,
|
66
|
+
# meaning that string and symbol keys will not match.
|
63
67
|
#
|
64
68
|
# { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
|
65
69
|
# { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
|
@@ -1,6 +1,12 @@
|
|
1
1
|
class Hash
|
2
|
-
# Slice a hash to include only the given keys.
|
3
|
-
#
|
2
|
+
# Slice a hash to include only the given keys. Returns a hash containing
|
3
|
+
# the given keys.
|
4
|
+
#
|
5
|
+
# { a: 1, b: 2, c: 3, d: 4 }.slice(:a, :b)
|
6
|
+
# # => {:a=>1, :b=>2}
|
7
|
+
#
|
8
|
+
# This is useful for limiting an options hash to valid keys before
|
9
|
+
# passing to a method:
|
4
10
|
#
|
5
11
|
# def search(criteria = {})
|
6
12
|
# criteria.assert_valid_keys(:mass, :velocity, :time)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Hash
|
2
|
+
# Returns a new hash with the results of running +block+ once for every value.
|
3
|
+
# The keys are unchanged.
|
4
|
+
#
|
5
|
+
# { a: 1, b: 2, c: 3 }.transform_values { |x| x * 2 }
|
6
|
+
# # => { a: 2, b: 4, c: 6 }
|
7
|
+
def transform_values
|
8
|
+
return enum_for(:transform_values) unless block_given?
|
9
|
+
result = self.class.new
|
10
|
+
each do |key, value|
|
11
|
+
result[key] = yield(value)
|
12
|
+
end
|
13
|
+
result
|
14
|
+
end unless method_defined? :transform_values
|
15
|
+
|
16
|
+
# Destructive +transform_values+
|
17
|
+
def transform_values!
|
18
|
+
return enum_for(:transform_values!) unless block_given?
|
19
|
+
each do |key, value|
|
20
|
+
self[key] = yield(value)
|
21
|
+
end
|
22
|
+
end unless method_defined? :transform_values!
|
23
|
+
end
|
@@ -17,21 +17,6 @@ class Integer
|
|
17
17
|
#
|
18
18
|
# # equivalent to Time.now.advance(months: 4, years: 5)
|
19
19
|
# (4.months + 5.years).from_now
|
20
|
-
#
|
21
|
-
# While these methods provide precise calculation when used as in the examples
|
22
|
-
# above, care should be taken to note that this is not true if the result of
|
23
|
-
# +months+, +years+, etc is converted before use:
|
24
|
-
#
|
25
|
-
# # equivalent to 30.days.to_i.from_now
|
26
|
-
# 1.month.to_i.from_now
|
27
|
-
#
|
28
|
-
# # equivalent to 365.25.days.to_f.from_now
|
29
|
-
# 1.year.to_f.from_now
|
30
|
-
#
|
31
|
-
# In such cases, Ruby's core
|
32
|
-
# Date[http://ruby-doc.org/stdlib/libdoc/date/rdoc/Date.html] and
|
33
|
-
# Time[http://ruby-doc.org/stdlib/libdoc/time/rdoc/Time.html] should be used for precision
|
34
|
-
# date and time arithmetic.
|
35
20
|
def months
|
36
21
|
ActiveSupport::Duration.new(self * 30.days, [[:months, self]])
|
37
22
|
end
|
@@ -1,4 +1,5 @@
|
|
1
|
-
require 'active_support/core_ext/kernel/reporting'
|
2
1
|
require 'active_support/core_ext/kernel/agnostics'
|
3
|
-
require 'active_support/core_ext/kernel/
|
2
|
+
require 'active_support/core_ext/kernel/concern'
|
3
|
+
require 'active_support/core_ext/kernel/debugger' if RUBY_VERSION < '2.0.0'
|
4
|
+
require 'active_support/core_ext/kernel/reporting'
|
4
5
|
require 'active_support/core_ext/kernel/singleton_class'
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'active_support/core_ext/module/concerning'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
# A shortcut to define a toplevel concern, not within a module.
|
5
|
+
#
|
6
|
+
# See Module::Concerning for more.
|
7
|
+
def concern(topic, &module_definition)
|
8
|
+
Object.concern topic, &module_definition
|
9
|
+
end
|
10
|
+
end
|
@@ -3,7 +3,7 @@ module Kernel
|
|
3
3
|
# Starts a debugging session if the +debugger+ gem has been loaded (call rails server --debugger to do load it).
|
4
4
|
def debugger
|
5
5
|
message = "\n***** Debugger requested, but was not available (ensure the debugger gem is listed in Gemfile/installed as gem): Start server with --debugger to enable *****\n"
|
6
|
-
defined?(Rails) ? Rails.logger.info(message) : $stderr.puts(message)
|
6
|
+
defined?(Rails.logger) ? Rails.logger.info(message) : $stderr.puts(message)
|
7
7
|
end
|
8
8
|
alias breakpoint debugger unless respond_to?(:breakpoint)
|
9
9
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'rbconfig'
|
2
2
|
require 'tempfile'
|
3
|
+
require 'active_support/deprecation'
|
3
4
|
|
4
5
|
module Kernel
|
5
6
|
# Sets $VERBOSE to nil for the duration of the block and back to its original
|
@@ -31,9 +32,13 @@ module Kernel
|
|
31
32
|
|
32
33
|
# For compatibility
|
33
34
|
def silence_stderr #:nodoc:
|
35
|
+
ActiveSupport::Deprecation.warn(
|
36
|
+
"`#silence_stderr` is deprecated and will be removed in the next release."
|
37
|
+
) #not thread-safe
|
34
38
|
silence_stream(STDERR) { yield }
|
35
39
|
end
|
36
40
|
|
41
|
+
# Deprecated : this method is not thread safe
|
37
42
|
# Silences any stream for the duration of the block.
|
38
43
|
#
|
39
44
|
# silence_stream(STDOUT) do
|
@@ -41,6 +46,8 @@ module Kernel
|
|
41
46
|
# end
|
42
47
|
#
|
43
48
|
# puts 'But this will'
|
49
|
+
#
|
50
|
+
# This method is not thread-safe.
|
44
51
|
def silence_stream(stream)
|
45
52
|
old_stream = stream.dup
|
46
53
|
stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
|
@@ -80,6 +87,9 @@ module Kernel
|
|
80
87
|
# stream = capture(:stderr) { system('echo error 1>&2') }
|
81
88
|
# stream # => "error\n"
|
82
89
|
def capture(stream)
|
90
|
+
ActiveSupport::Deprecation.warn(
|
91
|
+
"`#capture(stream)` is deprecated and will be removed in the next release."
|
92
|
+
) #not thread-safe
|
83
93
|
stream = stream.to_s
|
84
94
|
captured_stream = Tempfile.new(stream)
|
85
95
|
stream_io = eval("$#{stream}")
|
@@ -100,7 +110,12 @@ module Kernel
|
|
100
110
|
# Silences both STDOUT and STDERR, even for subprocesses.
|
101
111
|
#
|
102
112
|
# quietly { system 'bundle install' }
|
113
|
+
#
|
114
|
+
# This method is not thread-safe.
|
103
115
|
def quietly
|
116
|
+
ActiveSupport::Deprecation.warn(
|
117
|
+
"`#quietly` is deprecated and will be removed in the next release."
|
118
|
+
) #not thread-safe
|
104
119
|
silence_stream(STDOUT) do
|
105
120
|
silence_stream(STDERR) do
|
106
121
|
yield
|
@@ -7,6 +7,7 @@ class LoadError
|
|
7
7
|
]
|
8
8
|
|
9
9
|
unless method_defined?(:path)
|
10
|
+
# Returns the path which was unable to be loaded.
|
10
11
|
def path
|
11
12
|
@path ||= begin
|
12
13
|
REGEXPS.find do |regex|
|
@@ -17,9 +18,11 @@ class LoadError
|
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
21
|
+
# Returns true if the given path name (except perhaps for the ".rb"
|
22
|
+
# extension) is the missing file which caused the exception to be raised.
|
20
23
|
def is_missing?(location)
|
21
24
|
location.sub(/\.rb$/, '') == path.sub(/\.rb$/, '')
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
25
|
-
MissingSourceFile = LoadError
|
28
|
+
MissingSourceFile = LoadError
|
@@ -2,13 +2,16 @@ require 'active_support/core_ext/module/aliasing'
|
|
2
2
|
|
3
3
|
module Marshal
|
4
4
|
class << self
|
5
|
-
def load_with_autoloading(source)
|
6
|
-
load_without_autoloading(source)
|
5
|
+
def load_with_autoloading(source, proc = nil)
|
6
|
+
load_without_autoloading(source, proc)
|
7
7
|
rescue ArgumentError, NameError => exc
|
8
|
-
if exc.message.match(%r|undefined class/module (
|
8
|
+
if exc.message.match(%r|undefined class/module (.+?)(::)?\z|)
|
9
9
|
# try loading the class/module
|
10
|
-
$1.constantize
|
11
|
-
|
10
|
+
loaded = $1.constantize
|
11
|
+
|
12
|
+
raise unless $1 == loaded.name
|
13
|
+
|
14
|
+
# if it is an IO we need to go back to read the object
|
12
15
|
source.rewind if source.respond_to?(:rewind)
|
13
16
|
retry
|
14
17
|
else
|
@@ -19,9 +19,9 @@ class Module
|
|
19
19
|
# alias_method :foo_without_feature?, :foo?
|
20
20
|
# alias_method :foo?, :foo_with_feature?
|
21
21
|
#
|
22
|
-
# so you can safely chain foo, foo?, and foo
|
22
|
+
# so you can safely chain foo, foo?, foo! and/or foo= with the same feature.
|
23
23
|
def alias_method_chain(target, feature)
|
24
|
-
# Strip out punctuation on predicates or
|
24
|
+
# Strip out punctuation on predicates, bang or writer methods since
|
25
25
|
# e.g. target?_without_feature is not a valid method name.
|
26
26
|
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
|
27
27
|
yield(aliased_target, punctuation) if block_given?
|
@@ -1,8 +1,24 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
1
3
|
class Module
|
2
4
|
# Error generated by +delegate+ when a method is called on +nil+ and +allow_nil+
|
3
5
|
# option is not used.
|
4
6
|
class DelegationError < NoMethodError; end
|
5
7
|
|
8
|
+
RUBY_RESERVED_WORDS = Set.new(
|
9
|
+
%w(alias and BEGIN begin break case class def defined? do else elsif END
|
10
|
+
end ensure false for if in module next nil not or redo rescue retry
|
11
|
+
return self super then true undef unless until when while yield)
|
12
|
+
).freeze
|
13
|
+
|
14
|
+
DELEGATION_RESERVED_KEYWORDS = Set.new(
|
15
|
+
%w(_ arg args block)
|
16
|
+
)
|
17
|
+
|
18
|
+
DELEGATION_RESERVED_METHOD_NAMES = Set.new(
|
19
|
+
RUBY_RESERVED_WORDS + DELEGATION_RESERVED_KEYWORDS
|
20
|
+
).freeze
|
21
|
+
|
6
22
|
# Provides a +delegate+ class method to easily expose contained objects'
|
7
23
|
# public methods as your own.
|
8
24
|
#
|
@@ -163,14 +179,14 @@ class Module
|
|
163
179
|
line = line.to_i
|
164
180
|
|
165
181
|
to = to.to_s
|
166
|
-
to =
|
182
|
+
to = "self.#{to}" if RUBY_RESERVED_WORDS.include?(to)
|
167
183
|
|
168
184
|
methods.each do |method|
|
169
185
|
# Attribute writer methods only accept one argument. Makes sure []=
|
170
186
|
# methods still accept two arguments.
|
171
187
|
definition = (method =~ /[^\]]=$/) ? 'arg' : '*args, &block'
|
172
188
|
|
173
|
-
# The following generated
|
189
|
+
# The following generated method calls the target exactly once, storing
|
174
190
|
# the returned value in a dummy variable.
|
175
191
|
#
|
176
192
|
# Reason is twofold: On one hand doing less calls is in general better.
|
@@ -179,27 +195,27 @@ class Module
|
|
179
195
|
# be doing one call.
|
180
196
|
if allow_nil
|
181
197
|
method_def = [
|
182
|
-
"def #{method_prefix}#{method}(#{definition})",
|
183
|
-
"_ = #{to}",
|
184
|
-
"if !_.nil? || nil.respond_to?(:#{method})",
|
185
|
-
" _.#{method}(#{definition})",
|
186
|
-
"end",
|
187
|
-
"end"
|
198
|
+
"def #{method_prefix}#{method}(#{definition})",
|
199
|
+
"_ = #{to}",
|
200
|
+
"if !_.nil? || nil.respond_to?(:#{method})",
|
201
|
+
" _.#{method}(#{definition})",
|
202
|
+
"end",
|
203
|
+
"end"
|
188
204
|
].join ';'
|
189
205
|
else
|
190
206
|
exception = %(raise DelegationError, "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
|
191
207
|
|
192
208
|
method_def = [
|
193
|
-
"def #{method_prefix}#{method}(#{definition})",
|
194
|
-
" _ = #{to}",
|
195
|
-
" _.#{method}(#{definition})",
|
196
|
-
"rescue NoMethodError => e",
|
197
|
-
" if _.nil? && e.name == :#{method}",
|
198
|
-
" #{exception}",
|
199
|
-
" else",
|
200
|
-
" raise",
|
201
|
-
" end",
|
202
|
-
"end"
|
209
|
+
"def #{method_prefix}#{method}(#{definition})",
|
210
|
+
" _ = #{to}",
|
211
|
+
" _.#{method}(#{definition})",
|
212
|
+
"rescue NoMethodError => e",
|
213
|
+
" if _.nil? && e.name == :#{method}",
|
214
|
+
" #{exception}",
|
215
|
+
" else",
|
216
|
+
" raise",
|
217
|
+
" end",
|
218
|
+
"end"
|
203
219
|
].join ';'
|
204
220
|
end
|
205
221
|
|
@@ -2,7 +2,9 @@ class Module
|
|
2
2
|
###
|
3
3
|
# TODO: remove this after 1.9 support is dropped
|
4
4
|
def methods_transplantable? # :nodoc:
|
5
|
-
x = Module.new {
|
5
|
+
x = Module.new {
|
6
|
+
def foo; end # :nodoc:
|
7
|
+
}
|
6
8
|
Module.new { define_method :bar, x.instance_method(:foo) }
|
7
9
|
true
|
8
10
|
rescue TypeError
|
@@ -41,7 +41,7 @@ class Numeric
|
|
41
41
|
# 1000.to_s(:percentage, delimiter: '.', separator: ',') # => 1.000,000%
|
42
42
|
# 302.24398923423.to_s(:percentage, precision: 5) # => 302.24399%
|
43
43
|
# 1000.to_s(:percentage, locale: :fr) # => 1 000,000%
|
44
|
-
# 100.to_s(:percentage, format: '%n %') # => 100 %
|
44
|
+
# 100.to_s(:percentage, format: '%n %') # => 100.000 %
|
45
45
|
#
|
46
46
|
# Delimited:
|
47
47
|
# 12345678.to_s(:delimited) # => 12,345,678
|
@@ -78,7 +78,7 @@ class Numeric
|
|
78
78
|
# 1234567.to_s(:human_size, precision: 2) # => 1.2 MB
|
79
79
|
# 483989.to_s(:human_size, precision: 2) # => 470 KB
|
80
80
|
# 1234567.to_s(:human_size, precision: 2, separator: ',') # => 1,2 MB
|
81
|
-
# 1234567890123.to_s(:human_size, precision: 5) # => "1.
|
81
|
+
# 1234567890123.to_s(:human_size, precision: 5) # => "1.1228 TB"
|
82
82
|
# 524288000.to_s(:human_size, precision: 5) # => "500 MB"
|
83
83
|
#
|
84
84
|
# Human-friendly format:
|
@@ -118,7 +118,15 @@ class Numeric
|
|
118
118
|
end
|
119
119
|
end
|
120
120
|
|
121
|
-
[Float,
|
121
|
+
klasses = [Float, BigDecimal]
|
122
|
+
# Ruby 2.4+ unifies Fixnum & Bignum into Integer.
|
123
|
+
if 0.class == Integer
|
124
|
+
klasses << Integer
|
125
|
+
else
|
126
|
+
klasses << Fixnum << Bignum
|
127
|
+
end
|
128
|
+
|
129
|
+
klasses.each do |klass|
|
122
130
|
klass.send(:alias_method, :to_default_s, :to_s)
|
123
131
|
|
124
132
|
klass.send(:define_method, :to_s) do |*args|
|
@@ -16,21 +16,6 @@ class Numeric
|
|
16
16
|
#
|
17
17
|
# # equivalent to Time.current.advance(months: 4, years: 5)
|
18
18
|
# (4.months + 5.years).from_now
|
19
|
-
#
|
20
|
-
# While these methods provide precise calculation when used as in the examples above, care
|
21
|
-
# should be taken to note that this is not true if the result of `months', `years', etc is
|
22
|
-
# converted before use:
|
23
|
-
#
|
24
|
-
# # equivalent to 30.days.to_i.from_now
|
25
|
-
# 1.month.to_i.from_now
|
26
|
-
#
|
27
|
-
# # equivalent to 365.25.days.to_f.from_now
|
28
|
-
# 1.year.to_f.from_now
|
29
|
-
#
|
30
|
-
# In such cases, Ruby's core
|
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
19
|
def seconds
|
35
20
|
ActiveSupport::Duration.new(self, [[:seconds, self]])
|
36
21
|
end
|
@@ -61,25 +46,7 @@ class Numeric
|
|
61
46
|
end
|
62
47
|
alias :fortnight :fortnights
|
63
48
|
|
64
|
-
#
|
65
|
-
def ago(time = ::Time.current)
|
66
|
-
ActiveSupport::Deprecation.warn "Calling #ago or #until on a number (e.g. 5.ago) is deprecated and will be removed in the future, use 5.seconds.ago instead"
|
67
|
-
time - self
|
68
|
-
end
|
69
|
-
|
70
|
-
# Reads best with argument: 10.minutes.until(time)
|
71
|
-
alias :until :ago
|
72
|
-
|
73
|
-
# Reads best with argument: 10.minutes.since(time)
|
74
|
-
def since(time = ::Time.current)
|
75
|
-
ActiveSupport::Deprecation.warn "Calling #since or #from_now on a number (e.g. 5.since) is deprecated and will be removed in the future, use 5.seconds.since instead"
|
76
|
-
time + self
|
77
|
-
end
|
78
|
-
|
79
|
-
# Reads best without arguments: 10.minutes.from_now
|
80
|
-
alias :from_now :since
|
81
|
-
|
82
|
-
# Used with the standard time durations, like 1.hour.in_milliseconds --
|
49
|
+
# Used with the standard time durations, like 1.hour.in_milliseconds --
|
83
50
|
# so we can feed them to JavaScript functions like getTime().
|
84
51
|
def in_milliseconds
|
85
52
|
self * 1000
|
@@ -2,6 +2,7 @@ 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
4
|
require 'active_support/core_ext/object/deep_dup'
|
5
|
+
require 'active_support/core_ext/object/itself'
|
5
6
|
require 'active_support/core_ext/object/try'
|
6
7
|
require 'active_support/core_ext/object/inclusion'
|
7
8
|
|