activesupport 3.1.0 → 5.0.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 (276) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +798 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +13 -7
  5. data/lib/active_support/array_inquirer.rb +44 -0
  6. data/lib/active_support/backtrace_cleaner.rb +38 -34
  7. data/lib/active_support/benchmarkable.rb +17 -28
  8. data/lib/active_support/cache/file_store.rb +85 -70
  9. data/lib/active_support/cache/mem_cache_store.rb +75 -66
  10. data/lib/active_support/cache/memory_store.rb +31 -23
  11. data/lib/active_support/cache/null_store.rb +41 -0
  12. data/lib/active_support/cache/strategy/local_cache.rb +73 -70
  13. data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
  14. data/lib/active_support/cache.rb +360 -294
  15. data/lib/active_support/callbacks.rb +563 -393
  16. data/lib/active_support/concern.rb +42 -34
  17. data/lib/active_support/concurrency/latch.rb +19 -0
  18. data/lib/active_support/concurrency/share_lock.rb +186 -0
  19. data/lib/active_support/configurable.rb +70 -12
  20. data/lib/active_support/core_ext/array/access.rb +53 -9
  21. data/lib/active_support/core_ext/array/conversions.rb +109 -62
  22. data/lib/active_support/core_ext/array/extract_options.rb +2 -2
  23. data/lib/active_support/core_ext/array/grouping.rb +39 -32
  24. data/lib/active_support/core_ext/array/inquiry.rb +17 -0
  25. data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
  26. data/lib/active_support/core_ext/array/wrap.rb +16 -18
  27. data/lib/active_support/core_ext/array.rb +2 -2
  28. data/lib/active_support/core_ext/benchmark.rb +7 -0
  29. data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -36
  30. data/lib/active_support/core_ext/class/attribute.rb +47 -34
  31. data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -79
  32. data/lib/active_support/core_ext/class/subclasses.rb +12 -7
  33. data/lib/active_support/core_ext/class.rb +0 -3
  34. data/lib/active_support/core_ext/date/blank.rb +12 -0
  35. data/lib/active_support/core_ext/date/calculations.rb +57 -167
  36. data/lib/active_support/core_ext/date/conversions.rb +31 -42
  37. data/lib/active_support/core_ext/date/zones.rb +2 -10
  38. data/lib/active_support/core_ext/date.rb +5 -0
  39. data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
  40. data/lib/active_support/core_ext/date_and_time/compatibility.rb +18 -0
  41. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  42. data/lib/active_support/core_ext/date_time/acts_like.rb +1 -0
  43. data/lib/active_support/core_ext/date_time/blank.rb +12 -0
  44. data/lib/active_support/core_ext/date_time/calculations.rb +132 -65
  45. data/lib/active_support/core_ext/date_time/compatibility.rb +5 -0
  46. data/lib/active_support/core_ext/date_time/conversions.rb +36 -34
  47. data/lib/active_support/core_ext/date_time.rb +5 -0
  48. data/lib/active_support/core_ext/digest/uuid.rb +51 -0
  49. data/lib/active_support/core_ext/enumerable.rb +81 -74
  50. data/lib/active_support/core_ext/file/atomic.rb +53 -26
  51. data/lib/active_support/core_ext/file.rb +0 -1
  52. data/lib/active_support/core_ext/hash/compact.rb +20 -0
  53. data/lib/active_support/core_ext/hash/conversions.rb +175 -70
  54. data/lib/active_support/core_ext/hash/deep_merge.rb +30 -8
  55. data/lib/active_support/core_ext/hash/except.rb +11 -12
  56. data/lib/active_support/core_ext/hash/indifferent_access.rb +7 -8
  57. data/lib/active_support/core_ext/hash/keys.rb +147 -24
  58. data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
  59. data/lib/active_support/core_ext/hash/slice.rb +22 -14
  60. data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
  61. data/lib/active_support/core_ext/hash.rb +2 -2
  62. data/lib/active_support/core_ext/integer/inflections.rb +13 -1
  63. data/lib/active_support/core_ext/integer/multiple.rb +4 -0
  64. data/lib/active_support/core_ext/integer/time.rb +12 -22
  65. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -2
  66. data/lib/active_support/core_ext/kernel/concern.rb +12 -0
  67. data/lib/active_support/core_ext/kernel/debugger.rb +2 -15
  68. data/lib/active_support/core_ext/kernel/reporting.rb +12 -62
  69. data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
  70. data/lib/active_support/core_ext/kernel.rb +2 -3
  71. data/lib/active_support/core_ext/load_error.rb +14 -7
  72. data/lib/active_support/core_ext/marshal.rb +22 -0
  73. data/lib/active_support/core_ext/module/aliasing.rb +16 -12
  74. data/lib/active_support/core_ext/module/anonymous.rb +12 -8
  75. data/lib/active_support/core_ext/module/attr_internal.rb +2 -5
  76. data/lib/active_support/core_ext/module/attribute_accessors.rb +165 -13
  77. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
  78. data/lib/active_support/core_ext/module/concerning.rb +135 -0
  79. data/lib/active_support/core_ext/module/delegation.rb +141 -68
  80. data/lib/active_support/core_ext/module/deprecation.rb +17 -3
  81. data/lib/active_support/core_ext/module/introspection.rb +9 -31
  82. data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
  83. data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
  84. data/lib/active_support/core_ext/module/reachable.rb +1 -3
  85. data/lib/active_support/core_ext/module/remove_method.rb +24 -5
  86. data/lib/active_support/core_ext/module.rb +3 -3
  87. data/lib/active_support/core_ext/name_error.rb +15 -2
  88. data/lib/active_support/core_ext/numeric/bytes.rb +20 -0
  89. data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
  90. data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
  91. data/lib/active_support/core_ext/numeric/time.rb +31 -36
  92. data/lib/active_support/core_ext/numeric.rb +2 -0
  93. data/lib/active_support/core_ext/object/acts_like.rb +4 -4
  94. data/lib/active_support/core_ext/object/blank.rb +52 -18
  95. data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
  96. data/lib/active_support/core_ext/object/duplicable.rb +12 -20
  97. data/lib/active_support/core_ext/object/inclusion.rb +13 -1
  98. data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
  99. data/lib/active_support/core_ext/object/json.rb +205 -0
  100. data/lib/active_support/core_ext/object/to_param.rb +1 -55
  101. data/lib/active_support/core_ext/object/to_query.rb +66 -9
  102. data/lib/active_support/core_ext/object/try.rb +124 -33
  103. data/lib/active_support/core_ext/object/with_options.rb +37 -11
  104. data/lib/active_support/core_ext/object.rb +2 -1
  105. data/lib/active_support/core_ext/range/conversions.rb +17 -7
  106. data/lib/active_support/core_ext/range/each.rb +21 -0
  107. data/lib/active_support/core_ext/range/include_range.rb +20 -18
  108. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  109. data/lib/active_support/core_ext/range.rb +1 -2
  110. data/lib/active_support/core_ext/securerandom.rb +23 -0
  111. data/lib/active_support/core_ext/string/access.rb +95 -90
  112. data/lib/active_support/core_ext/string/behavior.rb +1 -1
  113. data/lib/active_support/core_ext/string/conversions.rb +41 -38
  114. data/lib/active_support/core_ext/string/exclude.rb +6 -1
  115. data/lib/active_support/core_ext/string/filters.rb +70 -17
  116. data/lib/active_support/core_ext/string/indent.rb +43 -0
  117. data/lib/active_support/core_ext/string/inflections.rb +139 -59
  118. data/lib/active_support/core_ext/string/inquiry.rb +2 -2
  119. data/lib/active_support/core_ext/string/multibyte.rb +46 -65
  120. data/lib/active_support/core_ext/string/output_safety.rb +153 -56
  121. data/lib/active_support/core_ext/string/strip.rb +3 -6
  122. data/lib/active_support/core_ext/string/zones.rb +14 -0
  123. data/lib/active_support/core_ext/string.rb +2 -3
  124. data/lib/active_support/core_ext/struct.rb +3 -0
  125. data/lib/active_support/core_ext/time/calculations.rb +173 -173
  126. data/lib/active_support/core_ext/time/compatibility.rb +5 -0
  127. data/lib/active_support/core_ext/time/conversions.rb +33 -29
  128. data/lib/active_support/core_ext/time/marshal.rb +2 -56
  129. data/lib/active_support/core_ext/time/zones.rb +57 -32
  130. data/lib/active_support/core_ext/time.rb +5 -0
  131. data/lib/active_support/core_ext/uri.rb +13 -19
  132. data/lib/active_support/core_ext.rb +3 -2
  133. data/lib/active_support/dependencies/autoload.rb +47 -20
  134. data/lib/active_support/dependencies/interlock.rb +51 -0
  135. data/lib/active_support/dependencies.rb +315 -265
  136. data/lib/active_support/deprecation/behaviors.rb +71 -30
  137. data/lib/active_support/deprecation/instance_delegator.rb +24 -0
  138. data/lib/active_support/deprecation/method_wrappers.rb +59 -18
  139. data/lib/active_support/deprecation/proxy_wrappers.rb +82 -14
  140. data/lib/active_support/deprecation/reporting.rb +61 -14
  141. data/lib/active_support/deprecation.rb +38 -13
  142. data/lib/active_support/descendants_tracker.rb +34 -19
  143. data/lib/active_support/duration/iso8601_parser.rb +122 -0
  144. data/lib/active_support/duration/iso8601_serializer.rb +51 -0
  145. data/lib/active_support/duration.rb +85 -14
  146. data/lib/active_support/evented_file_update_checker.rb +194 -0
  147. data/lib/active_support/execution_wrapper.rb +117 -0
  148. data/lib/active_support/executor.rb +6 -0
  149. data/lib/active_support/file_update_checker.rb +138 -17
  150. data/lib/active_support/gem_version.rb +15 -0
  151. data/lib/active_support/gzip.rb +11 -5
  152. data/lib/active_support/hash_with_indifferent_access.rb +199 -49
  153. data/lib/active_support/i18n.rb +6 -2
  154. data/lib/active_support/i18n_railtie.rb +40 -21
  155. data/lib/active_support/inflections.rb +22 -13
  156. data/lib/active_support/inflector/inflections.rb +175 -144
  157. data/lib/active_support/inflector/methods.rb +328 -91
  158. data/lib/active_support/inflector/transliterate.rb +51 -37
  159. data/lib/active_support/json/decoding.rb +31 -22
  160. data/lib/active_support/json/encoding.rb +88 -248
  161. data/lib/active_support/key_generator.rb +71 -0
  162. data/lib/active_support/lazy_load_hooks.rb +27 -25
  163. data/lib/active_support/locale/en.yml +102 -3
  164. data/lib/active_support/log_subscriber/test_helper.rb +24 -21
  165. data/lib/active_support/log_subscriber.rb +36 -49
  166. data/lib/active_support/logger.rb +106 -0
  167. data/lib/active_support/logger_silence.rb +28 -0
  168. data/lib/active_support/logger_thread_safe_level.rb +31 -0
  169. data/lib/active_support/message_encryptor.rb +72 -36
  170. data/lib/active_support/message_verifier.rb +96 -24
  171. data/lib/active_support/multibyte/chars.rb +88 -333
  172. data/lib/active_support/multibyte/unicode.rb +156 -136
  173. data/lib/active_support/multibyte.rb +5 -28
  174. data/lib/active_support/notifications/fanout.rb +115 -19
  175. data/lib/active_support/notifications/instrumenter.rb +52 -15
  176. data/lib/active_support/notifications.rb +168 -33
  177. data/lib/active_support/number_helper/number_converter.rb +182 -0
  178. data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
  179. data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
  180. data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
  181. data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
  182. data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
  183. data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
  184. data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
  185. data/lib/active_support/number_helper.rb +368 -0
  186. data/lib/active_support/option_merger.rb +1 -1
  187. data/lib/active_support/ordered_hash.rb +18 -183
  188. data/lib/active_support/ordered_options.rb +44 -24
  189. data/lib/active_support/per_thread_registry.rb +58 -0
  190. data/lib/active_support/proxy_object.rb +13 -0
  191. data/lib/active_support/rails.rb +27 -0
  192. data/lib/active_support/railtie.rb +25 -34
  193. data/lib/active_support/reloader.rb +129 -0
  194. data/lib/active_support/rescuable.rb +98 -48
  195. data/lib/active_support/security_utils.rb +27 -0
  196. data/lib/active_support/string_inquirer.rb +14 -9
  197. data/lib/active_support/subscriber.rb +120 -0
  198. data/lib/active_support/tagged_logging.rb +78 -0
  199. data/lib/active_support/test_case.rb +69 -17
  200. data/lib/active_support/testing/assertions.rb +43 -41
  201. data/lib/active_support/testing/autorun.rb +12 -0
  202. data/lib/active_support/testing/constant_lookup.rb +50 -0
  203. data/lib/active_support/testing/declarative.rb +7 -21
  204. data/lib/active_support/testing/deprecation.rb +14 -33
  205. data/lib/active_support/testing/file_fixtures.rb +34 -0
  206. data/lib/active_support/testing/isolation.rb +53 -95
  207. data/lib/active_support/testing/method_call_assertions.rb +41 -0
  208. data/lib/active_support/testing/setup_and_teardown.rb +21 -82
  209. data/lib/active_support/testing/stream.rb +42 -0
  210. data/lib/active_support/testing/tagged_logging.rb +25 -0
  211. data/lib/active_support/testing/time_helpers.rb +134 -0
  212. data/lib/active_support/time.rb +6 -23
  213. data/lib/active_support/time_with_zone.rb +239 -92
  214. data/lib/active_support/values/time_zone.rb +236 -160
  215. data/lib/active_support/values/unicode_tables.dat +0 -0
  216. data/lib/active_support/version.rb +5 -7
  217. data/lib/active_support/xml_mini/jdom.rb +19 -13
  218. data/lib/active_support/xml_mini/libxml.rb +3 -4
  219. data/lib/active_support/xml_mini/libxmlsax.rb +2 -3
  220. data/lib/active_support/xml_mini/nokogiri.rb +3 -4
  221. data/lib/active_support/xml_mini/nokogirisax.rb +2 -3
  222. data/lib/active_support/xml_mini/rexml.rb +8 -10
  223. data/lib/active_support/xml_mini.rb +66 -34
  224. data/lib/active_support.rb +40 -23
  225. metadata +185 -134
  226. data/CHANGELOG +0 -1534
  227. data/lib/active_support/base64.rb +0 -42
  228. data/lib/active_support/basic_object.rb +0 -21
  229. data/lib/active_support/buffered_logger.rb +0 -137
  230. data/lib/active_support/cache/compressed_mem_cache_store.rb +0 -13
  231. data/lib/active_support/cache/synchronized_memory_store.rb +0 -11
  232. data/lib/active_support/core_ext/array/random_access.rb +0 -30
  233. data/lib/active_support/core_ext/array/uniq_by.rb +0 -16
  234. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -44
  235. data/lib/active_support/core_ext/class/inheritable_attributes.rb +0 -178
  236. data/lib/active_support/core_ext/date/freeze.rb +0 -31
  237. data/lib/active_support/core_ext/date_time/zones.rb +0 -21
  238. data/lib/active_support/core_ext/exception.rb +0 -3
  239. data/lib/active_support/core_ext/file/path.rb +0 -5
  240. data/lib/active_support/core_ext/float/rounding.rb +0 -19
  241. data/lib/active_support/core_ext/float.rb +0 -1
  242. data/lib/active_support/core_ext/hash/deep_dup.rb +0 -11
  243. data/lib/active_support/core_ext/hash/diff.rb +0 -13
  244. data/lib/active_support/core_ext/kernel/requires.rb +0 -28
  245. data/lib/active_support/core_ext/logger.rb +0 -81
  246. data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +0 -31
  247. data/lib/active_support/core_ext/module/method_names.rb +0 -14
  248. data/lib/active_support/core_ext/module/synchronization.rb +0 -43
  249. data/lib/active_support/core_ext/object/to_json.rb +0 -19
  250. data/lib/active_support/core_ext/proc.rb +0 -14
  251. data/lib/active_support/core_ext/process/daemon.rb +0 -23
  252. data/lib/active_support/core_ext/process.rb +0 -1
  253. data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
  254. data/lib/active_support/core_ext/range/cover.rb +0 -3
  255. data/lib/active_support/core_ext/rexml.rb +0 -46
  256. data/lib/active_support/core_ext/string/encoding.rb +0 -11
  257. data/lib/active_support/core_ext/string/interpolation.rb +0 -2
  258. data/lib/active_support/core_ext/string/xchar.rb +0 -18
  259. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
  260. data/lib/active_support/file_watcher.rb +0 -36
  261. data/lib/active_support/json/variable.rb +0 -9
  262. data/lib/active_support/memoizable.rb +0 -105
  263. data/lib/active_support/multibyte/exceptions.rb +0 -8
  264. data/lib/active_support/multibyte/utils.rb +0 -60
  265. data/lib/active_support/ruby/shim.rb +0 -22
  266. data/lib/active_support/secure_random.rb +0 -6
  267. data/lib/active_support/testing/mochaing.rb +0 -7
  268. data/lib/active_support/testing/pending.rb +0 -52
  269. data/lib/active_support/testing/performance/jruby.rb +0 -115
  270. data/lib/active_support/testing/performance/rubinius.rb +0 -113
  271. data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
  272. data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
  273. data/lib/active_support/testing/performance/ruby.rb +0 -152
  274. data/lib/active_support/testing/performance.rb +0 -317
  275. data/lib/active_support/time/autoload.rb +0 -5
  276. data/lib/active_support/whiny_nil.rb +0 -60
