activesupport 4.2.0 → 5.2.0

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 (254) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +366 -232
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +4 -5
  5. data/lib/active_support.rb +17 -7
  6. data/lib/active_support/all.rb +5 -3
  7. data/lib/active_support/array_inquirer.rb +48 -0
  8. data/lib/active_support/backtrace_cleaner.rb +7 -5
  9. data/lib/active_support/benchmarkable.rb +6 -4
  10. data/lib/active_support/builder.rb +3 -1
  11. data/lib/active_support/cache.rb +271 -177
  12. data/lib/active_support/cache/file_store.rb +41 -35
  13. data/lib/active_support/cache/mem_cache_store.rb +97 -88
  14. data/lib/active_support/cache/memory_store.rb +27 -30
  15. data/lib/active_support/cache/null_store.rb +7 -8
  16. data/lib/active_support/cache/redis_cache_store.rb +454 -0
  17. data/lib/active_support/cache/strategy/local_cache.rb +67 -34
  18. data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
  19. data/lib/active_support/callbacks.rb +654 -560
  20. data/lib/active_support/concern.rb +5 -3
  21. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
  22. data/lib/active_support/concurrency/share_lock.rb +227 -0
  23. data/lib/active_support/configurable.rb +8 -5
  24. data/lib/active_support/core_ext.rb +3 -1
  25. data/lib/active_support/core_ext/array.rb +9 -6
  26. data/lib/active_support/core_ext/array/access.rb +29 -1
  27. data/lib/active_support/core_ext/array/conversions.rb +22 -18
  28. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  29. data/lib/active_support/core_ext/array/grouping.rb +11 -18
  30. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  31. data/lib/active_support/core_ext/array/prepend_and_append.rb +5 -3
  32. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  33. data/lib/active_support/core_ext/benchmark.rb +3 -1
  34. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  35. data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
  36. data/lib/active_support/core_ext/class.rb +4 -3
  37. data/lib/active_support/core_ext/class/attribute.rb +41 -22
  38. data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
  39. data/lib/active_support/core_ext/class/subclasses.rb +20 -8
  40. data/lib/active_support/core_ext/date.rb +6 -4
  41. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  42. data/lib/active_support/core_ext/date/blank.rb +14 -0
  43. data/lib/active_support/core_ext/date/calculations.rb +11 -9
  44. data/lib/active_support/core_ext/date/conversions.rb +31 -23
  45. data/lib/active_support/core_ext/date/zones.rb +4 -2
  46. data/lib/active_support/core_ext/date_and_time/calculations.rb +179 -56
  47. data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
  48. data/lib/active_support/core_ext/date_and_time/zones.rb +12 -12
  49. data/lib/active_support/core_ext/date_time.rb +7 -4
  50. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  51. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  52. data/lib/active_support/core_ext/date_time/calculations.rb +58 -20
  53. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  54. data/lib/active_support/core_ext/date_time/conversions.rb +16 -12
  55. data/lib/active_support/core_ext/digest/uuid.rb +7 -5
  56. data/lib/active_support/core_ext/enumerable.rb +107 -28
  57. data/lib/active_support/core_ext/file.rb +3 -1
  58. data/lib/active_support/core_ext/file/atomic.rb +38 -31
  59. data/lib/active_support/core_ext/hash.rb +11 -9
  60. data/lib/active_support/core_ext/hash/compact.rb +24 -15
  61. data/lib/active_support/core_ext/hash/conversions.rb +63 -43
  62. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  63. data/lib/active_support/core_ext/hash/except.rb +11 -8
  64. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
  65. data/lib/active_support/core_ext/hash/keys.rb +33 -27
  66. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  67. data/lib/active_support/core_ext/hash/slice.rb +8 -8
  68. data/lib/active_support/core_ext/hash/transform_values.rb +16 -7
  69. data/lib/active_support/core_ext/integer.rb +5 -3
  70. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  71. data/lib/active_support/core_ext/integer/multiple.rb +2 -0
  72. data/lib/active_support/core_ext/integer/time.rb +11 -33
  73. data/lib/active_support/core_ext/kernel.rb +6 -5
  74. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
  75. data/lib/active_support/core_ext/kernel/concern.rb +5 -1
  76. data/lib/active_support/core_ext/kernel/reporting.rb +4 -83
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  78. data/lib/active_support/core_ext/load_error.rb +3 -22
  79. data/lib/active_support/core_ext/marshal.rb +13 -10
  80. data/lib/active_support/core_ext/module.rb +14 -11
  81. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  82. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  83. data/lib/active_support/core_ext/module/attr_internal.rb +8 -9
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +43 -40
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +150 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +11 -12
  87. data/lib/active_support/core_ext/module/delegation.rb +121 -39
  88. data/lib/active_support/core_ext/module/deprecation.rb +4 -2
  89. data/lib/active_support/core_ext/module/introspection.rb +9 -9
  90. data/lib/active_support/core_ext/module/reachable.rb +5 -2
  91. data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +8 -3
  93. data/lib/active_support/core_ext/name_error.rb +22 -2
  94. data/lib/active_support/core_ext/numeric.rb +6 -3
  95. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  96. data/lib/active_support/core_ext/numeric/conversions.rb +79 -74
  97. data/lib/active_support/core_ext/numeric/inquiry.rb +28 -0
  98. data/lib/active_support/core_ext/numeric/time.rb +35 -38
  99. data/lib/active_support/core_ext/object.rb +14 -13
  100. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  101. data/lib/active_support/core_ext/object/blank.rb +29 -4
  102. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  103. data/lib/active_support/core_ext/object/deep_dup.rb +13 -4
  104. data/lib/active_support/core_ext/object/duplicable.rb +98 -45
  105. data/lib/active_support/core_ext/object/inclusion.rb +5 -3
  106. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  107. data/lib/active_support/core_ext/object/json.rb +49 -19
  108. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  109. data/lib/active_support/core_ext/object/to_query.rb +6 -4
  110. data/lib/active_support/core_ext/object/try.rb +70 -22
  111. data/lib/active_support/core_ext/object/with_options.rb +16 -3
  112. data/lib/active_support/core_ext/range.rb +7 -4
  113. data/lib/active_support/core_ext/range/conversions.rb +27 -7
  114. data/lib/active_support/core_ext/range/each.rb +19 -17
  115. data/lib/active_support/core_ext/range/include_range.rb +21 -19
  116. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  117. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  118. data/lib/active_support/core_ext/regexp.rb +6 -0
  119. data/lib/active_support/core_ext/securerandom.rb +25 -0
  120. data/lib/active_support/core_ext/string.rb +15 -13
  121. data/lib/active_support/core_ext/string/access.rb +9 -7
  122. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  123. data/lib/active_support/core_ext/string/conversions.rb +8 -5
  124. data/lib/active_support/core_ext/string/exclude.rb +2 -0
  125. data/lib/active_support/core_ext/string/filters.rb +10 -8
  126. data/lib/active_support/core_ext/string/indent.rb +6 -4
  127. data/lib/active_support/core_ext/string/inflections.rb +61 -24
  128. data/lib/active_support/core_ext/string/inquiry.rb +3 -1
  129. data/lib/active_support/core_ext/string/multibyte.rb +15 -7
  130. data/lib/active_support/core_ext/string/output_safety.rb +35 -35
  131. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
  132. data/lib/active_support/core_ext/string/strip.rb +4 -5
  133. data/lib/active_support/core_ext/string/zones.rb +4 -2
  134. data/lib/active_support/core_ext/time.rb +7 -5
  135. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  136. data/lib/active_support/core_ext/time/calculations.rb +101 -51
  137. data/lib/active_support/core_ext/time/compatibility.rb +16 -0
  138. data/lib/active_support/core_ext/time/conversions.rb +20 -13
  139. data/lib/active_support/core_ext/time/zones.rb +41 -7
  140. data/lib/active_support/core_ext/uri.rb +5 -4
  141. data/lib/active_support/current_attributes.rb +195 -0
  142. data/lib/active_support/dependencies.rb +143 -160
  143. data/lib/active_support/dependencies/autoload.rb +2 -0
  144. data/lib/active_support/dependencies/interlock.rb +57 -0
  145. data/lib/active_support/deprecation.rb +12 -9
  146. data/lib/active_support/deprecation/behaviors.rb +41 -12
  147. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  148. data/lib/active_support/deprecation/instance_delegator.rb +17 -2
  149. data/lib/active_support/deprecation/method_wrappers.rb +54 -21
  150. data/lib/active_support/deprecation/proxy_wrappers.rb +56 -28
  151. data/lib/active_support/deprecation/reporting.rb +32 -12
  152. data/lib/active_support/descendants_tracker.rb +2 -0
  153. data/lib/active_support/digest.rb +20 -0
  154. data/lib/active_support/duration.rb +326 -30
  155. data/lib/active_support/duration/iso8601_parser.rb +125 -0
  156. data/lib/active_support/duration/iso8601_serializer.rb +55 -0
  157. data/lib/active_support/encrypted_configuration.rb +49 -0
  158. data/lib/active_support/encrypted_file.rb +99 -0
  159. data/lib/active_support/evented_file_update_checker.rb +205 -0
  160. data/lib/active_support/execution_wrapper.rb +128 -0
  161. data/lib/active_support/executor.rb +8 -0
  162. data/lib/active_support/file_update_checker.rb +63 -37
  163. data/lib/active_support/gem_version.rb +4 -2
  164. data/lib/active_support/gzip.rb +7 -5
  165. data/lib/active_support/hash_with_indifferent_access.rb +130 -30
  166. data/lib/active_support/i18n.rb +8 -6
  167. data/lib/active_support/i18n_railtie.rb +34 -14
  168. data/lib/active_support/inflections.rb +13 -11
  169. data/lib/active_support/inflector.rb +7 -5
  170. data/lib/active_support/inflector/inflections.rb +61 -12
  171. data/lib/active_support/inflector/methods.rb +161 -136
  172. data/lib/active_support/inflector/transliterate.rb +48 -27
  173. data/lib/active_support/json.rb +4 -2
  174. data/lib/active_support/json/decoding.rb +16 -13
  175. data/lib/active_support/json/encoding.rb +15 -57
  176. data/lib/active_support/key_generator.rb +25 -25
  177. data/lib/active_support/lazy_load_hooks.rb +50 -20
  178. data/lib/active_support/locale/en.yml +2 -0
  179. data/lib/active_support/log_subscriber.rb +13 -10
  180. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  181. data/lib/active_support/logger.rb +54 -3
  182. data/lib/active_support/logger_silence.rb +12 -7
  183. data/lib/active_support/logger_thread_safe_level.rb +33 -0
  184. data/lib/active_support/message_encryptor.rb +173 -51
  185. data/lib/active_support/message_verifier.rb +150 -17
  186. data/lib/active_support/messages/metadata.rb +71 -0
  187. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  188. data/lib/active_support/messages/rotator.rb +56 -0
  189. data/lib/active_support/multibyte.rb +4 -2
  190. data/lib/active_support/multibyte/chars.rb +37 -24
  191. data/lib/active_support/multibyte/unicode.rb +100 -96
  192. data/lib/active_support/notifications.rb +11 -7
  193. data/lib/active_support/notifications/fanout.rb +10 -8
  194. data/lib/active_support/notifications/instrumenter.rb +27 -7
  195. data/lib/active_support/number_helper.rb +94 -68
  196. data/lib/active_support/number_helper/number_converter.rb +13 -11
  197. data/lib/active_support/number_helper/number_to_currency_converter.rb +9 -9
  198. data/lib/active_support/number_helper/number_to_delimited_converter.rb +9 -3
  199. data/lib/active_support/number_helper/number_to_human_converter.rb +11 -9
  200. data/lib/active_support/number_helper/number_to_human_size_converter.rb +9 -8
  201. data/lib/active_support/number_helper/number_to_percentage_converter.rb +3 -1
  202. data/lib/active_support/number_helper/number_to_phone_converter.rb +13 -4
  203. data/lib/active_support/number_helper/number_to_rounded_converter.rb +23 -56
  204. data/lib/active_support/number_helper/rounding_helper.rb +66 -0
  205. data/lib/active_support/option_merger.rb +3 -1
  206. data/lib/active_support/ordered_hash.rb +6 -4
  207. data/lib/active_support/ordered_options.rb +22 -4
  208. data/lib/active_support/per_thread_registry.rb +13 -6
  209. data/lib/active_support/proxy_object.rb +2 -0
  210. data/lib/active_support/rails.rb +16 -8
  211. data/lib/active_support/railtie.rb +43 -9
  212. data/lib/active_support/reloader.rb +131 -0
  213. data/lib/active_support/rescuable.rb +108 -53
  214. data/lib/active_support/security_utils.rb +17 -6
  215. data/lib/active_support/string_inquirer.rb +11 -3
  216. data/lib/active_support/subscriber.rb +15 -14
  217. data/lib/active_support/tagged_logging.rb +14 -11
  218. data/lib/active_support/test_case.rb +18 -46
  219. data/lib/active_support/testing/assertions.rb +137 -20
  220. data/lib/active_support/testing/autorun.rb +4 -2
  221. data/lib/active_support/testing/constant_lookup.rb +2 -1
  222. data/lib/active_support/testing/declarative.rb +3 -1
  223. data/lib/active_support/testing/deprecation.rb +14 -10
  224. data/lib/active_support/testing/file_fixtures.rb +36 -0
  225. data/lib/active_support/testing/isolation.rb +34 -25
  226. data/lib/active_support/testing/method_call_assertions.rb +43 -0
  227. data/lib/active_support/testing/setup_and_teardown.rb +12 -3
  228. data/lib/active_support/testing/stream.rb +44 -0
  229. data/lib/active_support/testing/tagged_logging.rb +3 -1
  230. data/lib/active_support/testing/time_helpers.rb +96 -27
  231. data/lib/active_support/time.rb +14 -12
  232. data/lib/active_support/time_with_zone.rb +195 -53
  233. data/lib/active_support/values/time_zone.rb +200 -61
  234. data/lib/active_support/values/unicode_tables.dat +0 -0
  235. data/lib/active_support/version.rb +3 -1
  236. data/lib/active_support/xml_mini.rb +69 -51
  237. data/lib/active_support/xml_mini/jdom.rb +116 -113
  238. data/lib/active_support/xml_mini/libxml.rb +17 -16
  239. data/lib/active_support/xml_mini/libxmlsax.rb +16 -18
  240. data/lib/active_support/xml_mini/nokogiri.rb +15 -15
  241. data/lib/active_support/xml_mini/nokogirisax.rb +15 -16
  242. data/lib/active_support/xml_mini/rexml.rb +17 -16
  243. metadata +55 -43
  244. data/lib/active_support/concurrency/latch.rb +0 -27
  245. data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -14
  246. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
  247. data/lib/active_support/core_ext/date_time/zones.rb +0 -6
  248. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  249. data/lib/active_support/core_ext/module/method_transplanting.rb +0 -11
  250. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  251. data/lib/active_support/core_ext/object/itself.rb +0 -15
  252. data/lib/active_support/core_ext/struct.rb +0 -6
  253. data/lib/active_support/core_ext/thread.rb +0 -86
  254. data/lib/active_support/core_ext/time/marshal.rb +0 -30
