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.

Files changed (210) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +215 -820
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +1 -1
  5. data/lib/active_support.rb +8 -4
  6. data/lib/active_support/all.rb +3 -3
  7. data/lib/active_support/array_inquirer.rb +7 -5
  8. data/lib/active_support/backtrace_cleaner.rb +4 -4
  9. data/lib/active_support/benchmarkable.rb +3 -3
  10. data/lib/active_support/builder.rb +1 -1
  11. data/lib/active_support/cache.rb +42 -49
  12. data/lib/active_support/cache/file_store.rb +12 -21
  13. data/lib/active_support/cache/mem_cache_store.rb +30 -40
  14. data/lib/active_support/cache/memory_store.rb +11 -13
  15. data/lib/active_support/cache/null_store.rb +4 -4
  16. data/lib/active_support/cache/strategy/local_cache.rb +16 -25
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +8 -9
  18. data/lib/active_support/callbacks.rb +647 -584
  19. data/lib/active_support/concurrency/share_lock.rb +20 -21
  20. data/lib/active_support/configurable.rb +5 -5
  21. data/lib/active_support/core_ext.rb +1 -2
  22. data/lib/active_support/core_ext/array.rb +7 -7
  23. data/lib/active_support/core_ext/array/access.rb +1 -1
  24. data/lib/active_support/core_ext/array/conversions.rb +15 -15
  25. data/lib/active_support/core_ext/array/grouping.rb +1 -1
  26. data/lib/active_support/core_ext/array/inquiry.rb +1 -1
  27. data/lib/active_support/core_ext/array/prepend_and_append.rb +1 -1
  28. data/lib/active_support/core_ext/benchmark.rb +1 -1
  29. data/lib/active_support/core_ext/big_decimal.rb +1 -1
  30. data/lib/active_support/core_ext/big_decimal/conversions.rb +4 -6
  31. data/lib/active_support/core_ext/class.rb +2 -2
  32. data/lib/active_support/core_ext/class/attribute.rb +5 -5
  33. data/lib/active_support/core_ext/class/attribute_accessors.rb +1 -1
  34. data/lib/active_support/core_ext/class/subclasses.rb +18 -4
  35. data/lib/active_support/core_ext/date.rb +5 -5
  36. data/lib/active_support/core_ext/date/acts_like.rb +1 -1
  37. data/lib/active_support/core_ext/date/blank.rb +1 -1
  38. data/lib/active_support/core_ext/date/calculations.rb +8 -8
  39. data/lib/active_support/core_ext/date/conversions.rb +12 -12
  40. data/lib/active_support/core_ext/date/zones.rb +2 -2
  41. data/lib/active_support/core_ext/date_and_time/calculations.rb +27 -22
  42. data/lib/active_support/core_ext/date_and_time/compatibility.rb +9 -1
  43. data/lib/active_support/core_ext/date_and_time/zones.rb +7 -8
  44. data/lib/active_support/core_ext/date_time.rb +5 -5
  45. data/lib/active_support/core_ext/date_time/acts_like.rb +2 -2
  46. data/lib/active_support/core_ext/date_time/blank.rb +1 -1
  47. data/lib/active_support/core_ext/date_time/calculations.rb +9 -9
  48. data/lib/active_support/core_ext/date_time/compatibility.rb +2 -13
  49. data/lib/active_support/core_ext/date_time/conversions.rb +12 -12
  50. data/lib/active_support/core_ext/digest/uuid.rb +4 -4
  51. data/lib/active_support/core_ext/enumerable.rb +46 -57
  52. data/lib/active_support/core_ext/file.rb +1 -1
  53. data/lib/active_support/core_ext/file/atomic.rb +4 -4
  54. data/lib/active_support/core_ext/hash.rb +9 -9
  55. data/lib/active_support/core_ext/hash/compact.rb +12 -9
  56. data/lib/active_support/core_ext/hash/conversions.rb +36 -37
  57. data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -2
  58. data/lib/active_support/core_ext/hash/keys.rb +8 -8
  59. data/lib/active_support/core_ext/hash/reverse_merge.rb +1 -1
  60. data/lib/active_support/core_ext/hash/slice.rb +4 -4
  61. data/lib/active_support/core_ext/hash/transform_values.rb +1 -0
  62. data/lib/active_support/core_ext/integer.rb +3 -3
  63. data/lib/active_support/core_ext/integer/inflections.rb +1 -1
  64. data/lib/active_support/core_ext/integer/time.rb +2 -2
  65. data/lib/active_support/core_ext/kernel.rb +4 -4
  66. data/lib/active_support/core_ext/kernel/concern.rb +1 -1
  67. data/lib/active_support/core_ext/kernel/reporting.rb +1 -1
  68. data/lib/active_support/core_ext/load_error.rb +1 -18
  69. data/lib/active_support/core_ext/marshal.rb +2 -2
  70. data/lib/active_support/core_ext/module.rb +11 -12
  71. data/lib/active_support/core_ext/module/aliasing.rb +3 -48
  72. data/lib/active_support/core_ext/module/attr_internal.rb +4 -4
  73. data/lib/active_support/core_ext/module/attribute_accessors.rb +11 -5
  74. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +20 -13
  75. data/lib/active_support/core_ext/module/concerning.rb +1 -1
  76. data/lib/active_support/core_ext/module/delegation.rb +82 -16
  77. data/lib/active_support/core_ext/module/introspection.rb +3 -11
  78. data/lib/active_support/core_ext/module/reachable.rb +2 -2
  79. data/lib/active_support/core_ext/numeric.rb +4 -4
  80. data/lib/active_support/core_ext/numeric/conversions.rb +3 -9
  81. data/lib/active_support/core_ext/numeric/inquiry.rb +21 -21
  82. data/lib/active_support/core_ext/numeric/time.rb +5 -5
  83. data/lib/active_support/core_ext/object.rb +12 -12
  84. data/lib/active_support/core_ext/object/blank.rb +3 -1
  85. data/lib/active_support/core_ext/object/conversions.rb +4 -4
  86. data/lib/active_support/core_ext/object/deep_dup.rb +1 -1
  87. data/lib/active_support/core_ext/object/duplicable.rb +24 -4
  88. data/lib/active_support/core_ext/object/inclusion.rb +1 -1
  89. data/lib/active_support/core_ext/object/json.rb +26 -12
  90. data/lib/active_support/core_ext/object/to_param.rb +1 -1
  91. data/lib/active_support/core_ext/object/to_query.rb +4 -4
  92. data/lib/active_support/core_ext/object/try.rb +1 -1
  93. data/lib/active_support/core_ext/object/with_options.rb +1 -1
  94. data/lib/active_support/core_ext/range.rb +4 -4
  95. data/lib/active_support/core_ext/range/conversions.rb +1 -1
  96. data/lib/active_support/core_ext/regexp.rb +4 -0
  97. data/lib/active_support/core_ext/securerandom.rb +3 -3
  98. data/lib/active_support/core_ext/string.rb +13 -13
  99. data/lib/active_support/core_ext/string/access.rb +6 -6
  100. data/lib/active_support/core_ext/string/conversions.rb +2 -2
  101. data/lib/active_support/core_ext/string/filters.rb +3 -3
  102. data/lib/active_support/core_ext/string/indent.rb +4 -4
  103. data/lib/active_support/core_ext/string/inflections.rb +10 -14
  104. data/lib/active_support/core_ext/string/inquiry.rb +1 -1
  105. data/lib/active_support/core_ext/string/multibyte.rb +1 -1
  106. data/lib/active_support/core_ext/string/output_safety.rb +19 -20
  107. data/lib/active_support/core_ext/string/strip.rb +1 -1
  108. data/lib/active_support/core_ext/string/zones.rb +2 -2
  109. data/lib/active_support/core_ext/time.rb +5 -5
  110. data/lib/active_support/core_ext/time/acts_like.rb +1 -1
  111. data/lib/active_support/core_ext/time/calculations.rb +23 -29
  112. data/lib/active_support/core_ext/time/compatibility.rb +1 -10
  113. data/lib/active_support/core_ext/time/conversions.rb +12 -12
  114. data/lib/active_support/core_ext/time/zones.rb +3 -3
  115. data/lib/active_support/core_ext/uri.rb +2 -2
  116. data/lib/active_support/dependencies.rb +45 -47
  117. data/lib/active_support/dependencies/interlock.rb +1 -1
  118. data/lib/active_support/deprecation.rb +8 -8
  119. data/lib/active_support/deprecation/behaviors.rb +3 -3
  120. data/lib/active_support/deprecation/instance_delegator.rb +2 -2
  121. data/lib/active_support/deprecation/method_wrappers.rb +3 -3
  122. data/lib/active_support/deprecation/proxy_wrappers.rb +6 -4
  123. data/lib/active_support/deprecation/reporting.rb +7 -7
  124. data/lib/active_support/duration.rb +30 -26
  125. data/lib/active_support/duration/iso8601_parser.rb +66 -65
  126. data/lib/active_support/duration/iso8601_serializer.rb +11 -9
  127. data/lib/active_support/evented_file_update_checker.rb +59 -60
  128. data/lib/active_support/execution_wrapper.rb +3 -3
  129. data/lib/active_support/executor.rb +1 -1
  130. data/lib/active_support/file_update_checker.rb +54 -50
  131. data/lib/active_support/gem_version.rb +3 -3
  132. data/lib/active_support/gzip.rb +5 -5
  133. data/lib/active_support/hash_with_indifferent_access.rb +10 -20
  134. data/lib/active_support/i18n.rb +5 -5
  135. data/lib/active_support/i18n_railtie.rb +11 -10
  136. data/lib/active_support/inflections.rb +11 -11
  137. data/lib/active_support/inflector.rb +5 -5
  138. data/lib/active_support/inflector/inflections.rb +11 -9
  139. data/lib/active_support/inflector/methods.rb +51 -50
  140. data/lib/active_support/inflector/transliterate.rb +8 -11
  141. data/lib/active_support/json.rb +2 -2
  142. data/lib/active_support/json/decoding.rb +3 -3
  143. data/lib/active_support/json/encoding.rb +8 -7
  144. data/lib/active_support/key_generator.rb +17 -17
  145. data/lib/active_support/lazy_load_hooks.rb +12 -30
  146. data/lib/active_support/log_subscriber.rb +5 -5
  147. data/lib/active_support/log_subscriber/test_helper.rb +9 -9
  148. data/lib/active_support/logger.rb +3 -3
  149. data/lib/active_support/logger_silence.rb +3 -3
  150. data/lib/active_support/logger_thread_safe_level.rb +1 -1
  151. data/lib/active_support/message_encryptor.rb +72 -35
  152. data/lib/active_support/message_verifier.rb +7 -7
  153. data/lib/active_support/multibyte.rb +2 -2
  154. data/lib/active_support/multibyte/chars.rb +23 -21
  155. data/lib/active_support/multibyte/unicode.rb +68 -89
  156. data/lib/active_support/notifications.rb +5 -5
  157. data/lib/active_support/notifications/fanout.rb +3 -3
  158. data/lib/active_support/notifications/instrumenter.rb +5 -5
  159. data/lib/active_support/number_helper.rb +4 -4
  160. data/lib/active_support/number_helper/number_converter.rb +11 -11
  161. data/lib/active_support/number_helper/number_to_currency_converter.rb +3 -3
  162. data/lib/active_support/number_helper/number_to_delimited_converter.rb +1 -2
  163. data/lib/active_support/number_helper/number_to_human_converter.rb +6 -6
  164. data/lib/active_support/number_helper/number_to_human_size_converter.rb +6 -11
  165. data/lib/active_support/number_helper/number_to_percentage_converter.rb +1 -1
  166. data/lib/active_support/number_helper/number_to_phone_converter.rb +1 -3
  167. data/lib/active_support/number_helper/number_to_rounded_converter.rb +6 -6
  168. data/lib/active_support/option_merger.rb +1 -1
  169. data/lib/active_support/ordered_hash.rb +3 -3
  170. data/lib/active_support/ordered_options.rb +6 -4
  171. data/lib/active_support/per_thread_registry.rb +5 -5
  172. data/lib/active_support/rails.rb +12 -6
  173. data/lib/active_support/railtie.rb +3 -3
  174. data/lib/active_support/reloader.rb +1 -1
  175. data/lib/active_support/rescuable.rb +8 -14
  176. data/lib/active_support/security_utils.rb +1 -1
  177. data/lib/active_support/string_inquirer.rb +8 -2
  178. data/lib/active_support/subscriber.rb +9 -5
  179. data/lib/active_support/tagged_logging.rb +4 -4
  180. data/lib/active_support/test_case.rb +12 -29
  181. data/lib/active_support/testing/assertions.rb +100 -2
  182. data/lib/active_support/testing/autorun.rb +6 -2
  183. data/lib/active_support/testing/constant_lookup.rb +0 -1
  184. data/lib/active_support/testing/declarative.rb +1 -1
  185. data/lib/active_support/testing/deprecation.rb +3 -2
  186. data/lib/active_support/testing/isolation.rb +13 -22
  187. data/lib/active_support/testing/method_call_assertions.rb +1 -1
  188. data/lib/active_support/testing/setup_and_teardown.rb +2 -2
  189. data/lib/active_support/testing/stream.rb +28 -28
  190. data/lib/active_support/testing/tagged_logging.rb +1 -1
  191. data/lib/active_support/testing/time_helpers.rb +45 -12
  192. data/lib/active_support/time.rb +12 -12
  193. data/lib/active_support/time_with_zone.rb +16 -26
  194. data/lib/active_support/values/time_zone.rb +40 -46
  195. data/lib/active_support/values/unicode_tables.dat +0 -0
  196. data/lib/active_support/version.rb +1 -1
  197. data/lib/active_support/xml_mini.rb +34 -36
  198. data/lib/active_support/xml_mini/jdom.rb +112 -112
  199. data/lib/active_support/xml_mini/libxml.rb +15 -12
  200. data/lib/active_support/xml_mini/libxmlsax.rb +17 -15
  201. data/lib/active_support/xml_mini/nokogiri.rb +13 -11
  202. data/lib/active_support/xml_mini/nokogirisax.rb +15 -14
  203. data/lib/active_support/xml_mini/rexml.rb +9 -9
  204. metadata +8 -19
  205. data/lib/active_support/concurrency/latch.rb +0 -26
  206. data/lib/active_support/core_ext/kernel/debugger.rb +0 -3
  207. data/lib/active_support/core_ext/module/method_transplanting.rb +0 -3
  208. data/lib/active_support/core_ext/module/qualified_const.rb +0 -70
  209. data/lib/active_support/core_ext/struct.rb +0 -3
  210. data/lib/active_support/core_ext/time/marshal.rb +0 -3
