activesupport 5.0.7.1

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 (236) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1013 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +39 -0
  5. data/lib/active_support.rb +99 -0
  6. data/lib/active_support/all.rb +3 -0
  7. data/lib/active_support/array_inquirer.rb +44 -0
  8. data/lib/active_support/backtrace_cleaner.rb +103 -0
  9. data/lib/active_support/benchmarkable.rb +49 -0
  10. data/lib/active_support/builder.rb +6 -0
  11. data/lib/active_support/cache.rb +701 -0
  12. data/lib/active_support/cache/file_store.rb +204 -0
  13. data/lib/active_support/cache/mem_cache_store.rb +207 -0
  14. data/lib/active_support/cache/memory_store.rb +167 -0
  15. data/lib/active_support/cache/null_store.rb +41 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +172 -0
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
  18. data/lib/active_support/callbacks.rb +791 -0
  19. data/lib/active_support/concern.rb +142 -0
  20. data/lib/active_support/concurrency/latch.rb +26 -0
  21. data/lib/active_support/concurrency/share_lock.rb +226 -0
  22. data/lib/active_support/configurable.rb +148 -0
  23. data/lib/active_support/core_ext.rb +4 -0
  24. data/lib/active_support/core_ext/array.rb +7 -0
  25. data/lib/active_support/core_ext/array/access.rb +90 -0
  26. data/lib/active_support/core_ext/array/conversions.rb +211 -0
  27. data/lib/active_support/core_ext/array/extract_options.rb +29 -0
  28. data/lib/active_support/core_ext/array/grouping.rb +107 -0
  29. data/lib/active_support/core_ext/array/inquiry.rb +17 -0
  30. data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
  31. data/lib/active_support/core_ext/array/wrap.rb +46 -0
  32. data/lib/active_support/core_ext/benchmark.rb +14 -0
  33. data/lib/active_support/core_ext/big_decimal.rb +1 -0
  34. data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
  35. data/lib/active_support/core_ext/class.rb +2 -0
  36. data/lib/active_support/core_ext/class/attribute.rb +128 -0
  37. data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -0
  38. data/lib/active_support/core_ext/class/subclasses.rb +41 -0
  39. data/lib/active_support/core_ext/date.rb +5 -0
  40. data/lib/active_support/core_ext/date/acts_like.rb +8 -0
  41. data/lib/active_support/core_ext/date/blank.rb +12 -0
  42. data/lib/active_support/core_ext/date/calculations.rb +143 -0
  43. data/lib/active_support/core_ext/date/conversions.rb +95 -0
  44. data/lib/active_support/core_ext/date/zones.rb +6 -0
  45. data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
  46. data/lib/active_support/core_ext/date_and_time/compatibility.rb +14 -0
  47. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  48. data/lib/active_support/core_ext/date_time.rb +5 -0
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +14 -0
  50. data/lib/active_support/core_ext/date_time/blank.rb +12 -0
  51. data/lib/active_support/core_ext/date_time/calculations.rb +199 -0
  52. data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
  53. data/lib/active_support/core_ext/date_time/conversions.rb +105 -0
  54. data/lib/active_support/core_ext/digest/uuid.rb +51 -0
  55. data/lib/active_support/core_ext/enumerable.rb +146 -0
  56. data/lib/active_support/core_ext/file.rb +1 -0
  57. data/lib/active_support/core_ext/file/atomic.rb +68 -0
  58. data/lib/active_support/core_ext/hash.rb +9 -0
  59. data/lib/active_support/core_ext/hash/compact.rb +24 -0
  60. data/lib/active_support/core_ext/hash/conversions.rb +262 -0
  61. data/lib/active_support/core_ext/hash/deep_merge.rb +38 -0
  62. data/lib/active_support/core_ext/hash/except.rb +22 -0
  63. data/lib/active_support/core_ext/hash/indifferent_access.rb +23 -0
  64. data/lib/active_support/core_ext/hash/keys.rb +170 -0
  65. data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -0
  66. data/lib/active_support/core_ext/hash/slice.rb +48 -0
  67. data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
  68. data/lib/active_support/core_ext/integer.rb +3 -0
  69. data/lib/active_support/core_ext/integer/inflections.rb +29 -0
  70. data/lib/active_support/core_ext/integer/multiple.rb +10 -0
  71. data/lib/active_support/core_ext/integer/time.rb +29 -0
  72. data/lib/active_support/core_ext/kernel.rb +4 -0
  73. data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
  74. data/lib/active_support/core_ext/kernel/concern.rb +12 -0
  75. data/lib/active_support/core_ext/kernel/debugger.rb +3 -0
  76. data/lib/active_support/core_ext/kernel/reporting.rb +43 -0
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +6 -0
  78. data/lib/active_support/core_ext/load_error.rb +31 -0
  79. data/lib/active_support/core_ext/marshal.rb +22 -0
  80. data/lib/active_support/core_ext/module.rb +12 -0
  81. data/lib/active_support/core_ext/module/aliasing.rb +74 -0
  82. data/lib/active_support/core_ext/module/anonymous.rb +28 -0
  83. data/lib/active_support/core_ext/module/attr_internal.rb +36 -0
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +135 -0
  87. data/lib/active_support/core_ext/module/delegation.rb +216 -0
  88. data/lib/active_support/core_ext/module/deprecation.rb +23 -0
  89. data/lib/active_support/core_ext/module/introspection.rb +68 -0
  90. data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
  91. data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
  92. data/lib/active_support/core_ext/module/reachable.rb +8 -0
  93. data/lib/active_support/core_ext/module/remove_method.rb +35 -0
  94. data/lib/active_support/core_ext/name_error.rb +31 -0
  95. data/lib/active_support/core_ext/numeric.rb +4 -0
  96. data/lib/active_support/core_ext/numeric/bytes.rb +64 -0
  97. data/lib/active_support/core_ext/numeric/conversions.rb +144 -0
  98. data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
  99. data/lib/active_support/core_ext/numeric/time.rb +74 -0
  100. data/lib/active_support/core_ext/object.rb +14 -0
  101. data/lib/active_support/core_ext/object/acts_like.rb +10 -0
  102. data/lib/active_support/core_ext/object/blank.rb +143 -0
  103. data/lib/active_support/core_ext/object/conversions.rb +4 -0
  104. data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
  105. data/lib/active_support/core_ext/object/duplicable.rb +124 -0
  106. data/lib/active_support/core_ext/object/inclusion.rb +27 -0
  107. data/lib/active_support/core_ext/object/instance_variables.rb +28 -0
  108. data/lib/active_support/core_ext/object/json.rb +205 -0
  109. data/lib/active_support/core_ext/object/to_param.rb +1 -0
  110. data/lib/active_support/core_ext/object/to_query.rb +84 -0
  111. data/lib/active_support/core_ext/object/try.rb +146 -0
  112. data/lib/active_support/core_ext/object/with_options.rb +69 -0
  113. data/lib/active_support/core_ext/range.rb +4 -0
  114. data/lib/active_support/core_ext/range/conversions.rb +31 -0
  115. data/lib/active_support/core_ext/range/each.rb +21 -0
  116. data/lib/active_support/core_ext/range/include_range.rb +23 -0
  117. data/lib/active_support/core_ext/range/overlaps.rb +8 -0
  118. data/lib/active_support/core_ext/regexp.rb +5 -0
  119. data/lib/active_support/core_ext/securerandom.rb +23 -0
  120. data/lib/active_support/core_ext/string.rb +13 -0
  121. data/lib/active_support/core_ext/string/access.rb +104 -0
  122. data/lib/active_support/core_ext/string/behavior.rb +6 -0
  123. data/lib/active_support/core_ext/string/conversions.rb +57 -0
  124. data/lib/active_support/core_ext/string/exclude.rb +11 -0
  125. data/lib/active_support/core_ext/string/filters.rb +102 -0
  126. data/lib/active_support/core_ext/string/indent.rb +43 -0
  127. data/lib/active_support/core_ext/string/inflections.rb +244 -0
  128. data/lib/active_support/core_ext/string/inquiry.rb +13 -0
  129. data/lib/active_support/core_ext/string/multibyte.rb +53 -0
  130. data/lib/active_support/core_ext/string/output_safety.rb +260 -0
  131. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
  132. data/lib/active_support/core_ext/string/strip.rb +23 -0
  133. data/lib/active_support/core_ext/string/zones.rb +14 -0
  134. data/lib/active_support/core_ext/struct.rb +3 -0
  135. data/lib/active_support/core_ext/time.rb +5 -0
  136. data/lib/active_support/core_ext/time/acts_like.rb +8 -0
  137. data/lib/active_support/core_ext/time/calculations.rb +290 -0
  138. data/lib/active_support/core_ext/time/compatibility.rb +14 -0
  139. data/lib/active_support/core_ext/time/conversions.rb +67 -0
  140. data/lib/active_support/core_ext/time/marshal.rb +3 -0
  141. data/lib/active_support/core_ext/time/zones.rb +111 -0
  142. data/lib/active_support/core_ext/uri.rb +24 -0
  143. data/lib/active_support/dependencies.rb +755 -0
  144. data/lib/active_support/dependencies/autoload.rb +77 -0
  145. data/lib/active_support/dependencies/interlock.rb +55 -0
  146. data/lib/active_support/deprecation.rb +43 -0
  147. data/lib/active_support/deprecation/behaviors.rb +90 -0
  148. data/lib/active_support/deprecation/instance_delegator.rb +37 -0
  149. data/lib/active_support/deprecation/method_wrappers.rb +70 -0
  150. data/lib/active_support/deprecation/proxy_wrappers.rb +149 -0
  151. data/lib/active_support/deprecation/reporting.rb +112 -0
  152. data/lib/active_support/descendants_tracker.rb +60 -0
  153. data/lib/active_support/duration.rb +235 -0
  154. data/lib/active_support/duration/iso8601_parser.rb +122 -0
  155. data/lib/active_support/duration/iso8601_serializer.rb +51 -0
  156. data/lib/active_support/evented_file_update_checker.rb +199 -0
  157. data/lib/active_support/execution_wrapper.rb +126 -0
  158. data/lib/active_support/executor.rb +6 -0
  159. data/lib/active_support/file_update_checker.rb +157 -0
  160. data/lib/active_support/gem_version.rb +15 -0
  161. data/lib/active_support/gzip.rb +36 -0
  162. data/lib/active_support/hash_with_indifferent_access.rb +329 -0
  163. data/lib/active_support/i18n.rb +13 -0
  164. data/lib/active_support/i18n_railtie.rb +115 -0
  165. data/lib/active_support/inflections.rb +70 -0
  166. data/lib/active_support/inflector.rb +7 -0
  167. data/lib/active_support/inflector/inflections.rb +242 -0
  168. data/lib/active_support/inflector/methods.rb +390 -0
  169. data/lib/active_support/inflector/transliterate.rb +112 -0
  170. data/lib/active_support/json.rb +2 -0
  171. data/lib/active_support/json/decoding.rb +74 -0
  172. data/lib/active_support/json/encoding.rb +127 -0
  173. data/lib/active_support/key_generator.rb +71 -0
  174. data/lib/active_support/lazy_load_hooks.rb +76 -0
  175. data/lib/active_support/locale/en.yml +135 -0
  176. data/lib/active_support/log_subscriber.rb +109 -0
  177. data/lib/active_support/log_subscriber/test_helper.rb +104 -0
  178. data/lib/active_support/logger.rb +106 -0
  179. data/lib/active_support/logger_silence.rb +28 -0
  180. data/lib/active_support/logger_thread_safe_level.rb +31 -0
  181. data/lib/active_support/message_encryptor.rb +114 -0
  182. data/lib/active_support/message_verifier.rb +134 -0
  183. data/lib/active_support/multibyte.rb +21 -0
  184. data/lib/active_support/multibyte/chars.rb +231 -0
  185. data/lib/active_support/multibyte/unicode.rb +413 -0
  186. data/lib/active_support/notifications.rb +212 -0
  187. data/lib/active_support/notifications/fanout.rb +157 -0
  188. data/lib/active_support/notifications/instrumenter.rb +91 -0
  189. data/lib/active_support/number_helper.rb +368 -0
  190. data/lib/active_support/number_helper/number_converter.rb +182 -0
  191. data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
  192. data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
  193. data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
  194. data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
  195. data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
  196. data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
  197. data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
  198. data/lib/active_support/option_merger.rb +25 -0
  199. data/lib/active_support/ordered_hash.rb +48 -0
  200. data/lib/active_support/ordered_options.rb +81 -0
  201. data/lib/active_support/per_thread_registry.rb +58 -0
  202. data/lib/active_support/proxy_object.rb +13 -0
  203. data/lib/active_support/rails.rb +27 -0
  204. data/lib/active_support/railtie.rb +51 -0
  205. data/lib/active_support/reloader.rb +129 -0
  206. data/lib/active_support/rescuable.rb +173 -0
  207. data/lib/active_support/security_utils.rb +27 -0
  208. data/lib/active_support/string_inquirer.rb +26 -0
  209. data/lib/active_support/subscriber.rb +120 -0
  210. data/lib/active_support/tagged_logging.rb +77 -0
  211. data/lib/active_support/test_case.rb +88 -0
  212. data/lib/active_support/testing/assertions.rb +99 -0
  213. data/lib/active_support/testing/autorun.rb +5 -0
  214. data/lib/active_support/testing/constant_lookup.rb +50 -0
  215. data/lib/active_support/testing/declarative.rb +26 -0
  216. data/lib/active_support/testing/deprecation.rb +36 -0
  217. data/lib/active_support/testing/file_fixtures.rb +34 -0
  218. data/lib/active_support/testing/isolation.rb +115 -0
  219. data/lib/active_support/testing/method_call_assertions.rb +41 -0
  220. data/lib/active_support/testing/setup_and_teardown.rb +50 -0
  221. data/lib/active_support/testing/stream.rb +42 -0
  222. data/lib/active_support/testing/tagged_logging.rb +25 -0
  223. data/lib/active_support/testing/time_helpers.rb +136 -0
  224. data/lib/active_support/time.rb +18 -0
  225. data/lib/active_support/time_with_zone.rb +511 -0
  226. data/lib/active_support/values/time_zone.rb +484 -0
  227. data/lib/active_support/values/unicode_tables.dat +0 -0
  228. data/lib/active_support/version.rb +8 -0
  229. data/lib/active_support/xml_mini.rb +209 -0
  230. data/lib/active_support/xml_mini/jdom.rb +181 -0
  231. data/lib/active_support/xml_mini/libxml.rb +77 -0
  232. data/lib/active_support/xml_mini/libxmlsax.rb +82 -0
  233. data/lib/active_support/xml_mini/nokogiri.rb +81 -0
  234. data/lib/active_support/xml_mini/nokogirisax.rb +85 -0
  235. data/lib/active_support/xml_mini/rexml.rb +128 -0
  236. metadata +350 -0