@@ -1 +1,3 @@
1
- require 'active_support/core_ext/object/to_query'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/object/to_query"
@@ -1,4 +1,6 @@
1
- require 'cgi'
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi"
2
4
 
3
5
  class Object
4
6
  # Alias of <tt>to_s</tt>.
@@ -38,7 +40,7 @@ class Array
38
40
  # Calls <tt>to_param</tt> on all its elements and joins the result with
39
41
  # slashes. This is used by <tt>url_for</tt> in Action Pack.
40
42
  def to_param
41
- collect { |e| e.to_param }.join '/'
43
+ collect(&:to_param).join "/"
42
44
  end
43
45
 
44
46
  # Converts an array into a string suitable for use as a URL query string,
@@ -51,7 +53,7 @@ class Array
51
53
  if empty?
52
54
  nil.to_query(prefix)
53
55
  else
54
- collect { |value| value.to_query(prefix) }.join '&'
56
+ collect { |value| value.to_query(prefix) }.join "&"
55
57
  end
56
58
  end
57
59
  end
@@ -77,7 +79,7 @@ class Hash
77
79
  unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?
78
80
  value.to_query(namespace ? "#{namespace}[#{key}]" : key)
79
81
  end
80
- end.compact.sort! * '&'
82
+ end.compact.sort! * "&"
81
83
  end