@@ -1,4 +1,4 @@
1
- require 'active_support/core_ext/module/attribute_accessors'
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 || self.to_time
25
+ time || to_time
26
26
  end
27
27
  end
28
28
 
29
29
  private
30
30
 
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))
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 '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
+ 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,5 +1,5 @@
1
- require 'date'
2
- require 'active_support/core_ext/object/acts_like'
1
+ require "date"
2
+ require "active_support/core_ext/object/acts_like"
3
3
 
4
4
  class DateTime
5
5
  # Duck-types as a Date-like class. See Object#acts_like?.
@@ -1,4 +1,4 @@
1
- require 'date'
1
+ require "date"
2
2
 
3
3
  class DateTime #:nodoc:
4
4
  # No DateTime is ever blank:
@@ -1,4 +1,4 @@
1
- require 'date'
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(:year => d.year, :month => d.month, :day => d.day)
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(:hour => 0)
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(:hour => 12)
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(:hour => 23, :min => 59, :sec => 59)
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(:min => 0)
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(:min => 59, :sec => 59)
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(:sec => 0)
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(:sec => 59)
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 'active_support/core_ext/date_and_time/compatibility'
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
- include DateAndTime::Compatibility
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 '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'
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
- def offset_in_seconds
99
- (offset * 86400).to_i
100
- end
98
+ def offset_in_seconds
99
+ (offset * 86400).to_i
100
+ end
101
101
 