@@ -1,79 +1,4 @@
1
- require 'active_support/core_ext/array/extract_options'
2
-
3
- # Extends the class object with class and instance accessors for class attributes,
4
- # just like the native attr* accessors for instance attributes.
5
- #
6
- # Note that unlike +class_attribute+, if a subclass changes the value then that would
7
- # also change the value for parent class. Similarly if parent class changes the value
8
- # then that would change the value of subclasses too.
9
- #
10
- # class Person
11
- # cattr_accessor :hair_colors
12
- # end
13
- #
14
- # Person.hair_colors = [:brown, :black, :blonde, :red]
15
- # Person.hair_colors # => [:brown, :black, :blonde, :red]
16
- # Person.new.hair_colors # => [:brown, :black, :blonde, :red]
17
- #
18
- # To opt out of the instance writer method, pass :instance_writer => false.
19
- # To opt out of the instance reader method, pass :instance_reader => false.
20
- #
21
- # class Person
22
- # cattr_accessor :hair_colors, :instance_writer => false, :instance_reader => false
23
- # end
24
- #
25
- # Person.new.hair_colors = [:brown] # => NoMethodError
26
- # Person.new.hair_colors # => NoMethodError
27
- class Class
28
- def cattr_reader(*syms)
29
- options = syms.extract_options!
30
- syms.each do |sym|
31
- class_eval(<<-EOS, __FILE__, __LINE__ + 1)
32
- unless defined? @@#{sym}
33
- @@#{sym} = nil
34
- end
35
-
36
- def self.#{sym}
37
- @@#{sym}
38
- end
39
- EOS
40
-
41
- unless options[:instance_reader] == false
42
- class_eval(<<-EOS, __FILE__, __LINE__ + 1)
43
- def #{sym}
44
- @@#{sym}
45
- end
46
- EOS
47
- end
48
- end
49
- end
50
-
51
- def cattr_writer(*syms)
52
- options = syms.extract_options!
53
- syms.each do |sym|
54
- class_eval(<<-EOS, __FILE__, __LINE__ + 1)
55
- unless defined? @@#{sym}
56
- @@#{sym} = nil
57
- end
58
-
59
- def self.#{sym}=(obj)
60
- @@#{sym} = obj
61
- end
62
- EOS
63
-
64
- unless options[:instance_writer] == false
65
- class_eval(<<-EOS, __FILE__, __LINE__ + 1)
66
- def #{sym}=(obj)
67
- @@#{sym} = obj
68
- end
69
- EOS
70
- end
71
- self.send("#{sym}=", yield) if block_given?
72
- end
73
- end
74
-
75
- def cattr_accessor(*syms, &blk)
76
- cattr_reader(*syms)
77
- cattr_writer(*syms, &blk)
78
- end
79
- end
1
+ # cattr_* became mattr_* aliases in 7dfbd91b0780fbd6a1dd9bfbc176e10894871d2d,
2
+ # but we keep this around for libraries that directly require it knowing they
3
+ # want cattr_*. No need to deprecate.
4
+ require 'active_support/core_ext/module/attribute_accessors'
@@ -1,19 +1,20 @@
1
1
  require 'active_support/core_ext/module/anonymous'