82
84
 
83
85
  alias_method :to_param, :to_query
@@ -1,4 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delegate"
4
+
5
+ module ActiveSupport
6
+ module Tryable #:nodoc:
7
+ def try(*a, &b)
8
+ try!(*a, &b) if a.empty? || respond_to?(a.first)
9
+ end
10
+
11
+ def try!(*a, &b)
12
+ if a.empty? && block_given?
13
+ if b.arity == 0
14
+ instance_eval(&b)
15
+ else
16
+ yield self
17
+ end
18
+ else
19
+ public_send(*a, &b)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
1
25
  class Object
26
+ include ActiveSupport::Tryable
27
+
28
+ ##
29
+ # :method: try
30
+ #
31
+ # :call-seq:
32
+ # try(*a, &b)
33
+ #
2
34
  # Invokes the public method whose name goes as first argument just like
3
35
  # +public_send+ does, except that if the receiver does not respond to it the
4
36
  # call returns +nil+ rather than raising an exception.
@@ -21,11 +53,11 @@ class Object
21
53
  #
22
54
  # +try+ will also return +nil+ if the receiver does not respond to the method:
23
55
  #
24
- # @person.try(:non_existing_method) #=> nil
56
+ # @person.try(:non_existing_method) # => nil
25
57
  #
26
58
  # instead of
