activesupport 4.2.11.3 → 5.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 +5 -5
- data/CHANGELOG.md +309 -485
- data/MIT-LICENSE +1 -1
- data/README.rdoc +2 -3
- data/lib/active_support.rb +8 -15
- data/lib/active_support/array_inquirer.rb +44 -0
- data/lib/active_support/backtrace_cleaner.rb +1 -1
- data/lib/active_support/cache.rb +59 -72
- data/lib/active_support/cache/file_store.rb +27 -19
- data/lib/active_support/cache/mem_cache_store.rb +71 -60
- data/lib/active_support/cache/memory_store.rb +16 -21
- data/lib/active_support/cache/null_store.rb +1 -4
- data/lib/active_support/cache/strategy/local_cache.rb +31 -20
- data/lib/active_support/callbacks.rb +107 -111
- data/lib/active_support/concern.rb +1 -1
- data/lib/active_support/concurrency/latch.rb +7 -15
- data/lib/active_support/concurrency/share_lock.rb +142 -0
- data/lib/active_support/configurable.rb +1 -0
- data/lib/active_support/core_ext.rb +2 -1
- data/lib/active_support/core_ext/array.rb +1 -0
- data/lib/active_support/core_ext/array/access.rb +13 -1
- data/lib/active_support/core_ext/array/conversions.rb +6 -4
- data/lib/active_support/core_ext/array/inquiry.rb +17 -0
- data/lib/active_support/core_ext/array/wrap.rb +5 -4
- data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -10
- data/lib/active_support/core_ext/class.rb +0 -1
- data/lib/active_support/core_ext/class/attribute.rb +10 -9
- data/lib/active_support/core_ext/class/subclasses.rb +5 -2
- data/lib/active_support/core_ext/date.rb +1 -1
- data/lib/active_support/core_ext/date/blank.rb +12 -0
- data/lib/active_support/core_ext/date/calculations.rb +1 -1
- data/lib/active_support/core_ext/date/conversions.rb +3 -3
- data/lib/active_support/core_ext/date_and_time/calculations.rb +93 -27
- data/lib/active_support/core_ext/date_and_time/zones.rb +1 -2
- data/lib/active_support/core_ext/date_time.rb +1 -1
- data/lib/active_support/core_ext/date_time/blank.rb +12 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +7 -23
- data/lib/active_support/core_ext/date_time/conversions.rb +2 -0
- data/lib/active_support/core_ext/enumerable.rb +27 -17
- data/lib/active_support/core_ext/file/atomic.rb +30 -25
- data/lib/active_support/core_ext/hash/compact.rb +15 -19
- data/lib/active_support/core_ext/hash/conversions.rb +21 -2
- data/lib/active_support/core_ext/hash/deep_merge.rb +1 -1
- data/lib/active_support/core_ext/hash/except.rb +9 -8
- data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -1
- data/lib/active_support/core_ext/hash/keys.rb +22 -18
- data/lib/active_support/core_ext/hash/slice.rb +1 -1
- data/lib/active_support/core_ext/hash/transform_values.rb +13 -7
- data/lib/active_support/core_ext/integer/time.rb +1 -1
- data/lib/active_support/core_ext/kernel.rb +0 -1
- data/lib/active_support/core_ext/kernel/debugger.rb +3 -10
- data/lib/active_support/core_ext/kernel/reporting.rb +0 -84
- data/lib/active_support/core_ext/load_error.rb +4 -2
- data/lib/active_support/core_ext/marshal.rb +8 -13
- data/lib/active_support/core_ext/module.rb +1 -0
- data/lib/active_support/core_ext/module/aliasing.rb +6 -1
- data/lib/active_support/core_ext/module/anonymous.rb +10 -1
- data/lib/active_support/core_ext/module/attr_internal.rb +2 -5
- data/lib/active_support/core_ext/module/attribute_accessors.rb +7 -7
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
- data/lib/active_support/core_ext/module/concerning.rb +4 -4
- data/lib/active_support/core_ext/module/delegation.rb +7 -14
- data/lib/active_support/core_ext/module/method_transplanting.rb +3 -13
- data/lib/active_support/core_ext/module/qualified_const.rb +30 -12
- data/lib/active_support/core_ext/module/remove_method.rb +23 -0
- data/lib/active_support/core_ext/name_error.rb +15 -2
- data/lib/active_support/core_ext/numeric.rb +1 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +20 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +12 -23
- data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
- data/lib/active_support/core_ext/numeric/time.rb +20 -0
- data/lib/active_support/core_ext/object.rb +0 -1
- data/lib/active_support/core_ext/object/blank.rb +11 -2
- data/lib/active_support/core_ext/object/deep_dup.rb +10 -3
- data/lib/active_support/core_ext/object/duplicable.rb +39 -70
- data/lib/active_support/core_ext/object/inclusion.rb +2 -2
- data/lib/active_support/core_ext/object/instance_variables.rb +1 -1
- data/lib/active_support/core_ext/object/json.rb +9 -7
- data/lib/active_support/core_ext/object/to_query.rb +1 -1
- data/lib/active_support/core_ext/object/try.rb +67 -21
- data/lib/active_support/core_ext/object/with_options.rb +1 -1
- data/lib/active_support/core_ext/range/conversions.rb +18 -6
- data/lib/active_support/core_ext/range/each.rb +16 -18
- data/lib/active_support/core_ext/range/include_range.rb +20 -20
- data/lib/active_support/core_ext/securerandom.rb +23 -0
- data/lib/active_support/core_ext/string/access.rb +1 -1
- data/lib/active_support/core_ext/string/behavior.rb +1 -1
- data/lib/active_support/core_ext/string/conversions.rb +2 -2
- data/lib/active_support/core_ext/string/filters.rb +1 -2
- data/lib/active_support/core_ext/string/inflections.rb +23 -5
- data/lib/active_support/core_ext/string/multibyte.rb +11 -7
- data/lib/active_support/core_ext/string/output_safety.rb +8 -9
- data/lib/active_support/core_ext/string/strip.rb +3 -6
- data/lib/active_support/core_ext/struct.rb +3 -6
- data/lib/active_support/core_ext/time.rb +0 -2
- data/lib/active_support/core_ext/time/calculations.rb +18 -16
- data/lib/active_support/core_ext/time/conversions.rb +4 -2
- data/lib/active_support/core_ext/time/marshal.rb +2 -29
- data/lib/active_support/core_ext/time/zones.rb +19 -3
- data/lib/active_support/core_ext/uri.rb +1 -3
- data/lib/active_support/dependencies.rb +79 -44
- data/lib/active_support/dependencies/interlock.rb +47 -0
- data/lib/active_support/deprecation/behaviors.rb +12 -0
- data/lib/active_support/deprecation/method_wrappers.rb +42 -16
- data/lib/active_support/deprecation/proxy_wrappers.rb +47 -24
- data/lib/active_support/deprecation/reporting.rb +13 -2
- data/lib/active_support/duration.rb +5 -8
- data/lib/active_support/evented_file_update_checker.rb +150 -0
- data/lib/active_support/file_update_checker.rb +1 -1
- data/lib/active_support/gem_version.rb +5 -5
- data/lib/active_support/hash_with_indifferent_access.rb +15 -17
- data/lib/active_support/i18n_railtie.rb +25 -4
- data/lib/active_support/inflector/inflections.rb +36 -5
- data/lib/active_support/inflector/methods.rb +87 -89
- data/lib/active_support/inflector/transliterate.rb +36 -21
- data/lib/active_support/json/decoding.rb +2 -8
- data/lib/active_support/json/encoding.rb +0 -50
- data/lib/active_support/key_generator.rb +4 -4
- data/lib/active_support/log_subscriber.rb +1 -1
- data/lib/active_support/log_subscriber/test_helper.rb +3 -3
- data/lib/active_support/logger.rb +4 -52
- data/lib/active_support/logger_silence.rb +3 -5
- data/lib/active_support/message_encryptor.rb +4 -11
- data/lib/active_support/message_verifier.rb +64 -8
- data/lib/active_support/multibyte/chars.rb +12 -3
- data/lib/active_support/multibyte/unicode.rb +6 -8
- data/lib/active_support/notifications.rb +2 -2
- data/lib/active_support/notifications/fanout.rb +5 -5
- data/lib/active_support/notifications/instrumenter.rb +19 -2
- data/lib/active_support/number_helper.rb +21 -15
- data/lib/active_support/number_helper/number_to_currency_converter.rb +4 -4
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +7 -2
- data/lib/active_support/number_helper/number_to_human_converter.rb +6 -4
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +5 -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 +28 -25
- data/lib/active_support/ordered_options.rb +15 -1
- data/lib/active_support/per_thread_registry.rb +3 -0
- data/lib/active_support/rails.rb +2 -2
- data/lib/active_support/railtie.rb +6 -1
- data/lib/active_support/rescuable.rb +4 -4
- data/lib/active_support/security_utils.rb +0 -7
- data/lib/active_support/string_inquirer.rb +1 -1
- data/lib/active_support/subscriber.rb +5 -10
- data/lib/active_support/tagged_logging.rb +3 -1
- data/lib/active_support/test_case.rb +13 -25
- data/lib/active_support/testing/assertions.rb +15 -13
- data/lib/active_support/testing/autorun.rb +8 -1
- data/lib/active_support/testing/composite_filter.rb +54 -0
- data/lib/active_support/testing/deprecation.rb +9 -8
- data/lib/active_support/testing/file_fixtures.rb +34 -0
- data/lib/active_support/testing/isolation.rb +22 -8
- data/lib/active_support/testing/method_call_assertions.rb +41 -0
- data/lib/active_support/testing/stream.rb +42 -0
- data/lib/active_support/testing/time_helpers.rb +6 -6
- data/lib/active_support/time_with_zone.rb +135 -53
- data/lib/active_support/values/time_zone.rb +80 -46
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/xml_mini.rb +15 -30
- data/lib/active_support/xml_mini/jdom.rb +1 -1
- data/lib/active_support/xml_mini/libxml.rb +5 -3
- data/lib/active_support/xml_mini/libxmlsax.rb +4 -1
- data/lib/active_support/xml_mini/nokogiri.rb +5 -3
- data/lib/active_support/xml_mini/nokogirisax.rb +3 -1
- data/lib/active_support/xml_mini/rexml.rb +3 -1
- metadata +57 -21
- data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -16
- data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +0 -15
- data/lib/active_support/core_ext/date_time/compatibility.rb +0 -16
- data/lib/active_support/core_ext/object/itself.rb +0 -15
- data/lib/active_support/core_ext/thread.rb +0 -86
- data/lib/active_support/core_ext/time/compatibility.rb +0 -14
- data/lib/active_support/logger_thread_safe_level.rb +0 -32
@@ -7,36 +7,56 @@ class Numeric
|
|
7
7
|
EXABYTE = PETABYTE * 1024
|
8
8
|
|
9
9
|
# Enables the use of byte calculations and declarations, like 45.bytes + 2.6.megabytes
|
10
|
+
#
|
11
|
+
# 2.bytes # => 2
|
10
12
|
def bytes
|
11
13
|
self
|
12
14
|
end
|
13
15
|
alias :byte :bytes
|
14
16
|
|
17
|
+
# Returns the number of bytes equivalent to the kilobytes provided.
|
18
|
+
#
|
19
|
+
# 2.kilobytes # => 2048
|
15
20
|
def kilobytes
|
16
21
|
self * KILOBYTE
|
17
22
|
end
|
18
23
|
alias :kilobyte :kilobytes
|
19
24
|
|
25
|
+
# Returns the number of bytes equivalent to the megabytes provided.
|
26
|
+
#
|
27
|
+
# 2.megabytes # => 2_097_152
|
20
28
|
def megabytes
|
21
29
|
self * MEGABYTE
|
22
30
|
end
|
23
31
|
alias :megabyte :megabytes
|
24
32
|
|
33
|
+
# Returns the number of bytes equivalent to the gigabytes provided.
|
34
|
+
#
|
35
|
+
# 2.gigabytes # => 2_147_483_648
|
25
36
|
def gigabytes
|
26
37
|
self * GIGABYTE
|
27
38
|
end
|
28
39
|
alias :gigabyte :gigabytes
|
29
40
|
|
41
|
+
# Returns the number of bytes equivalent to the terabytes provided.
|
42
|
+
#
|
43
|
+
# 2.terabytes # => 2_199_023_255_552
|
30
44
|
def terabytes
|
31
45
|
self * TERABYTE
|
32
46
|
end
|
33
47
|
alias :terabyte :terabytes
|
34
48
|
|
49
|
+
# Returns the number of bytes equivalent to the petabytes provided.
|
50
|
+
#
|
51
|
+
# 2.petabytes # => 2_251_799_813_685_248
|
35
52
|
def petabytes
|
36
53
|
self * PETABYTE
|
37
54
|
end
|
38
55
|
alias :petabyte :petabytes
|
39
56
|
|
57
|
+
# Returns the number of bytes equivalent to the exabytes provided.
|
58
|
+
#
|
59
|
+
# 2.exabytes # => 2_305_843_009_213_693_952
|
40
60
|
def exabytes
|
41
61
|
self * EXABYTE
|
42
62
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'active_support/core_ext/big_decimal/conversions'
|
2
2
|
require 'active_support/number_helper'
|
3
3
|
|
4
|
-
|
4
|
+
module ActiveSupport::NumericWithFormat
|
5
5
|
|
6
6
|
# Provides options for converting numbers into formatted strings.
|
7
7
|
# Options are provided for phone numbers, currency, percentage,
|
@@ -97,7 +97,10 @@ class Numeric
|
|
97
97
|
# 1234567.to_s(:human, precision: 1,
|
98
98
|
# separator: ',',
|
99
99
|
# significant: false) # => "1,2 Million"
|
100
|
-
def
|
100
|
+
def to_s(*args)
|
101
|
+
format, options = args
|
102
|
+
options ||= {}
|
103
|
+
|
101
104
|
case format
|
102
105
|
when :phone
|
103
106
|
return ActiveSupport::NumberHelper.number_to_phone(self, options)
|
@@ -114,30 +117,16 @@ class Numeric
|
|
114
117
|
when :human_size
|
115
118
|
return ActiveSupport::NumberHelper.number_to_human_size(self, options)
|
116
119
|
else
|
117
|
-
|
120
|
+
super
|
118
121
|
end
|
119
122
|
end
|
120
123
|
|
121
|
-
|
122
|
-
|
123
|
-
if 0.class == Integer
|
124
|
-
klasses << Integer
|
125
|
-
else
|
126
|
-
klasses << Fixnum << Bignum
|
124
|
+
def to_formatted_s(*args)
|
125
|
+
to_s(*args)
|
127
126
|
end
|
127
|
+
deprecate to_formatted_s: :to_s
|
128
|
+
end
|
128
129
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
klass.send(:define_method, :to_s) do |*args|
|
133
|
-
if args[0].is_a?(Symbol)
|
134
|
-
format = args[0]
|
135
|
-
options = args[1] || {}
|
136
|
-
|
137
|
-
self.to_formatted_s(format, options)
|
138
|
-
else
|
139
|
-
to_default_s(*args)
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
130
|
+
[Fixnum, Bignum, Float, BigDecimal].each do |klass|
|
131
|
+
klass.prepend(ActiveSupport::NumericWithFormat)
|
143
132
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
unless 1.respond_to?(:positive?) # TODO: Remove this file when we drop support to ruby < 2.3
|
2
|
+
class Numeric
|
3
|
+
# Returns true if the number is positive.
|
4
|
+
#
|
5
|
+
# 1.positive? # => true
|
6
|
+
# 0.positive? # => false
|
7
|
+
# -1.positive? # => false
|
8
|
+
def positive?
|
9
|
+
self > 0
|
10
|
+
end
|
11
|
+
|
12
|
+
# Returns true if the number is negative.
|
13
|
+
#
|
14
|
+
# -1.negative? # => true
|
15
|
+
# 0.negative? # => false
|
16
|
+
# 1.negative? # => false
|
17
|
+
def negative?
|
18
|
+
self < 0
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Complex
|
23
|
+
undef :positive?
|
24
|
+
undef :negative?
|
25
|
+
end
|
26
|
+
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'active_support/duration'
|
2
2
|
require 'active_support/core_ext/time/calculations'
|
3
3
|
require 'active_support/core_ext/time/acts_like'
|
4
|
+
require 'active_support/core_ext/date/calculations'
|
5
|
+
require 'active_support/core_ext/date/acts_like'
|
4
6
|
|
5
7
|
class Numeric
|
6
8
|
# Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
|
@@ -21,33 +23,51 @@ class Numeric
|
|
21
23
|
end
|
22
24
|
alias :second :seconds
|
23
25
|
|
26
|
+
# Returns a Duration instance matching the number of minutes provided.
|
27
|
+
#
|
28
|
+
# 2.minutes # => 120 seconds
|
24
29
|
def minutes
|
25
30
|
ActiveSupport::Duration.new(self * 60, [[:seconds, self * 60]])
|
26
31
|
end
|
27
32
|
alias :minute :minutes
|
28
33
|
|
34
|
+
# Returns a Duration instance matching the number of hours provided.
|
35
|
+
#
|
36
|
+
# 2.hours # => 7_200 seconds
|
29
37
|
def hours
|
30
38
|
ActiveSupport::Duration.new(self * 3600, [[:seconds, self * 3600]])
|
31
39
|
end
|
32
40
|
alias :hour :hours
|
33
41
|
|
42
|
+
# Returns a Duration instance matching the number of days provided.
|
43
|
+
#
|
44
|
+
# 2.days # => 2 days
|
34
45
|
def days
|
35
46
|
ActiveSupport::Duration.new(self * 24.hours, [[:days, self]])
|
36
47
|
end
|
37
48
|
alias :day :days
|
38
49
|
|
50
|
+
# Returns a Duration instance matching the number of weeks provided.
|
51
|
+
#
|
52
|
+
# 2.weeks # => 14 days
|
39
53
|
def weeks
|
40
54
|
ActiveSupport::Duration.new(self * 7.days, [[:days, self * 7]])
|
41
55
|
end
|
42
56
|
alias :week :weeks
|
43
57
|
|
58
|
+
# Returns a Duration instance matching the number of fortnights provided.
|
59
|
+
#
|
60
|
+
# 2.fortnights # => 28 days
|
44
61
|
def fortnights
|
45
62
|
ActiveSupport::Duration.new(self * 2.weeks, [[:days, self * 14]])
|
46
63
|
end
|
47
64
|
alias :fortnight :fortnights
|
48
65
|
|
66
|
+
# Returns the number of milliseconds equivalent to the seconds provided.
|
49
67
|
# Used with the standard time durations, like 1.hour.in_milliseconds --
|
50
68
|
# so we can feed them to JavaScript functions like getTime().
|
69
|
+
#
|
70
|
+
# 2.in_milliseconds # => 2_000
|
51
71
|
def in_milliseconds
|
52
72
|
self * 1000
|
53
73
|
end
|
@@ -2,7 +2,6 @@ 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'
|
6
5
|
require 'active_support/core_ext/object/try'
|
7
6
|
require 'active_support/core_ext/object/inclusion'
|
8
7
|
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
class Object
|
4
2
|
# An object is blank if it's false, empty, or a whitespace string.
|
5
3
|
# For example, +false+, '', ' ', +nil+, [], and {} are all blank.
|
@@ -129,3 +127,14 @@ class Numeric #:nodoc:
|
|
129
127
|
false
|
130
128
|
end
|
131
129
|
end
|
130
|
+
|
131
|
+
class Time #:nodoc:
|
132
|
+
# No Time is blank:
|
133
|
+
#
|
134
|
+
# Time.now.blank? # => false
|
135
|
+
#
|
136
|
+
# @return [false]
|
137
|
+
def blank?
|
138
|
+
false
|
139
|
+
end
|
140
|
+
end
|
@@ -25,7 +25,7 @@ class Array
|
|
25
25
|
# array[1][2] # => nil
|
26
26
|
# dup[1][2] # => 4
|
27
27
|
def deep_dup
|
28
|
-
map
|
28
|
+
map(&:deep_dup)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -39,8 +39,15 @@ class Hash
|
|
39
39
|
# hash[:a][:c] # => nil
|
40
40
|
# dup[:a][:c] # => "c"
|
41
41
|
def deep_dup
|
42
|
-
|
43
|
-
|
42
|
+
hash = dup
|
43
|
+
each_pair do |key, value|
|
44
|
+
if key.frozen? && ::String === key
|
45
|
+
hash[key] = value.deep_dup
|
46
|
+
else
|
47
|
+
hash.delete(key)
|
48
|
+
hash[key.deep_dup] = value.deep_dup
|
49
|
+
end
|
44
50
|
end
|
51
|
+
hash
|
45
52
|
end
|
46
53
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
#--
|
2
|
-
# Most objects are cloneable, but not all. For example you can't dup
|
2
|
+
# Most objects are cloneable, but not all. For example you can't dup +nil+:
|
3
3
|
#
|
4
|
-
#
|
4
|
+
# nil.dup # => TypeError: can't dup NilClass
|
5
5
|
#
|
6
6
|
# Classes may signal their instances are not duplicable removing +dup+/+clone+
|
7
7
|
# or raising exceptions from them. So, to dup an arbitrary object you normally
|
@@ -19,7 +19,7 @@
|
|
19
19
|
class Object
|
20
20
|
# Can you safely dup this object?
|
21
21
|
#
|
22
|
-
# False for +nil+, +false+, +true+, symbol, number
|
22
|
+
# False for +nil+, +false+, +true+, symbol, number, method objects;
|
23
23
|
# true otherwise.
|
24
24
|
def duplicable?
|
25
25
|
true
|
@@ -27,94 +27,63 @@ class Object
|
|
27
27
|
end
|
28
28
|
|
29
29
|
class NilClass
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
# nil.duplicable? # => false
|
37
|
-
# nil.dup # => TypeError: can't dup NilClass
|
38
|
-
def duplicable?
|
39
|
-
false
|
40
|
-
end
|
30
|
+
# +nil+ is not duplicable:
|
31
|
+
#
|
32
|
+
# nil.duplicable? # => false
|
33
|
+
# nil.dup # => TypeError: can't dup NilClass
|
34
|
+
def duplicable?
|
35
|
+
false
|
41
36
|
end
|
42
37
|
end
|
43
38
|
|
44
39
|
class FalseClass
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
# false.duplicable? # => false
|
52
|
-
# false.dup # => TypeError: can't dup FalseClass
|
53
|
-
def duplicable?
|
54
|
-
false
|
55
|
-
end
|
40
|
+
# +false+ is not duplicable:
|
41
|
+
#
|
42
|
+
# false.duplicable? # => false
|
43
|
+
# false.dup # => TypeError: can't dup FalseClass
|
44
|
+
def duplicable?
|
45
|
+
false
|
56
46
|
end
|
57
47
|
end
|
58
48
|
|
59
49
|
class TrueClass
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
# true.duplicable? # => false
|
67
|
-
# true.dup # => TypeError: can't dup TrueClass
|
68
|
-
def duplicable?
|
69
|
-
false
|
70
|
-
end
|
50
|
+
# +true+ is not duplicable:
|
51
|
+
#
|
52
|
+
# true.duplicable? # => false
|
53
|
+
# true.dup # => TypeError: can't dup TrueClass
|
54
|
+
def duplicable?
|
55
|
+
false
|
71
56
|
end
|
72
57
|
end
|
73
58
|
|
74
59
|
class Symbol
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
#
|
82
|
-
# :my_symbol.duplicable? # => false
|
83
|
-
# :my_symbol.dup # => TypeError: can't dup Symbol
|
84
|
-
def duplicable?
|
85
|
-
false
|
86
|
-
end
|
60
|
+
# Symbols are not duplicable:
|
61
|
+
#
|
62
|
+
# :my_symbol.duplicable? # => false
|
63
|
+
# :my_symbol.dup # => TypeError: can't dup Symbol
|
64
|
+
def duplicable?
|
65
|
+
false
|
87
66
|
end
|
88
67
|
end
|
89
68
|
|
90
69
|
class Numeric
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
# 3.duplicable? # => false
|
98
|
-
# 3.dup # => TypeError: can't dup Integer
|
99
|
-
def duplicable?
|
100
|
-
false
|
101
|
-
end
|
70
|
+
# Numbers are not duplicable:
|
71
|
+
#
|
72
|
+
# 3.duplicable? # => false
|
73
|
+
# 3.dup # => TypeError: can't dup Fixnum
|
74
|
+
def duplicable?
|
75
|
+
false
|
102
76
|
end
|
103
77
|
end
|
104
78
|
|
105
79
|
require 'bigdecimal'
|
106
80
|
class BigDecimal
|
107
|
-
#
|
108
|
-
#
|
109
|
-
#
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
def duplicable?
|
114
|
-
true
|
115
|
-
end
|
116
|
-
rescue TypeError
|
117
|
-
# can't dup, so use superclass implementation
|
81
|
+
# BigDecimals are duplicable:
|
82
|
+
#
|
83
|
+
# BigDecimal.new("1.2").duplicable? # => true
|
84
|
+
# BigDecimal.new("1.2").dup # => #<BigDecimal:...,'0.12E1',18(18)>
|
85
|
+
def duplicable?
|
86
|
+
true
|
118
87
|
end
|
119
88
|
end
|
120
89
|
|
@@ -5,7 +5,7 @@ class Object
|
|
5
5
|
# characters = ["Konata", "Kagami", "Tsukasa"]
|
6
6
|
# "Konata".in?(characters) # => true
|
7
7
|
#
|
8
|
-
# This will throw an ArgumentError if the argument doesn't respond
|
8
|
+
# This will throw an +ArgumentError+ if the argument doesn't respond
|
9
9
|
# to +#include?+.
|
10
10
|
def in?(another_object)
|
11
11
|
another_object.include?(self)
|
@@ -18,7 +18,7 @@ class Object
|
|
18
18
|
#
|
19
19
|
# params[:bucket_type].presence_in %w( project calendar )
|
20
20
|
#
|
21
|
-
# This will throw an ArgumentError if the argument doesn't respond to +#include?+.
|
21
|
+
# This will throw an +ArgumentError+ if the argument doesn't respond to +#include?+.
|
22
22
|
#
|
23
23
|
# @return [Object]
|
24
24
|
def presence_in(another_object)
|
@@ -9,7 +9,6 @@ require 'time'
|
|
9
9
|
require 'active_support/core_ext/time/conversions'
|
10
10
|
require 'active_support/core_ext/date_time/conversions'
|
11
11
|
require 'active_support/core_ext/date/conversions'
|
12
|
-
require 'active_support/core_ext/module/aliasing'
|
13
12
|
|
14
13
|
# The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
|
15
14
|
# their default behavior. That said, we need to define the basic to_json method in all of them,
|
@@ -26,22 +25,25 @@ require 'active_support/core_ext/module/aliasing'
|
|
26
25
|
# bypassed completely. This means that as_json won't be invoked and the JSON gem will simply
|
27
26
|
# ignore any options it does not natively understand. This also means that ::JSON.{generate,dump}
|
28
27
|
# should give exactly the same results with or without active support.
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
|
29
|
+
module ActiveSupport
|
30
|
+
module ToJsonWithActiveSupportEncoder # :nodoc:
|
31
|
+
def to_json(options = nil)
|
32
32
|
if options.is_a?(::JSON::State)
|
33
33
|
# Called from JSON.{generate,dump}, forward it to JSON gem's to_json
|
34
|
-
|
34
|
+
super(options)
|
35
35
|
else
|
36
36
|
# to_json is being invoked directly, use ActiveSupport's encoder
|
37
37
|
ActiveSupport::JSON.encode(self, options)
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
41
|
-
alias_method_chain :to_json, :active_support_encoder
|
42
40
|
end
|
43
41
|
end
|
44
42
|
|
43
|
+
[Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass, Enumerable].reverse_each do |klass|
|
44
|
+
klass.prepend(ActiveSupport::ToJsonWithActiveSupportEncoder)
|
45
|
+
end
|
46
|
+
|
45
47
|
class Object
|
46
48
|
def as_json(options = nil) #:nodoc:
|
47
49
|
if respond_to?(:to_hash)
|