2
2
  require 'active_support/core_ext/module/reachable'
3
3
 
4
- class Class #:nodoc:
4
+ class Class
5
5
  begin
6
- ObjectSpace.each_object(Class.new) {}
6
+ # Test if this Ruby supports each_object against singleton_class
7
+ ObjectSpace.each_object(Numeric.singleton_class) {}
7
8
 
8
- def descendants
9
+ def descendants # :nodoc:
9
10
  descendants = []
10
- ObjectSpace.each_object(class << self; self; end) do |k|
11
+ ObjectSpace.each_object(singleton_class) do |k|
11
12
  descendants.unshift k unless k == self
12
13
  end
13
14
  descendants
14
15
  end
15
- rescue StandardError # JRuby
16
- def descendants
16
+ rescue StandardError # JRuby 9.0.4.0 and earlier
17
+ def descendants # :nodoc:
17
18
  descendants = []
18
19
  ObjectSpace.each_object(Class) do |k|
19
20
  descendants.unshift k if k < self
@@ -25,7 +26,11 @@ class Class #:nodoc:
25
26
 
26
27
  # Returns an array with the direct children of +self+.
27
28
  #
28
- # Integer.subclasses # => [Bignum, Fixnum]
29
+ # class Foo; end
30
+ # class Bar < Foo; end
31
+ # class Baz < Bar; end
32
+ #
33
+ # Foo.subclasses # => [Bar]
29
34
  def subclasses