27
59
  #
28
- # @person.non_existing_method if @person.respond_to?(:non_existing_method) #=> nil
60
+ # @person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil
29
61
  #
30
62
  # +try+ returns +nil+ when called on +nil+ regardless of whether it responds
31
63
  # to the method:
@@ -56,27 +88,40 @@ class Object
56
88
  #
57
89
  # Please also note that +try+ is defined on +Object+. Therefore, it won't work
58
90
  # with instances of classes that do not have +Object+ among their ancestors,
59
- # like direct subclasses of +BasicObject+. For example, using +try+ with
60
- # +SimpleDelegator+ will delegate +try+ to the target instead of calling it on
61
- # the delegator itself.
62
- def try(*a, &b)
63
- try!(*a, &b) if a.empty? || respond_to?(a.first)
64
- end
91
+ # like direct subclasses of +BasicObject+.
65
92
 
66
- # Same as #try, but will raise a NoMethodError exception if the receiver is not +nil+ and
67
- # does not implement the tried method.
68
-
69
- def try!(*a, &b)
70
- if a.empty? && block_given?
71
- if b.arity.zero?
72
- instance_eval(&b)
73
- else
74
- yield self
75
- end
76
- else
77
- public_send(*a, &b)
78
- end
79
- end
93
+ ##
94
+ # :method: try!
95
+ #
96
+ # :call-seq:
97
+ # try!(*a, &b)
98
+ #
99
+ # Same as #try, but raises a +NoMethodError+ exception if the receiver is
100
+ # not +nil+ and does not implement the tried method.
101
+ #
102
+ # "a".try!(:upcase) # => "A"
103
+ # nil.try!(:upcase) # => nil
104
+ # 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Integer
105
+ end
106
+
107
+ class Delegator
108
+ include ActiveSupport::Tryable
109
+
110
+ ##
111
+ # :method: try
112
+ #
113
+ # :call-seq:
114
+ # try(a*, &b)
115
+ #
116
+ # See Object#try
117
+
118
+ ##
119
+ # :method: try!
120
+ #
121
+ # :call-seq:
122
+ # try!(a*, &b)
123
+ #
124
+ # See Object#try!
80
125
  end