@@ -0,0 +1,92 @@
1
+ module ActiveSupport
2
+ module NumberHelper
3
+ class NumberToRoundedConverter < NumberConverter # :nodoc:
4
+ self.namespace = :precision
5
+ self.validate_float = true
6
+
7
+ def convert
8
+ precision = options.delete :precision
9
+
10
+ if precision
11
+ case number
12
+ when Float, String
13
+ @number = BigDecimal(number.to_s)
14
+ when Rational
15
+ @number = BigDecimal(number, digit_count(number.to_i) + precision)
16
+ else
17
+ @number = number.to_d
18
+ end
19
+
20
+ if options.delete(:significant) && precision > 0
21
+ digits, rounded_number = digits_and_rounded_number(precision)
22
+ precision -= digits
23
+ precision = 0 if precision < 0 # don't let it be negative
24
+ else
25
+ rounded_number = number.round(precision)
26
+ rounded_number = rounded_number.to_i if precision == 0 && rounded_number.finite?
27
+ rounded_number = rounded_number.abs if rounded_number.zero? # prevent showing negative zeros
28
+ end
29
+
30
+ formatted_string =
31
+ if BigDecimal === rounded_number && rounded_number.finite?
32
+ s = rounded_number.to_s('F')
33
+ s << '0'.freeze * precision
34
+ a, b = s.split('.'.freeze, 2)
35
+ a << '.'.freeze
36
+ a << b[0, precision]
37
+ else
38
+ "%00.#{precision}f" % rounded_number
39
+ end
40
+ else
41
+ formatted_string = number
42
+ end
43
+
44
+ delimited_number = NumberToDelimitedConverter.convert(formatted_string, options)
45
+ format_number(delimited_number)
46
+ end
47
+
48
+ private
49
+
50
+ def digits_and_rounded_number(precision)
51
+ if zero?
52
+ [1, 0]
53
+ else
54
+ digits = digit_count(number)
55
+ multiplier = 10 ** (digits - precision)
56
+ rounded_number = calculate_rounded_number(multiplier)
57
+ digits = digit_count(rounded_number) # After rounding, the number of digits may have changed
58
+ [digits, rounded_number]
59
+ end
60
+ end
61
+
62
+ def calculate_rounded_number(multiplier)
63
+ (number / BigDecimal.new(multiplier.to_f.to_s)).round * multiplier
64
+ end
65
+
66
+ def digit_count(number)
67
+ number.zero? ? 1 : (Math.log10(absolute_number(number)) + 1).floor
68
+ end
69
+
70
+ def strip_insignificant_zeros
71
+ options[:strip_insignificant_zeros]
72
+ end
73
+
74
+ def format_number(number)
75
+ if strip_insignificant_zeros
76
+ escaped_separator = Regexp.escape(options[:separator])
77
+ number.sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
78
+ else
79
+ number
80
+ end
81
+ end
82
+
83
+ def absolute_number(number)
84
+ number.respond_to?(:abs) ? number.abs : number.to_d.abs
85
+ end
86
+
87
+ def zero?
88
+ number.respond_to?(:zero?) ? number.zero? : number.to_d.zero?
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,25 @@
1
+ require 'active_support/core_ext/hash/deep_merge'
2
+
3
+ module ActiveSupport
4
+ class OptionMerger #:nodoc:
5
+ instance_methods.each do |method|
6
+ undef_method(method) if method !~ /^(__|instance_eval|class|object_id)/
7
+ end
8
+
9
+ def initialize(context, options)
10
+ @context, @options = context, options
11
+ end
12
+
13
+ private
14
+ def method_missing(method, *arguments, &block)
15
+ if arguments.first.is_a?(Proc)
16
+ proc = arguments.pop
17
+ arguments << lambda { |*args| @options.deep_merge(proc.call(*args)) }
18
+ else
19
+ arguments << (arguments.last.respond_to?(:to_hash) ? @options.deep_merge(arguments.pop) : @options.dup)
20
+ end
21
+
22
+ @context.__send__(method, *arguments, &block)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,48 @@
1
+ require 'yaml'
2
+
3
+ YAML.add_builtin_type("omap") do |type, val|
4
+ ActiveSupport::OrderedHash[val.map{ |v| v.to_a.first }]
5
+ end
6
+
7
+ module ActiveSupport
8
+ # DEPRECATED: <tt>ActiveSupport::OrderedHash</tt> implements a hash that preserves
9
+ # insertion order.
10
+ #
11
+ # oh = ActiveSupport::OrderedHash.new
12
+ # oh[:a] = 1
13
+ # oh[:b] = 2
14
+ # oh.keys # => [:a, :b], this order is guaranteed
15
+ #
16
+ # Also, maps the +omap+ feature for YAML files
17
+ # (See http://yaml.org/type/omap.html) to support ordered items
18
+ # when loading from yaml.
19
+ #
20
+ # <tt>ActiveSupport::OrderedHash</tt> is namespaced to prevent conflicts
21
+ # with other implementations.
22
+ class OrderedHash < ::Hash
23
+ def to_yaml_type
24
+ "!tag:yaml.org,2002:omap"
25
+ end
26
+
27
+ def encode_with(coder)
28
+ coder.represent_seq '!omap', map { |k,v| { k => v } }
29
+ end
30
+
31
+ def select(*args, &block)
32
+ dup.tap { |hash| hash.select!(*args, &block) }
33
+ end
34
+
35
+ def reject(*args, &block)
36
+ dup.tap { |hash| hash.reject!(*args, &block) }
37
+ end
38
+
39
+ def nested_under_indifferent_access
40
+ self
41
+ end
42
+
43
+ # Returns true to make sure that this hash is extractable via <tt>Array#extract_options!</tt>
44
+ def extractable_options?
45
+ true
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,81 @@
1
+ module ActiveSupport
2
+ # Usually key value pairs are handled something like this:
3
+ #
4
+ # h = {}
5
+ # h[:boy] = 'John'
6
+ # h[:girl] = 'Mary'
7
+ # h[:boy] # => 'John'
8
+ # h[:girl] # => 'Mary'
9
+ # h[:dog] # => nil
10
+ #
11
+ # Using +OrderedOptions+, the above code could be reduced to:
12
+ #
13
+ # h = ActiveSupport::OrderedOptions.new
14
+ # h.boy = 'John'
15
+ # h.girl = 'Mary'
16
+ # h.boy # => 'John'
17
+ # h.girl # => 'Mary'
18
+ # h.dog # => nil
19
+ #
20
+ # To raise an exception when the value is blank, append a
21
+ # bang to the key name, like:
22
+ #
23
+ # h.dog! # => raises KeyError: key not found: :dog
24
+ #
25
+ class OrderedOptions < Hash
26
+ alias_method :_get, :[] # preserve the original #[] method
27
+ protected :_get # make it protected
28
+
29
+ def []=(key, value)
30
+ super(key.to_sym, value)
31
+ end
32
+
33
+ def [](key)
34
+ super(key.to_sym)
35
+ end
36
+
37
+ def method_missing(name, *args)
38
+ name_string = name.to_s
39
+ if name_string.chomp!('=')
40
+ self[name_string] = args.first
41
+ else
42
+ bangs = name_string.chomp!('!')
43
+
44
+ if bangs
45
+ fetch(name_string.to_sym).presence || raise(KeyError.new("#{name_string} is blank."))
46
+ else
47
+ self[name_string]
48
+ end
49
+ end
50
+ end
51
+
52
+ def respond_to_missing?(name, include_private)
53
+ true
54
+ end
55
+ end
56
+
57
+ # +InheritableOptions+ provides a constructor to build an +OrderedOptions+
58
+ # hash inherited from another hash.
59
+ #
60
+ # Use this if you already have some hash and you want to create a new one based on it.
61
+ #
62
+ # h = ActiveSupport::InheritableOptions.new({ girl: 'Mary', boy: 'John' })
63
+ # h.girl # => 'Mary'
64
+ # h.boy # => 'John'
65
+ class InheritableOptions < OrderedOptions
66
+ def initialize(parent = nil)
67
+ if parent.kind_of?(OrderedOptions)
68
+ # use the faster _get when dealing with OrderedOptions
69
+ super() { |h,k| parent._get(k) }
70
+ elsif parent
71
+ super() { |h,k| parent[k] }
72
+ else
73
+ super()
74
+ end
75
+ end
76
+
77
+ def inheritable_copy
78
+ self.class.new(self)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,58 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+
3
+ module ActiveSupport
4
+ # NOTE: This approach has been deprecated for end-user code in favor of thread_mattr_accessor and friends.
5
+ # Please use that approach instead.
6
+ #
7
+ # This module is used to encapsulate access to thread local variables.
8
+ #
9
+ # Instead of polluting the thread locals namespace:
10
+ #
11
+ # Thread.current[:connection_handler]
12
+ #
13
+ # you define a class that extends this module:
14
+ #
15
+ # module ActiveRecord
16
+ # class RuntimeRegistry
17
+ # extend ActiveSupport::PerThreadRegistry
18
+ #
19
+ # attr_accessor :connection_handler
20
+ # end
21
+ # end
22
+ #
23
+ # and invoke the declared instance accessors as class methods. So
24
+ #
25
+ # ActiveRecord::RuntimeRegistry.connection_handler = connection_handler
26
+ #
27
+ # sets a connection handler local to the current thread, and
28
+ #
29
+ # ActiveRecord::RuntimeRegistry.connection_handler
30
+ #
31
+ # returns a connection handler local to the current thread.
32
+ #
33
+ # This feature is accomplished by instantiating the class and storing the
34
+ # instance as a thread local keyed by the class name. In the example above
35
+ # a key "ActiveRecord::RuntimeRegistry" is stored in <tt>Thread.current</tt>.
36
+ # The class methods proxy to said thread local instance.
37
+ #
38
+ # If the class has an initializer, it must accept no arguments.
39
+ module PerThreadRegistry
40
+ def self.extended(object)
41
+ object.instance_variable_set '@per_thread_registry_key', object.name.freeze
42
+ end
43
+
44
+ def instance
45
+ Thread.current[@per_thread_registry_key] ||= new
46
+ end
47
+
48
+ protected
49
+ def method_missing(name, *args, &block) # :nodoc:
50
+ # Caches the method definition as a singleton method of the receiver.
51
+ #
52
+ # By letting #delegate handle it, we avoid an enclosure that'll capture args.
53
+ singleton_class.delegate name, to: :instance
54
+
55
+ send(name, *args, &block)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,13 @@
1
+ module ActiveSupport
2
+ # A class with no predefined methods that behaves similarly to Builder's
3
+ # BlankSlate. Used for proxy classes.
4
+ class ProxyObject < ::BasicObject
5
+ undef_method :==
6
+ undef_method :equal?
7
+
8
+ # Let ActiveSupport::ProxyObject at least raise exceptions.
9
+ def raise(*args)
10
+ ::Object.send(:raise, *args)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ # This is private interface.
2
+ #
3
+ # Rails components cherry pick from Active Support as needed, but there are a
4
+ # few features that are used for sure in some way or another and it is not worth
5
+ # putting individual requires absolutely everywhere. Think blank? for example.
6
+ #
7
+ # This file is loaded by every Rails component except Active Support itself,
8
+ # but it does not belong to the Rails public interface. It is internal to
9
+ # Rails and can change anytime.
10
+
11
+ # Defines Object#blank? and Object#present?.
12
+ require 'active_support/core_ext/object/blank'
13
+
14
+ # Rails own autoload, eager_load, etc.
15
+ require 'active_support/dependencies/autoload'
16
+
17
+ # Support for ClassMethods and the included macro.
18
+ require 'active_support/concern'
19
+
20
+ # Defines Class#class_attribute.
21
+ require 'active_support/core_ext/class/attribute'
22
+
23
+ # Defines Module#delegate.
24
+ require 'active_support/core_ext/module/delegation'
25
+
26
+ # Defines ActiveSupport::Deprecation.
27
+ require 'active_support/deprecation'
@@ -0,0 +1,51 @@
1
+ require "active_support"
2
+ require "active_support/i18n_railtie"
3
+
4
+ module ActiveSupport
5
+ class Railtie < Rails::Railtie # :nodoc:
6
+ config.active_support = ActiveSupport::OrderedOptions.new
7
+
8
+ config.eager_load_namespaces << ActiveSupport
9
+
10
+ initializer "active_support.deprecation_behavior" do |app|
11
+ if deprecation = app.config.active_support.deprecation
12
+ ActiveSupport::Deprecation.behavior = deprecation
13
+ end
14
+ end
15
+
16
+ # Sets the default value for Time.zone
17
+ # If assigned value cannot be matched to a TimeZone, an exception will be raised.
18
+ initializer "active_support.initialize_time_zone" do |app|
19
+ begin
20
+ TZInfo::DataSource.get
21
+ rescue TZInfo::DataSourceNotFound => e
22
+ raise e.exception "tzinfo-data is not present. Please add gem 'tzinfo-data' to your Gemfile and run bundle install"
23
+ end
24
+ require 'active_support/core_ext/time/zones'
25
+ zone_default = Time.find_zone!(app.config.time_zone)
26
+
27
+ unless zone_default
28
+ raise 'Value assigned to config.time_zone not recognized. ' \
29
+ 'Run "rake time:zones:all" for a time zone names list.'
30
+ end
31
+
32
+ Time.zone_default = zone_default
33
+ end
34
+
35
+ # Sets the default week start
36
+ # If assigned value is not a valid day symbol (e.g. :sunday, :monday, ...), an exception will be raised.
37
+ initializer "active_support.initialize_beginning_of_week" do |app|
38
+ require 'active_support/core_ext/date/calculations'
39
+ beginning_of_week_default = Date.find_beginning_of_week!(app.config.beginning_of_week)
40
+
41
+ Date.beginning_of_week_default = beginning_of_week_default
42
+ end
43
+
44
+ initializer "active_support.set_configs" do |app|
45
+ app.config.active_support.each do |k, v|
46
+ k = "#{k}="
47
+ ActiveSupport.send(k, v) if ActiveSupport.respond_to? k
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,129 @@
1
+ require 'active_support/execution_wrapper'
2
+
3
+ module ActiveSupport
4
+ #--
5
+ # This class defines several callbacks:
6
+ #
7
+ # to_prepare -- Run once at application startup, and also from
8
+ # +to_run+.
9
+ #
10
+ # to_run -- Run before a work run that is reloading. If
11
+ # +reload_classes_only_on_change+ is true (the default), the class
12
+ # unload will have already occurred.
13
+ #
14
+ # to_complete -- Run after a work run that has reloaded. If
15
+ # +reload_classes_only_on_change+ is false, the class unload will
16
+ # have occurred after the work run, but before this callback.
17
+ #
18
+ # before_class_unload -- Run immediately before the classes are
19
+ # unloaded.
20
+ #
21
+ # after_class_unload -- Run immediately after the classes are
22
+ # unloaded.
23
+ #
24
+ class Reloader < ExecutionWrapper
25
+ define_callbacks :prepare
26
+
27
+ define_callbacks :class_unload
28
+
29
+ def self.to_prepare(*args, &block)
30
+ set_callback(:prepare, *args, &block)
31
+ end
32
+
33
+ def self.before_class_unload(*args, &block)
34
+ set_callback(:class_unload, *args, &block)
35
+ end
36
+
37
+ def self.after_class_unload(*args, &block)
38
+ set_callback(:class_unload, :after, *args, &block)
39
+ end
40
+
41
+ to_run(:after) { self.class.prepare! }
42
+
43
+ # Initiate a manual reload
44
+ def self.reload!
45
+ executor.wrap do
46
+ new.tap do |instance|
47
+ begin
48
+ instance.run!
49
+ ensure
50
+ instance.complete!
51
+ end
52
+ end
53
+ end
54
+ prepare!
55
+ end
56
+
57
+ def self.run! # :nodoc:
58
+ if check!
59
+ super
60
+ else
61
+ Null
62
+ end
63
+ end
64
+
65
+ # Run the supplied block as a work unit, reloading code as needed
66
+ def self.wrap
67
+ executor.wrap do
68
+ super
69
+ end
70
+ end
71
+
72
+ class_attribute :executor
73
+ class_attribute :check
74
+
75
+ self.executor = Executor
76
+ self.check = lambda { false }
77
+
78
+ def self.check! # :nodoc:
79
+ @should_reload ||= check.call
80
+ end
81
+
82
+ def self.reloaded! # :nodoc:
83
+ @should_reload = false
84
+ end
85
+
86
+ def self.prepare! # :nodoc:
87
+ new.run_callbacks(:prepare)
88
+ end
89
+
90
+ def initialize
91
+ super
92
+ @locked = false
93
+ end
94
+
95
+ # Acquire the ActiveSupport::Dependencies::Interlock unload lock,
96
+ # ensuring it will be released automatically
97
+ def require_unload_lock!
98
+ unless @locked
99
+ ActiveSupport::Dependencies.interlock.start_unloading
100
+ @locked = true
101
+ end
102
+ end
103
+
104
+ # Release the unload lock if it has been previously obtained
105
+ def release_unload_lock!
106
+ if @locked
107
+ @locked = false
108
+ ActiveSupport::Dependencies.interlock.done_unloading
109
+ end
110
+ end
111
+
112
+ def run! # :nodoc:
113
+ super
114
+ release_unload_lock!
115
+ end
116
+
117
+ def class_unload!(&block) # :nodoc:
118
+ require_unload_lock!
119
+ run_callbacks(:class_unload, &block)
120
+ end
121
+
122
+ def complete! # :nodoc:
123
+ super
124
+ self.class.reloaded!
125
+ ensure
126
+ release_unload_lock!
127
+ end
128
+ end
129
+ end