30
35
  subclasses, chain = [], descendants
31
36
  chain.each do |k|
@@ -1,5 +1,2 @@
1
1
  require 'active_support/core_ext/class/attribute'
2
- require 'active_support/core_ext/class/attribute_accessors'
3
- require 'active_support/core_ext/class/inheritable_attributes'
4
- require 'active_support/core_ext/class/delegating_attributes'
5
2
  require 'active_support/core_ext/class/subclasses'
@@ -0,0 +1,12 @@
1
+ require 'date'
2
+
3
+ class Date #:nodoc:
4
+ # No Date is blank:
5
+ #
6
+ # Date.today.blank? # => false
7
+ #
8
+ # @return [false]
9
+ def blank?
10
+ false
11
+ end
12
+ end
@@ -3,29 +3,35 @@ require 'active_support/duration'
3
3
  require 'active_support/core_ext/object/acts_like'
4
4
  require 'active_support/core_ext/date/zones'
5
5
  require 'active_support/core_ext/time/zones'
6
+ require 'active_support/core_ext/date_and_time/calculations'
6
7
 
7
8
  class Date
8
- DAYS_INTO_WEEK = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }
9
-
10
- if RUBY_VERSION < '1.9'
11
- undef :>>
12
-
13
- # Backported from 1.9. The one in 1.8 leads to incorrect next_month and
14
- # friends for dates where the calendar reform is involved. It additionally
15
- # prevents an infinite loop fixed in r27013.
16
- def >>(n)
17
- y, m = (year * 12 + (mon - 1) + n).divmod(12)
18
- m, = (m + 1) .divmod(1)
19
- d = mday
20
- until jd2 = self.class.valid_civil?(y, m, d, start)
21
- d -= 1
22
- raise ArgumentError, 'invalid date' unless d > 0
23
- end
24
- self + (jd2 - jd)
25
- end
26
- end
9
+ include DateAndTime::Calculations
27
10
 