81
126
 
82
127
  class NilClass
@@ -94,6 +139,9 @@ class NilClass
94
139
  nil
95
140
  end
96
141
 
142
+ # Calling +try!+ on +nil+ always returns +nil+.
143
+ #
144
+ # nil.try!(:name) # => nil
97
145
  def try!(*args)
98
146
  nil
99
147
  end
@@ -1,4 +1,6 @@
1
- require 'active_support/option_merger'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/option_merger"
2
4
 
3
5
  class Object
4
6
  # An elegant way to factor duplication out of options passed to a series of
@@ -7,7 +9,7 @@ class Object
7
9
  # provided. Each method called on the block variable must take an options
8
10
  # hash as its final argument.
9
11
  #
10
- # Without <tt>with_options></tt>, this code contains duplication:
12
+ # Without <tt>with_options</tt>, this code contains duplication:
11
13
  #
12
14
  # class Account < ActiveRecord::Base
13
15
  # has_many :customers, dependent: :destroy
@@ -60,7 +62,18 @@ class Object
60
62
  #
61
63
  # validates :content, length: { minimum: 50 }, if: -> { content.present? }
62
64
  #
63
- # Hence the inherited default for `if` key is ignored.
65
+ # Hence the inherited default for +if+ key is ignored.
66
+ #
67
+ # NOTE: You cannot call class methods implicitly inside of with_options.
68
+ # You can access these methods using the class name instead:
69
+ #
70
+ # class Phone < ActiveRecord::Base
71
+ # enum phone_number_type: [home: 0, office: 1, mobile: 2]
72
+ #
73
+ # with_options presence: true do
74
+ # validates :phone_number_type, inclusion: { in: Phone.phone_number_types.keys }
75
+ # end
76
+ # end
64
77
  #