102
- def seconds_since_unix_epoch
103
- (jd - 2440588) * 86400 - offset_in_seconds + seconds_since_midnight
104
- end
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 'securerandom'
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('NnnnnN')
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
- self.uuid_from_hash(Digest::MD5, uuid_namespace, name)
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
- self.uuid_from_hash(Digest::SHA1, uuid_namespace, name)
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
- # Enumerable#sum was added in Ruby 2.4 but it only works with Numeric elements
3
- # when we omit an identity.
2
+ # Calculates a sum from the elements.
4
3
  #
5
- # We tried shimming it to attempt the fast native method, rescue TypeError,
6
- # and fall back to the compatible implementation, but that's much slower than
7
- # just calling the compat method in the first place.
8
- if Enumerable.instance_methods(false).include?(:sum) && !((?a..?b).sum rescue false)
9
- # We can't use Refinements here because Refinements with Module which will be prepended
10
- # doesn't work well https://bugs.ruby-lang.org/issues/13446
11
- alias :_original_sum_with_required_identity :sum
12
- private :_original_sum_with_required_identity
13
- # Calculates a sum from the elements.
14
- #
15
- # payments.sum { |p| p.price * p.tax_rate }
16
- # payments.sum(&:price)
17
- #
18
- # The latter is a shortcut for:
19
- #
20
- # payments.inject(0) { |sum, p| sum + p.price }
21
- #
22
- # It can also calculate the sum without the use of a block.
23
- #
24
- # [5, 15, 10].sum # => 30
25
- # ['foo', 'bar'].sum # => "foobar"
26
- # [[1, 2], [3, 1, 5]].sum # => [1, 2, 3, 1, 5]
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
- # => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
32
+ # # => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
55
33
  # people.index_by { |person| "#{person.first_name} #{person.last_name}" }