28
11
  class << self
12
+ attr_accessor :beginning_of_week_default
13
+
14
+ # Returns the week start (e.g. :monday) for the current request, if this has been set (via Date.beginning_of_week=).
15
+ # If <tt>Date.beginning_of_week</tt> has not been set for the current request, returns the week start specified in <tt>config.beginning_of_week</tt>.
16
+ # If no config.beginning_of_week was specified, returns :monday.
17
+ def beginning_of_week
18
+ Thread.current[:beginning_of_week] || beginning_of_week_default || :monday
19
+ end
20
+
21
+ # Sets <tt>Date.beginning_of_week</tt> to a week start (e.g. :monday) for current request/thread.
22
+ #
23
+ # This method accepts any of the following day symbols:
24
+ # :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday
25
+ def beginning_of_week=(week_start)
26
+ Thread.current[:beginning_of_week] = find_beginning_of_week!(week_start)
27
+ end
28
+
29
+ # Returns week start day symbol (e.g. :monday), or raises an +ArgumentError+ for invalid day symbol.
30
+ def find_beginning_of_week!(week_start)
31
+ raise ArgumentError, "Invalid beginning of week: #{week_start}" unless ::Date::DAYS_INTO_WEEK.key?(week_start)
32
+ week_start
33
+ end
34
+
29
35
  # Returns a new Date representing the date 1 day ago (i.e. yesterday's date).
30
36
  def yesterday
31
37
  ::Date.current.yesterday
@@ -42,46 +48,42 @@ class Date
42
48
  end
43
49
  end
44
50
 