65
78
  def with_options(options, &block)
66
79
  option_merger = ActiveSupport::OptionMerger.new(self, options)
@@ -1,4 +1,7 @@
1
- require 'active_support/core_ext/range/conversions'
2
- require 'active_support/core_ext/range/include_range'
3
- require 'active_support/core_ext/range/overlaps'
4
- require 'active_support/core_ext/range/each'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/range/conversions"
4
+ require "active_support/core_ext/range/include_range"
5
+ require "active_support/core_ext/range/include_time_with_zone"
6
+ require "active_support/core_ext/range/overlaps"
7
+ require "active_support/core_ext/range/each"
@@ -1,19 +1,39 @@
1
- class Range
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport::RangeWithFormat
2
4
  RANGE_FORMATS = {
3
- :db => Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" }
5
+ db: -> (start, stop) do
6
+ case start
7
+ when String then "BETWEEN '#{start}' AND '#{stop}'"
8
+ else
9
+ "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'"
10
+ end
11
+ end
4
12
  }
5
13
 
6
- # Gives a human readable format of the range.
14
+ # Convert range to a formatted string. See RANGE_FORMATS for predefined formats.
15
+ #
16
+ # range = (1..100) # => 1..100
7
17
  #
8
- # (1..100).to_formatted_s # => "1..100"
9
- def to_formatted_s(format = :default)
18
+ # range.to_s # => "1..100"
19
+ # range.to_s(:db) # => "BETWEEN '1' AND '100'"
20
+ #
21
+ # == Adding your own range formats to to_s
22
+ # You can add your own formats to the Range::RANGE_FORMATS hash.
23
+ # Use the format name as the hash key and a Proc instance.
24
+ #
25
+ # # config/initializers/range_formats.rb
26
+ # Range::RANGE_FORMATS[:short] = ->(start, stop) { "Between #{start.to_s(:db)} and #{stop.to_s(:db)}" }
27
+ def to_s(format = :default)
10
28
  if formatter = RANGE_FORMATS[format]