56
- # => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
34
+ # # => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
57
35
  def index_by
58
36
  if block_given?
59
- Hash[map { |elem| [yield(elem), elem] }]
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
- # => ["David", "Rafael"]
70
+ # # => ["David", "Rafael"]
91
71
  #
92
72
  # {foo: 1, bar: 2, baz: 3}.without :bar
93
- # => {foo: 1, baz: 3}
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
- # => ["David", "Rafael", "Aaron"]
81
+ # # => ["David", "Rafael", "Aaron"]
102
82
  #
103
83
  # [{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pluck(:id, :name)
104
- # => [[1, "David"], [2, "Rafael"]]
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
- class Array
139
- remove_method :sum
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
- def sum(*args) #:nodoc:
142
- # Use Enumerable#sum instead.
143
- super
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 'active_support/core_ext/file/atomic'
1
+ require "active_support/core_ext/file/atomic"
@@ -1,4 +1,4 @@
1
- require 'fileutils'
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 'tempfile' unless defined?(Tempfile)
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
- '.permissions_check',
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 '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'
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 # => { a: true, b: false}
7
- # hash # => { a: true, b: false, c: nil}
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
- self.select { |_, value| !value.nil? }
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! # => { a: true, b: false}
19
- # hash # => { a: true, b: false}
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
- self.reject! { |_, value| value.nil? }
24
+ reject! { |_, value| value.nil? }
22
25
  end
23
26
  end
24
27
  end