45
- # Returns true if the Date object's date lies in the past. Otherwise returns false.
46
- def past?
47
- self < ::Date.current
48
- end
49
-
50
- # Returns true if the Date object's date is today.
51
- def today?
52
- self.to_date == ::Date.current # we need the to_date because of DateTime
53
- end
54
-
55
- # Returns true if the Date object's date lies in the future.
56
- def future?
57
- self > ::Date.current
58
- end
59
-
60
51
  # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
61
52
  # and then subtracts the specified number of seconds.
62
53
  def ago(seconds)
63
- to_time_in_current_zone.since(-seconds)
54
+ in_time_zone.since(-seconds)
64
55
  end
65
56
 
66
57
  # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
67
58
  # and then adds the specified number of seconds
68
59
  def since(seconds)
69
- to_time_in_current_zone.since(seconds)
60
+ in_time_zone.since(seconds)
70
61
  end
71
62
  alias :in :since
72
63
 
73
64
  # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
74
65
  def beginning_of_day
75
- to_time_in_current_zone
66
+ in_time_zone
76
67
  end
77
68
  alias :midnight :beginning_of_day
78
69
  alias :at_midnight :beginning_of_day
79
70
  alias :at_beginning_of_day :beginning_of_day
80
71
 
72
+ # Converts Date to a Time (or DateTime if necessary) with the time portion set to the middle of the day (12:00)
73
+ def middle_of_day
74
+ in_time_zone.middle_of_day
75
+ end
76
+ alias :midday :middle_of_day
77
+ alias :noon :middle_of_day
78
+ alias :at_midday :middle_of_day
79
+ alias :at_noon :middle_of_day
80
+ alias :at_middle_of_day :middle_of_day
81
+
81
82
  # Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
82
83
  def end_of_day
83
- to_time_in_current_zone.end_of_day
84
+ in_time_zone.end_of_day
84
85
  end
86
+ alias :at_end_of_day :end_of_day
85
87
 
86
88
  def plus_with_duration(other) #:nodoc:
87
89
  if ActiveSupport::Duration === other
@@ -103,7 +105,7 @@ class Date
103
105
  alias_method :minus_without_duration, :-
104
106
  alias_method :-, :minus_with_duration
105
107
 
106
- # Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
108
+ # Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
107
109
  # any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
108
110
  def advance(options)
109
111
  options = options.dup
@@ -116,138 +118,26 @@ class Date
116
118
  end
117
119
 
118
120
  # Returns a new Date where one or more of the elements have been changed according to the +options+ parameter.
121
+ # The +options+ parameter is a hash with a combination of these keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>.
119
122
  #
120
- # Examples:
121
- #
122
- # Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 1)
123
- # Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12)
123
+ # Date.new(2007, 5, 12).change(day: 1) # => Date.new(2007, 5, 1)
124
+ # Date.new(2007, 5, 12).change(year: 2005, month: 1) # => Date.new(2005, 1, 12)
124
125
  def change(options)
125
126
  ::Date.new(
126
- options[:year] || self.year,
127
- options[:month] || self.month,
128
- options[:day] || self.day
127
+ options.fetch(:year, year),
128
+ options.fetch(:month, month),
129
+ options.fetch(:day, day)
129
130
  )
130
131
  end