11
29
  formatter.call(first, last)
12
30
  else
13
- to_default_s
31
+ super()
14
32
  end
15
33
  end
16
34
 
17
35
  alias_method :to_default_s, :to_s
18
- alias_method :to_s, :to_formatted_s
36
+ alias_method :to_formatted_s, :to_s
19
37
  end
38
+
39
+ Range.prepend(ActiveSupport::RangeWithFormat)
@@ -1,23 +1,25 @@
1
- require 'active_support/core_ext/module/aliasing'
1
+ # frozen_string_literal: true
2
2
 
3
- class Range #:nodoc:
3
+ require "active_support/time_with_zone"
4
4
 
5
- def each_with_time_with_zone(&block)
6
- ensure_iteration_allowed
7
- each_without_time_with_zone(&block)
8
- end
9
- alias_method_chain :each, :time_with_zone
10
-
11
- def step_with_time_with_zone(n = 1, &block)
12
- ensure_iteration_allowed
13
- step_without_time_with_zone(n, &block)
14
- end
15
- alias_method_chain :step, :time_with_zone
5
+ module ActiveSupport
6
+ module EachTimeWithZone #:nodoc:
7
+ def each(&block)
8
+ ensure_iteration_allowed
9
+ super
10
+ end
16
11
 
17
- private
18
- def ensure_iteration_allowed
19
- if first.is_a?(Time)
20
- raise TypeError, "can't iterate from #{first.class}"
12
+ def step(n = 1, &block)
13
+ ensure_iteration_allowed
14
+ super
21
15
  end
16
+
17
+ private
18
+
19
+ def ensure_iteration_allowed
20
+ raise TypeError, "can't iterate from #{first.class}" if first.is_a?(TimeWithZone)
21
+ end
22
22
  end
23
23
  end
24
+
25
+ Range.prepend(ActiveSupport::EachTimeWithZone)
@@ -1,23 +1,25 @@
1
- require 'active_support/core_ext/module/aliasing'
1
+ # frozen_string_literal: true
2
2
 
3
- class Range
4
- # Extends the default Range#include? to support range comparisons.
5
- # (1..5).include?(1..5) # => true
6
- # (1..5).include?(2..3) # => true
7
- # (1..5).include?(2..6) # => false
8
- #
9
- # The native Range#include? behavior is untouched.
10
- # ('a'..'f').include?('c') # => true
11
- # (5..9).include?(11) # => false
12
- def include_with_range?(value)
13
- if value.is_a?(::Range)
14
- # 1...10 includes 1..9 but it does not include 1..10.
15
- operator = exclude_end? && !value.exclude_end? ? :< : :<=
16
- include_without_range?(value.first) && value.last.send(operator, last)
17
- else
18
- include_without_range?(value)
3
+ module ActiveSupport
4
+ module IncludeWithRange #:nodoc:
5
+ # Extends the default Range#include? to support range comparisons.
6
+ # (1..5).include?(1..5) # => true
7
+ # (1..5).include?(2..3) # => true
8
+ # (1..5).include?(2..6) # => false
9
+ #
10
+ # The native Range#include? behavior is untouched.
11
+ # ('a'..'f').include?('c') # => true
12
+ # (5..9).include?(11) # => false
13
+ def include?(value)
14
+ if value.is_a?(::Range)
15
+ # 1...10 includes 1..9 but it does not include 1..10.
16
+ operator = exclude_end? && !value.exclude_end? ? :< : :<=
17
+ super(value.first) && value.last.send(operator, last)
18
+ else
19
+ super
20
+ end
19
21
  end
