activesupport 5.0.7.2 → 5.1.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 +215 -820
- data/MIT-LICENSE +1 -1
- data/README.rdoc +1 -1
- data/lib/active_support.rb +8 -4
- data/lib/active_support/all.rb +3 -3
- data/lib/active_support/array_inquirer.rb +7 -5
- data/lib/active_support/backtrace_cleaner.rb +4 -4
- data/lib/active_support/benchmarkable.rb +3 -3
- data/lib/active_support/builder.rb +1 -1
- data/lib/active_support/cache.rb +42 -49
- data/lib/active_support/cache/file_store.rb +12 -21
- data/lib/active_support/cache/mem_cache_store.rb +30 -40
- data/lib/active_support/cache/memory_store.rb +11 -13
- data/lib/active_support/cache/null_store.rb +4 -4
- data/lib/active_support/cache/strategy/local_cache.rb +16 -25
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +8 -9
- data/lib/active_support/callbacks.rb +647 -584
- data/lib/active_support/concurrency/share_lock.rb +20 -21
- data/lib/active_support/configurable.rb +5 -5
- data/lib/active_support/core_ext.rb +1 -2
- data/lib/active_support/core_ext/array.rb +7 -7
- data/lib/active_support/core_ext/array/access.rb +1 -1
- data/lib/active_support/core_ext/array/conversions.rb +15 -15
- data/lib/active_support/core_ext/array/grouping.rb +1 -1
- data/lib/active_support/core_ext/array/inquiry.rb +1 -1
- data/lib/active_support/core_ext/array/prepend_and_append.rb +1 -1
- data/lib/active_support/core_ext/benchmark.rb +1 -1
- data/lib/active_support/core_ext/big_decimal.rb +1 -1
- data/lib/active_support/core_ext/big_decimal/conversions.rb +4 -6
- data/lib/active_support/core_ext/class.rb +2 -2
- data/lib/active_support/core_ext/class/attribute.rb +5 -5
- data/lib/active_support/core_ext/class/attribute_accessors.rb +1 -1
- data/lib/active_support/core_ext/class/subclasses.rb +18 -4
- data/lib/active_support/core_ext/date.rb +5 -5
- data/lib/active_support/core_ext/date/acts_like.rb +1 -1
- data/lib/active_support/core_ext/date/blank.rb +1 -1
- data/lib/active_support/core_ext/date/calculations.rb +8 -8
- data/lib/active_support/core_ext/date/conversions.rb +12 -12
- data/lib/active_support/core_ext/date/zones.rb +2 -2
- data/lib/active_support/core_ext/date_and_time/calculations.rb +27 -22
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +9 -1
- data/lib/active_support/core_ext/date_and_time/zones.rb +7 -8
- data/lib/active_support/core_ext/date_time.rb +5 -5
- data/lib/active_support/core_ext/date_time/acts_like.rb +2 -2
- data/lib/active_support/core_ext/date_time/blank.rb +1 -1
- data/lib/active_support/core_ext/date_time/calculations.rb +9 -9
- data/lib/active_support/core_ext/date_time/compatibility.rb +2 -13
- data/lib/active_support/core_ext/date_time/conversions.rb +12 -12
- data/lib/active_support/core_ext/digest/uuid.rb +4 -4
- data/lib/active_support/core_ext/enumerable.rb +46 -57
- data/lib/active_support/core_ext/file.rb +1 -1
- data/lib/active_support/core_ext/file/atomic.rb +4 -4
- data/lib/active_support/core_ext/hash.rb +9 -9
- data/lib/active_support/core_ext/hash/compact.rb +12 -9
- data/lib/active_support/core_ext/hash/conversions.rb +36 -37
- data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -2
- data/lib/active_support/core_ext/hash/keys.rb +8 -8
- data/lib/active_support/core_ext/hash/reverse_merge.rb +1 -1
- data/lib/active_support/core_ext/hash/slice.rb +4 -4
- data/lib/active_support/core_ext/hash/transform_values.rb +1 -0
- data/lib/active_support/core_ext/integer.rb +3 -3
- data/lib/active_support/core_ext/integer/inflections.rb +1 -1
- data/lib/active_support/core_ext/integer/time.rb +2 -2
- data/lib/active_support/core_ext/kernel.rb +4 -4
- data/lib/active_support/core_ext/kernel/concern.rb +1 -1
- data/lib/active_support/core_ext/kernel/reporting.rb +1 -1
- data/lib/active_support/core_ext/load_error.rb +1 -18
- data/lib/active_support/core_ext/marshal.rb +2 -2
- data/lib/active_support/core_ext/module.rb +11 -12
- data/lib/active_support/core_ext/module/aliasing.rb +3 -48
- data/lib/active_support/core_ext/module/attr_internal.rb +4 -4
- data/lib/active_support/core_ext/module/attribute_accessors.rb +11 -5
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +20 -13
- data/lib/active_support/core_ext/module/concerning.rb +1 -1
- data/lib/active_support/core_ext/module/delegation.rb +82 -16
- data/lib/active_support/core_ext/module/introspection.rb +3 -11
- data/lib/active_support/core_ext/module/reachable.rb +2 -2
- data/lib/active_support/core_ext/numeric.rb +4 -4
- data/lib/active_support/core_ext/numeric/conversions.rb +3 -9
- data/lib/active_support/core_ext/numeric/inquiry.rb +21 -21
- data/lib/active_support/core_ext/numeric/time.rb +5 -5
- data/lib/active_support/core_ext/object.rb +12 -12
- data/lib/active_support/core_ext/object/blank.rb +3 -1
- data/lib/active_support/core_ext/object/conversions.rb +4 -4
- data/lib/active_support/core_ext/object/deep_dup.rb +1 -1
- data/lib/active_support/core_ext/object/duplicable.rb +24 -4
- data/lib/active_support/core_ext/object/inclusion.rb +1 -1
- data/lib/active_support/core_ext/object/json.rb +26 -12
- data/lib/active_support/core_ext/object/to_param.rb +1 -1
- data/lib/active_support/core_ext/object/to_query.rb +4 -4
- data/lib/active_support/core_ext/object/try.rb +1 -1
- data/lib/active_support/core_ext/object/with_options.rb +1 -1
- data/lib/active_support/core_ext/range.rb +4 -4
- data/lib/active_support/core_ext/range/conversions.rb +1 -1
- data/lib/active_support/core_ext/regexp.rb +4 -0
- data/lib/active_support/core_ext/securerandom.rb +3 -3
- data/lib/active_support/core_ext/string.rb +13 -13
- data/lib/active_support/core_ext/string/access.rb +6 -6
- data/lib/active_support/core_ext/string/conversions.rb +2 -2
- data/lib/active_support/core_ext/string/filters.rb +3 -3
- data/lib/active_support/core_ext/string/indent.rb +4 -4
- data/lib/active_support/core_ext/string/inflections.rb +10 -14
- data/lib/active_support/core_ext/string/inquiry.rb +1 -1
- data/lib/active_support/core_ext/string/multibyte.rb +1 -1
- data/lib/active_support/core_ext/string/output_safety.rb +19 -20
- data/lib/active_support/core_ext/string/strip.rb +1 -1
- data/lib/active_support/core_ext/string/zones.rb +2 -2
- data/lib/active_support/core_ext/time.rb +5 -5
- data/lib/active_support/core_ext/time/acts_like.rb +1 -1
- data/lib/active_support/core_ext/time/calculations.rb +23 -29
- data/lib/active_support/core_ext/time/compatibility.rb +1 -10
- data/lib/active_support/core_ext/time/conversions.rb +12 -12
- data/lib/active_support/core_ext/time/zones.rb +3 -3
- data/lib/active_support/core_ext/uri.rb +2 -2
- data/lib/active_support/dependencies.rb +45 -47
- data/lib/active_support/dependencies/interlock.rb +1 -1
- data/lib/active_support/deprecation.rb +8 -8
- data/lib/active_support/deprecation/behaviors.rb +3 -3
- data/lib/active_support/deprecation/instance_delegator.rb +2 -2
- data/lib/active_support/deprecation/method_wrappers.rb +3 -3
- data/lib/active_support/deprecation/proxy_wrappers.rb +6 -4
- data/lib/active_support/deprecation/reporting.rb +7 -7
- data/lib/active_support/duration.rb +30 -26
- data/lib/active_support/duration/iso8601_parser.rb +66 -65
- data/lib/active_support/duration/iso8601_serializer.rb +11 -9
- data/lib/active_support/evented_file_update_checker.rb +59 -60
- data/lib/active_support/execution_wrapper.rb +3 -3
- data/lib/active_support/executor.rb +1 -1
- data/lib/active_support/file_update_checker.rb +54 -50
- data/lib/active_support/gem_version.rb +3 -3
- data/lib/active_support/gzip.rb +5 -5
- data/lib/active_support/hash_with_indifferent_access.rb +10 -20
- data/lib/active_support/i18n.rb +5 -5
- data/lib/active_support/i18n_railtie.rb +11 -10
- data/lib/active_support/inflections.rb +11 -11
- data/lib/active_support/inflector.rb +5 -5
- data/lib/active_support/inflector/inflections.rb +11 -9
- data/lib/active_support/inflector/methods.rb +51 -50
- data/lib/active_support/inflector/transliterate.rb +8 -11
- data/lib/active_support/json.rb +2 -2
- data/lib/active_support/json/decoding.rb +3 -3
- data/lib/active_support/json/encoding.rb +8 -7
- data/lib/active_support/key_generator.rb +17 -17
- data/lib/active_support/lazy_load_hooks.rb +12 -30
- data/lib/active_support/log_subscriber.rb +5 -5
- data/lib/active_support/log_subscriber/test_helper.rb +9 -9
- data/lib/active_support/logger.rb +3 -3
- data/lib/active_support/logger_silence.rb +3 -3
- data/lib/active_support/logger_thread_safe_level.rb +1 -1
- data/lib/active_support/message_encryptor.rb +72 -35
- data/lib/active_support/message_verifier.rb +7 -7
- data/lib/active_support/multibyte.rb +2 -2
- data/lib/active_support/multibyte/chars.rb +23 -21
- data/lib/active_support/multibyte/unicode.rb +68 -89
- data/lib/active_support/notifications.rb +5 -5
- data/lib/active_support/notifications/fanout.rb +3 -3
- data/lib/active_support/notifications/instrumenter.rb +5 -5
- data/lib/active_support/number_helper.rb +4 -4
- data/lib/active_support/number_helper/number_converter.rb +11 -11
- data/lib/active_support/number_helper/number_to_currency_converter.rb +3 -3
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +1 -2
- data/lib/active_support/number_helper/number_to_human_converter.rb +6 -6
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +6 -11
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +1 -1
- data/lib/active_support/number_helper/number_to_phone_converter.rb +1 -3
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +6 -6
- data/lib/active_support/option_merger.rb +1 -1
- data/lib/active_support/ordered_hash.rb +3 -3
- data/lib/active_support/ordered_options.rb +6 -4
- data/lib/active_support/per_thread_registry.rb +5 -5
- data/lib/active_support/rails.rb +12 -6
- data/lib/active_support/railtie.rb +3 -3
- data/lib/active_support/reloader.rb +1 -1
- data/lib/active_support/rescuable.rb +8 -14
- data/lib/active_support/security_utils.rb +1 -1
- data/lib/active_support/string_inquirer.rb +8 -2
- data/lib/active_support/subscriber.rb +9 -5
- data/lib/active_support/tagged_logging.rb +4 -4
- data/lib/active_support/test_case.rb +12 -29
- data/lib/active_support/testing/assertions.rb +100 -2
- data/lib/active_support/testing/autorun.rb +6 -2
- data/lib/active_support/testing/constant_lookup.rb +0 -1
- data/lib/active_support/testing/declarative.rb +1 -1
- data/lib/active_support/testing/deprecation.rb +3 -2
- data/lib/active_support/testing/isolation.rb +13 -22
- data/lib/active_support/testing/method_call_assertions.rb +1 -1
- data/lib/active_support/testing/setup_and_teardown.rb +2 -2
- data/lib/active_support/testing/stream.rb +28 -28
- data/lib/active_support/testing/tagged_logging.rb +1 -1
- data/lib/active_support/testing/time_helpers.rb +45 -12
- data/lib/active_support/time.rb +12 -12
- data/lib/active_support/time_with_zone.rb +16 -26
- data/lib/active_support/values/time_zone.rb +40 -46
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +1 -1
- data/lib/active_support/xml_mini.rb +34 -36
- data/lib/active_support/xml_mini/jdom.rb +112 -112
- data/lib/active_support/xml_mini/libxml.rb +15 -12
- data/lib/active_support/xml_mini/libxmlsax.rb +17 -15
- data/lib/active_support/xml_mini/nokogiri.rb +13 -11
- data/lib/active_support/xml_mini/nokogirisax.rb +15 -14
- data/lib/active_support/xml_mini/rexml.rb +9 -9
- metadata +8 -19
- data/lib/active_support/concurrency/latch.rb +0 -26
- data/lib/active_support/core_ext/kernel/debugger.rb +0 -3
- data/lib/active_support/core_ext/module/method_transplanting.rb +0 -3
- data/lib/active_support/core_ext/module/qualified_const.rb +0 -70
- data/lib/active_support/core_ext/struct.rb +0 -3
- data/lib/active_support/core_ext/time/marshal.rb +0 -3
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "active_support/core_ext/module/attribute_accessors"
|
2
2
|
|
3
3
|
module DateAndTime
|
4
4
|
module Compatibility
|
@@ -10,5 +10,13 @@ module DateAndTime
|
|
10
10
|
# this behavior, but new apps will have an initializer that sets
|
11
11
|
# this to true, because the new behavior is preferred.
|
12
12
|
mattr_accessor(:preserve_timezone, instance_writer: false) { false }
|
13
|
+
|
14
|
+
def to_time
|
15
|
+
if preserve_timezone
|
16
|
+
@_to_time_with_instance_offset ||= getlocal(utc_offset)
|
17
|
+
else
|
18
|
+
@_to_time_with_system_offset ||= getlocal
|
19
|
+
end
|
20
|
+
end
|
13
21
|
end
|
14
22
|
end
|
@@ -22,19 +22,18 @@ module DateAndTime
|
|
22
22
|
if time_zone
|
23
23
|
time_with_zone(time, time_zone)
|
24
24
|
else
|
25
|
-
time ||
|
25
|
+
time || to_time
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
private
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
def time_with_zone(time, zone)
|
32
|
+
if time
|
33
|
+
ActiveSupport::TimeWithZone.new(time.utc? ? time : time.getutc, zone)
|
34
|
+
else
|
35
|
+
ActiveSupport::TimeWithZone.new(nil, zone, to_time(:utc))
|
36
|
+
end
|
36
37
|
end
|
37
|
-
end
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
1
|
+
require "active_support/core_ext/date_time/acts_like"
|
2
|
+
require "active_support/core_ext/date_time/blank"
|
3
|
+
require "active_support/core_ext/date_time/calculations"
|
4
|
+
require "active_support/core_ext/date_time/compatibility"
|
5
|
+
require "active_support/core_ext/date_time/conversions"
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "date"
|
2
2
|
|
3
3
|
class DateTime
|
4
4
|
class << self
|
@@ -75,7 +75,7 @@ class DateTime
|
|
75
75
|
end
|
76
76
|
|
77
77
|
d = to_date.advance(options)
|
78
|
-
datetime_advanced_by_date = change(:
|
78
|
+
datetime_advanced_by_date = change(year: d.year, month: d.month, day: d.day)
|
79
79
|
seconds_to_advance = \
|
80
80
|
options.fetch(:seconds, 0) +
|
81
81
|
options.fetch(:minutes, 0) * 60 +
|
@@ -104,7 +104,7 @@ class DateTime
|
|
104
104
|
|
105
105
|
# Returns a new DateTime representing the start of the day (0:00).
|
106
106
|
def beginning_of_day
|
107
|
-
change(:
|
107
|
+
change(hour: 0)
|
108
108
|
end
|
109
109
|
alias :midnight :beginning_of_day
|
110
110
|
alias :at_midnight :beginning_of_day
|
@@ -112,7 +112,7 @@ class DateTime
|
|
112
112
|
|
113
113
|
# Returns a new DateTime representing the middle of the day (12:00)
|
114
114
|
def middle_of_day
|
115
|
-
change(:
|
115
|
+
change(hour: 12)
|
116
116
|
end
|
117
117
|
alias :midday :middle_of_day
|
118
118
|
alias :noon :middle_of_day
|
@@ -122,31 +122,31 @@ class DateTime
|
|
122
122
|
|
123
123
|
# Returns a new DateTime representing the end of the day (23:59:59).
|
124
124
|
def end_of_day
|
125
|
-
change(:
|
125
|
+
change(hour: 23, min: 59, sec: 59)
|
126
126
|
end
|
127
127
|
alias :at_end_of_day :end_of_day
|
128
128
|
|
129
129
|
# Returns a new DateTime representing the start of the hour (hh:00:00).
|
130
130
|
def beginning_of_hour
|
131
|
-
change(:
|
131
|
+
change(min: 0)
|
132
132
|
end
|
133
133
|
alias :at_beginning_of_hour :beginning_of_hour
|
134
134
|
|
135
135
|
# Returns a new DateTime representing the end of the hour (hh:59:59).
|
136
136
|
def end_of_hour
|
137
|
-
change(:
|
137
|
+
change(min: 59, sec: 59)
|
138
138
|
end
|
139
139
|
alias :at_end_of_hour :end_of_hour
|
140
140
|
|
141
141
|
# Returns a new DateTime representing the start of the minute (hh:mm:00).
|
142
142
|
def beginning_of_minute
|
143
|
-
change(:
|
143
|
+
change(sec: 0)
|
144
144
|
end
|
145
145
|
alias :at_beginning_of_minute :beginning_of_minute
|
146
146
|
|
147
147
|
# Returns a new DateTime representing the end of the minute (hh:mm:59).
|
148
148
|
def end_of_minute
|
149
|
-
change(:
|
149
|
+
change(sec: 59)
|
150
150
|
end
|
151
151
|
alias :at_end_of_minute :end_of_minute
|
152
152
|
|
@@ -1,16 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require 'active_support/core_ext/module/remove_method'
|
1
|
+
require "active_support/core_ext/date_and_time/compatibility"
|
3
2
|
|
4
3
|
class DateTime
|
5
|
-
|
6
|
-
|
7
|
-
remove_possible_method :to_time
|
8
|
-
|
9
|
-
# Either return an instance of `Time` with the same UTC offset
|
10
|
-
# as +self+ or an instance of `Time` representing the same time
|
11
|
-
# in the the local system timezone depending on the setting of
|
12
|
-
# on the setting of +ActiveSupport.to_time_preserves_timezone+.
|
13
|
-
def to_time
|
14
|
-
preserve_timezone ? getlocal(utc_offset) : getlocal
|
15
|
-
end
|
4
|
+
prepend DateAndTime::Compatibility
|
16
5
|
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
1
|
+
require "date"
|
2
|
+
require "active_support/inflector/methods"
|
3
|
+
require "active_support/core_ext/time/conversions"
|
4
|
+
require "active_support/core_ext/date_time/calculations"
|
5
|
+
require "active_support/values/time_zone"
|
6
6
|
|
7
7
|
class DateTime
|
8
8
|
# Convert to a formatted string. See Time::DATE_FORMATS for predefined formats.
|
@@ -64,7 +64,7 @@ class DateTime
|
|
64
64
|
# # => Sun, 01 Jan 2012 00:00:00 +0300
|
65
65
|
# DateTime.civil_from_format :local, 2012, 12, 17
|
66
66
|
# # => Mon, 17 Dec 2012 00:00:00 +0000
|
67
|
-
def self.civil_from_format(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0)
|
67
|
+
def self.civil_from_format(utc_or_local, year, month = 1, day = 1, hour = 0, min = 0, sec = 0)
|
68
68
|
if utc_or_local.to_sym == :local
|
69
69
|
offset = ::Time.local(year, month, day).utc_offset.to_r / 86400
|
70
70
|
else
|
@@ -95,11 +95,11 @@ class DateTime
|
|
95
95
|
|
96
96
|
private
|
97
97
|
|
98
|
-
|
99
|
-
|
100
|
-
|
98
|
+
def offset_in_seconds
|
99
|
+
(offset * 86400).to_i
|
100
|
+
end
|
101
101
|
|
102
|
-
|
103
|
-
|
104
|
-
|
102
|
+
def seconds_since_unix_epoch
|
103
|
+
(jd - 2440588) * 86400 - offset_in_seconds + seconds_since_midnight
|
104
|
+
end
|
105
105
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "securerandom"
|
2
2
|
|
3
3
|
module Digest
|
4
4
|
module UUID
|
@@ -26,7 +26,7 @@ module Digest
|
|
26
26
|
hash.update(uuid_namespace)
|
27
27
|
hash.update(name)
|
28
28
|
|
29
|
-
ary = hash.digest.unpack(
|
29
|
+
ary = hash.digest.unpack("NnnnnN")
|
30
30
|
ary[2] = (ary[2] & 0x0FFF) | (version << 12)
|
31
31
|
ary[3] = (ary[3] & 0x3FFF) | 0x8000
|
32
32
|
|
@@ -35,12 +35,12 @@ module Digest
|
|
35
35
|
|
36
36
|
# Convenience method for uuid_from_hash using Digest::MD5.
|
37
37
|
def self.uuid_v3(uuid_namespace, name)
|
38
|
-
|
38
|
+
uuid_from_hash(Digest::MD5, uuid_namespace, name)
|
39
39
|
end
|
40
40
|
|
41
41
|
# Convenience method for uuid_from_hash using Digest::SHA1.
|
42
42
|
def self.uuid_v5(uuid_namespace, name)
|
43
|
-
|
43
|
+
uuid_from_hash(Digest::SHA1, uuid_namespace, name)
|
44
44
|
end
|
45
45
|
|
46
46
|
# Convenience method for SecureRandom.uuid.
|
@@ -1,62 +1,42 @@
|
|
1
1
|
module Enumerable
|
2
|
-
#
|
3
|
-
# when we omit an identity.
|
2
|
+
# Calculates a sum from the elements.
|
4
3
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
#
|
28
|
-
# The default sum of an empty list is zero. You can override this default:
|
29
|
-
#
|
30
|
-
# [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0)
|
31
|
-
def sum(identity = nil, &block)
|
32
|
-
if identity
|
33
|
-
_original_sum_with_required_identity(identity, &block)
|
34
|
-
elsif block_given?
|
35
|
-
map(&block).sum(identity)
|
36
|
-
else
|
37
|
-
inject(:+) || 0
|
38
|
-
end
|
39
|
-
end
|
40
|
-
else
|
41
|
-
def sum(identity = nil, &block)
|
42
|
-
if block_given?
|
43
|
-
map(&block).sum(identity)
|
44
|
-
else
|
45
|
-
sum = identity ? inject(identity, :+) : inject(:+)
|
46
|
-
sum || identity || 0
|
47
|
-
end
|
4
|
+
# payments.sum { |p| p.price * p.tax_rate }
|
5
|
+
# payments.sum(&:price)
|
6
|
+
#
|
7
|
+
# The latter is a shortcut for:
|
8
|
+
#
|
9
|
+
# payments.inject(0) { |sum, p| sum + p.price }
|
10
|
+
#
|
11
|
+
# It can also calculate the sum without the use of a block.
|
12
|
+
#
|
13
|
+
# [5, 15, 10].sum # => 30
|
14
|
+
# ['foo', 'bar'].sum # => "foobar"
|
15
|
+
# [[1, 2], [3, 1, 5]].sum # => [1, 2, 3, 1, 5]
|
16
|
+
#
|
17
|
+
# The default sum of an empty list is zero. You can override this default:
|
18
|
+
#
|
19
|
+
# [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0)
|
20
|
+
def sum(identity = nil, &block)
|
21
|
+
if block_given?
|
22
|
+
map(&block).sum(identity)
|
23
|
+
else
|
24
|
+
sum = identity ? inject(identity, :+) : inject(:+)
|
25
|
+
sum || identity || 0
|
48
26
|
end
|
49
27
|
end
|
50
28
|
|
51
29
|
# Convert an enumerable to a hash.
|
52
30
|
#
|
53
31
|
# people.index_by(&:login)
|
54
|
-
#
|
32
|
+
# # => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
|
55
33
|
# people.index_by { |person| "#{person.first_name} #{person.last_name}" }
|
56
|
-
#
|
34
|
+
# # => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
|
57
35
|
def index_by
|
58
36
|
if block_given?
|
59
|
-
|
37
|
+
result = {}
|
38
|
+
each { |elem| result[yield(elem)] = elem }
|
39
|
+
result
|
60
40
|
else
|
61
41
|
to_enum(:index_by) { size if respond_to?(:size) }
|
62
42
|
end
|
@@ -87,10 +67,10 @@ module Enumerable
|
|
87
67
|
# Returns a copy of the enumerable without the specified elements.
|
88
68
|
#
|
89
69
|
# ["David", "Rafael", "Aaron", "Todd"].without "Aaron", "Todd"
|
90
|
-
#
|
70
|
+
# # => ["David", "Rafael"]
|
91
71
|
#
|
92
72
|
# {foo: 1, bar: 2, baz: 3}.without :bar
|
93
|
-
#
|
73
|
+
# # => {foo: 1, baz: 3}
|
94
74
|
def without(*elements)
|
95
75
|
reject { |element| elements.include?(element) }
|
96
76
|
end
|
@@ -98,10 +78,10 @@ module Enumerable
|
|
98
78
|
# Convert an enumerable to an array based on the given key.
|
99
79
|
#
|
100
80
|
# [{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pluck(:name)
|
101
|
-
#
|
81
|
+
# # => ["David", "Rafael", "Aaron"]
|
102
82
|
#
|
103
83
|
# [{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pluck(:id, :name)
|
104
|
-
#
|
84
|
+
# # => [[1, "David"], [2, "Rafael"]]
|
105
85
|
def pluck(*keys)
|
106
86
|
if keys.many?
|
107
87
|
map { |element| keys.map { |key| element[key] } }
|
@@ -135,12 +115,21 @@ end
|
|
135
115
|
# and fall back to the compatible implementation, but that's much slower than
|
136
116
|
# just calling the compat method in the first place.
|
137
117
|
if Array.instance_methods(false).include?(:sum) && !(%w[a].sum rescue false)
|
138
|
-
|
139
|
-
|
118
|
+
# Using Refinements here in order not to expose our internal method
|
119
|
+
using Module.new {
|
120
|
+
refine Array do
|
121
|
+
alias :orig_sum :sum
|
122
|
+
end
|
123
|
+
}
|
140
124
|
|
141
|
-
|
142
|
-
|
143
|
-
|
125
|
+
class Array
|
126
|
+
def sum(init = nil, &block) #:nodoc:
|
127
|
+
if init.is_a?(Numeric) || first.is_a?(Numeric)
|
128
|
+
init ||= 0
|
129
|
+
orig_sum(init, &block)
|
130
|
+
else
|
131
|
+
super
|
132
|
+
end
|
144
133
|
end
|
145
134
|
end
|
146
135
|
end
|
@@ -1 +1 @@
|
|
1
|
-
require
|
1
|
+
require "active_support/core_ext/file/atomic"
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "fileutils"
|
2
2
|
|
3
3
|
class File
|
4
4
|
# Write to a file atomically. Useful for situations where you don't
|
@@ -17,7 +17,7 @@ class File
|
|
17
17
|
# file.write('hello')
|
18
18
|
# end
|
19
19
|
def self.atomic_write(file_name, temp_dir = dirname(file_name))
|
20
|
-
require
|
20
|
+
require "tempfile" unless defined?(Tempfile)
|
21
21
|
|
22
22
|
Tempfile.open(".#{basename(file_name)}", temp_dir) do |temp_file|
|
23
23
|
temp_file.binmode
|
@@ -53,11 +53,11 @@ class File
|
|
53
53
|
# Private utility method.
|
54
54
|
def self.probe_stat_in(dir) #:nodoc:
|
55
55
|
basename = [
|
56
|
-
|
56
|
+
".permissions_check",
|
57
57
|
Thread.current.object_id,
|
58
58
|
Process.pid,
|
59
59
|
rand(1000000)
|
60
|
-
].join(
|
60
|
+
].join(".")
|
61
61
|
|
62
62
|
file_name = join(dir, basename)
|
63
63
|
FileUtils.touch(file_name)
|
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
1
|
+
require "active_support/core_ext/hash/compact"
|
2
|
+
require "active_support/core_ext/hash/conversions"
|
3
|
+
require "active_support/core_ext/hash/deep_merge"
|
4
|
+
require "active_support/core_ext/hash/except"
|
5
|
+
require "active_support/core_ext/hash/indifferent_access"
|
6
|
+
require "active_support/core_ext/hash/keys"
|
7
|
+
require "active_support/core_ext/hash/reverse_merge"
|
8
|
+
require "active_support/core_ext/hash/slice"
|
9
|
+
require "active_support/core_ext/hash/transform_values"
|
@@ -2,23 +2,26 @@ class Hash
|
|
2
2
|
unless Hash.instance_methods(false).include?(:compact)
|
3
3
|
# Returns a hash with non +nil+ values.
|
4
4
|
#
|
5
|
-
# hash = { a: true, b: false, c: nil}
|
6
|
-
# hash.compact
|
7
|
-
# hash
|
8
|
-
# { c: nil }.compact
|
5
|
+
# hash = { a: true, b: false, c: nil }
|
6
|
+
# hash.compact # => { a: true, b: false }
|
7
|
+
# hash # => { a: true, b: false, c: nil }
|
8
|
+
# { c: nil }.compact # => {}
|
9
|
+
# { c: true }.compact # => { c: true }
|
9
10
|
def compact
|
10
|
-
|
11
|
+
select { |_, value| !value.nil? }
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
14
15
|
unless Hash.instance_methods(false).include?(:compact!)
|
15
16
|
# Replaces current hash with non +nil+ values.
|
17
|
+
# Returns +nil+ if no changes were made, otherwise returns the hash.
|
16
18
|
#
|
17
|
-
# hash = { a: true, b: false, c: nil}
|
18
|
-
# hash.compact!
|
19
|
-
# hash
|
19
|
+
# hash = { a: true, b: false, c: nil }
|
20
|
+
# hash.compact! # => { a: true, b: false }
|
21
|
+
# hash # => { a: true, b: false }
|
22
|
+
# { c: true }.compact! # => nil
|
20
23
|
def compact!
|
21
|
-
|
24
|
+
reject! { |_, value| value.nil? }
|
22
25
|
end
|
23
26
|
end
|
24
27
|
end
|