131
-
132
- # Returns a new Date/DateTime representing the time a number of specified weeks ago.
133
- def weeks_ago(weeks)
134
- advance(:weeks => -weeks)
135
- end
136
-
137
- # Returns a new Date/DateTime representing the time a number of specified months ago.
138
- def months_ago(months)
139
- advance(:months => -months)
140
- end
141
-
142
- # Returns a new Date/DateTime representing the time a number of specified months in the future.
143
- def months_since(months)
144
- advance(:months => months)
145
- end
146
-
147
- # Returns a new Date/DateTime representing the time a number of specified years ago.
148
- def years_ago(years)
149
- advance(:years => -years)
150
- end
151
-
152
- # Returns a new Date/DateTime representing the time a number of specified years in the future.
153
- def years_since(years)
154
- advance(:years => years)
155
- end
156
-
157
- # Shorthand for years_ago(1)
158
- def prev_year
159
- years_ago(1)
160
- end unless method_defined?(:prev_year)
161
-
162
- # Shorthand for years_since(1)
163
- def next_year
164
- years_since(1)
165
- end unless method_defined?(:next_year)
166
-
167
- # Shorthand for months_ago(1)
168
- def prev_month
169
- months_ago(1)
170
- end unless method_defined?(:prev_month)
171
-
172
- # Shorthand for months_since(1)
173
- def next_month
174
- months_since(1)
175
- end unless method_defined?(:next_month)
176
-
177
- # Returns a new Date/DateTime representing the "start" of this week (i.e, Monday; DateTime objects will have time set to 0:00).
178
- def beginning_of_week
179
- days_to_monday = self.wday!=0 ? self.wday-1 : 6
180
- result = self - days_to_monday
181
- self.acts_like?(:time) ? result.midnight : result
182
- end
183
- alias :monday :beginning_of_week
184
- alias :at_beginning_of_week :beginning_of_week
185
-
186
- # Returns a new Date/DateTime representing the end of this week (Sunday, DateTime objects will have time set to 23:59:59).
187
- def end_of_week
188
- days_to_sunday = self.wday!=0 ? 7-self.wday : 0
189
- result = self + days_to_sunday.days
190
- self.acts_like?(:time) ? result.end_of_day : result
191
- end
192
- alias :sunday :end_of_week
193
- alias :at_end_of_week :end_of_week
194
-
195
- # Returns a new Date/DateTime representing the start of the given day in the previous week (default is Monday).
196
- def prev_week(day = :monday)
197
- result = (self - 7).beginning_of_week + DAYS_INTO_WEEK[day]
198
- self.acts_like?(:time) ? result.change(:hour => 0) : result
199
- end
200
-
201
- # Returns a new Date/DateTime representing the start of the given day in next week (default is Monday).
202
- def next_week(day = :monday)
203
- result = (self + 7).beginning_of_week + DAYS_INTO_WEEK[day]
204
- self.acts_like?(:time) ? result.change(:hour => 0) : result
205
- end
206
-
207
- # Returns a new ; DateTime objects will have time set to 0:00DateTime representing the start of the month (1st of the month; DateTime objects will have time set to 0:00)
208
- def beginning_of_month
209
- self.acts_like?(:time) ? change(:day => 1, :hour => 0) : change(:day => 1)
210
- end
211
- alias :at_beginning_of_month :beginning_of_month
212
-
213
- # Returns a new Date/DateTime representing the end of the month (last day of the month; DateTime objects will have time set to 0:00)
214
- def end_of_month
215
- last_day = ::Time.days_in_month( self.month, self.year )
216
- self.acts_like?(:time) ? change(:day => last_day, :hour => 23, :min => 59, :sec => 59) : change(:day => last_day)
217
- end
218
- alias :at_end_of_month :end_of_month
219
-
220
- # Returns a new Date/DateTime representing the start of the quarter (1st of january, april, july, october; DateTime objects will have time set to 0:00)
221
- def beginning_of_quarter
222
- beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
223
- end
224
- alias :at_beginning_of_quarter :beginning_of_quarter
225
-
226
- # Returns a new Date/DateTime representing the end of the quarter (last day of march, june, september, december; DateTime objects will have time set to 23:59:59)
227
- def end_of_quarter
228
- beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month
229
- end
230
- alias :at_end_of_quarter :end_of_quarter
231
-
232
- # Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00)
233
- def beginning_of_year
234
- self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0) : change(:month => 1, :day => 1)
235
- end
236
- alias :at_beginning_of_year :beginning_of_year
237
-
238
- # Returns a new Time representing the end of the year (31st of december; DateTime objects will have time set to 23:59:59)
239
- def end_of_year
240
- self.acts_like?(:time) ? change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59) : change(:month => 12, :day => 31)
241
- end
242
- alias :at_end_of_year :end_of_year
243
-
244
- # Convenience method which returns a new Date/DateTime representing the time 1 day ago
245
- def yesterday
246
- self - 1
247
- end
248
-
249
- # Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time
250
- def tomorrow
251
- self + 1
132
+
133
+ # Allow Date to be compared with Time by converting to DateTime and relying on the <=> from there.
134
+ def compare_with_coercion(other)
135
+ if other.is_a?(Time)
136
+ self.to_datetime <=> other
137
+ else
138
+ compare_without_coercion(other)
139
+ end
252
140
  end
141
+ alias_method :compare_without_coercion, :<=>
142
+ alias_method :<=>, :compare_with_coercion
253
143
  end
@@ -5,43 +5,50 @@ require 'active_support/core_ext/module/remove_method'
5
5
 
6
6
  class Date
7
7
  DATE_FORMATS = {
8
- :short => "%e %b",
9
- :long => "%B %e, %Y",
10
- :db => "%Y-%m-%d",
11
- :number => "%Y%m%d",
12
- :long_ordinal => lambda { |date| date.strftime("%B #{ActiveSupport::Inflector.ordinalize(date.day)}, %Y") }, # => "April 25th, 2007"
13
- :rfc822 => "%e %b %Y"
8
+ :short => '%d %b',
9
+ :long => '%B %d, %Y',
10
+ :db => '%Y-%m-%d',
11
+ :number => '%Y%m%d',
12
+ :long_ordinal => lambda { |date|
13
+ day_format = ActiveSupport::Inflector.ordinalize(date.day)
14
+ date.strftime("%B #{day_format}, %Y") # => "April 25th, 2007"
15
+ },
16
+ :rfc822 => '%d %b %Y',
17
+ :iso8601 => lambda { |date| date.iso8601 }
14
18
  }
15
19
 
16
20
  # Ruby 1.9 has Date#to_time which converts to localtime only.
17
- remove_possible_method :to_time
21
+ remove_method :to_time
18
22
 
19
- # Ruby 1.9 has Date#xmlschema which converts to a string without the time component.
23
+ # Ruby 1.9 has Date#xmlschema which converts to a string without the time
24
+ # component. This removal may generate an issue on FreeBSD, that's why we
25
+ # need to use remove_possible_method here
20
26
  remove_possible_method :xmlschema
21
27
 
22
28
  # Convert to a formatted string. See DATE_FORMATS for predefined formats.
23
29
  #
24
30
  # This method is aliased to <tt>to_s</tt>.
25
31
  #
26
- # ==== Examples
27
32
  # date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
28
33
  #
29
34
  # date.to_formatted_s(:db) # => "2007-11-10"
30
35
  # date.to_s(:db) # => "2007-11-10"