20
22
  end
21
-
22
- alias_method_chain :include?, :range
23
23
  end
24
+
25
+ Range.prepend(ActiveSupport::IncludeWithRange)
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/time_with_zone"
4
+
5
+ module ActiveSupport
6
+ module IncludeTimeWithZone #:nodoc:
7
+ # Extends the default Range#include? to support ActiveSupport::TimeWithZone.
8
+ #
9
+ # (1.hour.ago..1.hour.from_now).include?(Time.current) # => true
10
+ #
11
+ def include?(value)
12
+ if first.is_a?(TimeWithZone)
13
+ cover?(value)
14
+ elsif last.is_a?(TimeWithZone)
15
+ cover?(value)
16
+ else
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Range.prepend(ActiveSupport::IncludeTimeWithZone)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Range
2
4
  # Compare two ranges and see if they overlap each other
3
5
  # (1..5).overlaps?(4..6) # => true
@@ -1,5 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Regexp #:nodoc:
2
4
  def multiline?
3
5
  options & MULTILINE == MULTILINE
4
6
  end
7
+
8
+ def match?(string, pos = 0)
9
+ !!match(string, pos)
10
+ end unless //.respond_to?(:match?)
5
11
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module SecureRandom
6
+ BASE58_ALPHABET = ("0".."9").to_a + ("A".."Z").to_a + ("a".."z").to_a - ["0", "O", "I", "l"]
7
+ # SecureRandom.base58 generates a random base58 string.
8
+ #
9
+ # The argument _n_ specifies the length, of the random string to be generated.
10
+ #
11
+ # If _n_ is not specified or is +nil+, 16 is assumed. It may be larger in the future.
12
+ #
13
+ # The result may contain alphanumeric characters except 0, O, I and l
14
+ #
15
+ # p SecureRandom.base58 # => "4kUgL2pdQMSCQtjE"
16
+ # p SecureRandom.base58(24) # => "77TMHrHJFvFDwodq8w7Ev2m7"
17
+ #
18
+ def self.base58(n = 16)
19
+ SecureRandom.random_bytes(n).unpack("C*").map do |byte|
20
+ idx = byte % 64
21
+ idx = SecureRandom.random_number(58) if idx >= 58
22
+ BASE58_ALPHABET[idx]
23
+ end.join
24
+ end
25
+ end
@@ -1,13 +1,15 @@
1
- require 'active_support/core_ext/string/conversions'
2
- require 'active_support/core_ext/string/filters'
3
- require 'active_support/core_ext/string/multibyte'
4
- require 'active_support/core_ext/string/starts_ends_with'
5
- require 'active_support/core_ext/string/inflections'
6
- require 'active_support/core_ext/string/access'
7
- require 'active_support/core_ext/string/behavior'
8
- require 'active_support/core_ext/string/output_safety'
9
- require 'active_support/core_ext/string/exclude'
10
- require 'active_support/core_ext/string/strip'
11
- require 'active_support/core_ext/string/inquiry'
12
- require 'active_support/core_ext/string/indent'
13
- require 'active_support/core_ext/string/zones'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/string/conversions"
4
+ require "active_support/core_ext/string/filters"
5
+ require "active_support/core_ext/string/multibyte"
6
+ require "active_support/core_ext/string/starts_ends_with"
7
+ require "active_support/core_ext/string/inflections"
8
+ require "active_support/core_ext/string/access"
9
+ require "active_support/core_ext/string/behavior"
10
+ require "active_support/core_ext/string/output_safety"
11
+ require "active_support/core_ext/string/exclude"
12
+ require "active_support/core_ext/string/strip"
13
+ require "active_support/core_ext/string/inquiry"
14
+ require "active_support/core_ext/string/indent"
15
+ require "active_support/core_ext/string/zones"