31
36
  #
32
37
  # date.to_formatted_s(:short) # => "10 Nov"
38
+ # date.to_formatted_s(:number) # => "20071110"
33
39
  # date.to_formatted_s(:long) # => "November 10, 2007"
34
40
  # date.to_formatted_s(:long_ordinal) # => "November 10th, 2007"
35
41
  # date.to_formatted_s(:rfc822) # => "10 Nov 2007"
42
+ # date.to_formatted_s(:iso8601) # => "2007-11-10"
36
43
  #
37
- # == Adding your own time formats to to_formatted_s
44
+ # == Adding your own date formats to to_formatted_s
38
45
  # You can add your own formats to the Date::DATE_FORMATS hash.
39
46
  # Use the format name as the hash key and either a strftime string
40
47
  # or Proc instance that takes a date argument as the value.
41
48
  #
42
- # # config/initializers/time_formats.rb
43
- # Date::DATE_FORMATS[:month_and_year] = "%B %Y"
44
- # Date::DATE_FORMATS[:short_ordinal] = lambda { |date| date.strftime("%B #{date.day.ordinalize}") }
49
+ # # config/initializers/date_formats.rb
50
+ # Date::DATE_FORMATS[:month_and_year] = '%B %Y'
51
+ # Date::DATE_FORMATS[:short_ordinal] = ->(date) { date.strftime("%B #{date.day.ordinalize}") }
45
52
  def to_formatted_s(format = :default)
46
53
  if formatter = DATE_FORMATS[format]
47
54
  if formatter.respond_to?(:call)
@@ -58,49 +65,31 @@ class Date
58
65
 
59
66
  # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
60
67
  def readable_inspect
61
- strftime("%a, %d %b %Y")
68
+ strftime('%a, %d %b %Y')
62
69
  end
63
70
  alias_method :default_inspect, :inspect
64
71
  alias_method :inspect, :readable_inspect
65
72
 
66
- # A method to keep Time, Date and DateTime instances interchangeable on conversions.
67
- # In this case, it simply returns +self+.
68
- def to_date
69
- self
70
- end if RUBY_VERSION < '1.9'
71
-
72
73
  # Converts a Date instance to a Time, where the time is set to the beginning of the day.
73
74
  # The timezone can be either :local or :utc (default :local).
74
75
  #
75
- # ==== Examples
76
76
  # date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
77
77
  #
78
- # date.to_time # => Sat Nov 10 00:00:00 0800 2007
79
- # date.to_time(:local) # => Sat Nov 10 00:00:00 0800 2007
78
+ # date.to_time # => 2007-11-10 00:00:00 0800
79
+ # date.to_time(:local) # => 2007-11-10 00:00:00 0800
80
80
  #
81
- # date.to_time(:utc) # => Sat Nov 10 00:00:00 UTC 2007
81
+ # date.to_time(:utc) # => 2007-11-10 00:00:00 UTC
82
82
  def to_time(form = :local)
83
- ::Time.send("#{form}_time", year, month, day)
83
+ raise ArgumentError, "Expected :local or :utc, got #{form.inspect}." unless [:local, :utc].include?(form)
84
+ ::Time.send(form, year, month, day)
84
85
  end
85
86
 
86
- # Converts a Date instance to a DateTime, where the time is set to the beginning of the day
87
- # and UTC offset is set to 0.
88
- #
89
- # ==== Examples
90
- # date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
87
+ # Returns a string which represents the time in used time zone as DateTime
88
+ # defined by XML Schema:
91
89
  #
92
- # date.to_datetime # => Sat, 10 Nov 2007 00:00:00 0000
93
- def to_datetime
94
- ::DateTime.civil(year, month, day, 0, 0, 0, 0)
95
- end if RUBY_VERSION < '1.9'
96
-
97
- def iso8601
98
- strftime('%F')
99
- end if RUBY_VERSION < '1.9'
100
-
101
- alias_method :rfc3339, :iso8601 if RUBY_VERSION < '1.9'
102
-
90
+ # date = Date.new(2015, 05, 23) # => Sat, 23 May 2015
91
+ # date.xmlschema # => "2015-05-23T00:00:00+04:00"
103
92
  def xmlschema
104
- to_time_in_current_zone.xmlschema
93
+ in_time_zone.xmlschema
105
94
  end
106
95
  end
@@ -1,14 +1,6 @@
1
1
  require 'date'
2
- require 'active_support/core_ext/time/zones'
2
+ require 'active_support/core_ext/date_and_time/zones'
3
3
 
4
4
  class Date
5
- # Converts Date to a TimeWithZone in the current zone if Time.zone or Time.zone_default
6
- # is set, otherwise converts Date to a Time via Date#to_time
7
- def to_time_in_current_zone
8
- if ::Time.zone
9
- ::Time.zone.local(year, month, day)
10
- else
11
- to_time
12
- end
13
- end
5
+ include DateAndTime::Zones
14
6
  end
@@ -0,0 +1,5 @@
1
+ require 'active_support/core_ext/date/acts_like'
2
+ require 'active_support/core_ext/date/blank'
3
+ require 'active_support/core_ext/date/calculations'
4
+ require 'active_support/core_ext/date/conversions'
5
+ require 'active_support/core_ext/date/zones'