activesupport 3.2.22.5 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +364 -135
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +6 -4
  5. data/lib/active_support/backtrace_cleaner.rb +33 -25
  6. data/lib/active_support/basic_object.rb +7 -17
  7. data/lib/active_support/benchmarkable.rb +19 -15
  8. data/lib/active_support/buffered_logger.rb +9 -113
  9. data/lib/active_support/cache/file_store.rb +14 -14
  10. data/lib/active_support/cache/mem_cache_store.rb +24 -30
  11. data/lib/active_support/cache/memory_store.rb +2 -0
  12. data/lib/active_support/cache/strategy/local_cache.rb +48 -37
  13. data/lib/active_support/cache.rb +228 -183
  14. data/lib/active_support/callbacks.rb +201 -253
  15. data/lib/active_support/concern.rb +16 -23
  16. data/lib/active_support/concurrency/latch.rb +27 -0
  17. data/lib/active_support/configurable.rb +69 -12
  18. data/lib/active_support/core_ext/array/access.rb +17 -9
  19. data/lib/active_support/core_ext/array/conversions.rb +115 -55
  20. data/lib/active_support/core_ext/array/extract_options.rb +2 -2
  21. data/lib/active_support/core_ext/array/grouping.rb +21 -22
  22. data/lib/active_support/core_ext/array/uniq_by.rb +12 -9
  23. data/lib/active_support/core_ext/array/wrap.rb +13 -16
  24. data/lib/active_support/core_ext/array.rb +0 -1
  25. data/lib/active_support/core_ext/benchmark.rb +7 -0
  26. data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -24
  27. data/lib/active_support/core_ext/class/attribute.rb +40 -32
  28. data/lib/active_support/core_ext/class/attribute_accessors.rb +14 -12
  29. data/lib/active_support/core_ext/class/delegating_attributes.rb +15 -19
  30. data/lib/active_support/core_ext/class/subclasses.rb +11 -5
  31. data/lib/active_support/core_ext/date/calculations.rb +44 -187
  32. data/lib/active_support/core_ext/date/conversions.rb +16 -38
  33. data/lib/active_support/core_ext/date/zones.rb +25 -2
  34. data/lib/active_support/core_ext/date.rb +5 -0
  35. data/lib/active_support/core_ext/date_and_time/calculations.rb +232 -0
  36. data/lib/active_support/core_ext/date_time/acts_like.rb +0 -1
  37. data/lib/active_support/core_ext/date_time/calculations.rb +77 -62
  38. data/lib/active_support/core_ext/date_time/conversions.rb +21 -33
  39. data/lib/active_support/core_ext/date_time/zones.rb +11 -8
  40. data/lib/active_support/core_ext/date_time.rb +4 -0
  41. data/lib/active_support/core_ext/enumerable.rb +26 -73
  42. data/lib/active_support/core_ext/file/atomic.rb +27 -11
  43. data/lib/active_support/core_ext/file.rb +0 -1
  44. data/lib/active_support/core_ext/hash/conversions.rb +145 -79
  45. data/lib/active_support/core_ext/hash/deep_merge.rb +14 -8
  46. data/lib/active_support/core_ext/hash/diff.rb +5 -4
  47. data/lib/active_support/core_ext/hash/except.rb +1 -9
  48. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -5
  49. data/lib/active_support/core_ext/hash/keys.rb +108 -24
  50. data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
  51. data/lib/active_support/core_ext/hash/slice.rb +12 -12
  52. data/lib/active_support/core_ext/hash.rb +0 -1
  53. data/lib/active_support/core_ext/integer/inflections.rb +13 -1
  54. data/lib/active_support/core_ext/integer/time.rb +17 -12
  55. data/lib/active_support/core_ext/kernel/debugger.rb +2 -2
  56. data/lib/active_support/core_ext/kernel/reporting.rb +36 -22
  57. data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
  58. data/lib/active_support/core_ext/load_error.rb +7 -5
  59. data/lib/active_support/core_ext/logger.rb +7 -23
  60. data/lib/active_support/core_ext/marshal.rb +21 -0
  61. data/lib/active_support/core_ext/module/aliasing.rb +8 -9
  62. data/lib/active_support/core_ext/module/anonymous.rb +2 -7
  63. data/lib/active_support/core_ext/module/attr_internal.rb +0 -1
  64. data/lib/active_support/core_ext/module/attribute_accessors.rb +12 -10
  65. data/lib/active_support/core_ext/module/delegation.rb +92 -49
  66. data/lib/active_support/core_ext/module/deprecation.rb +19 -3
  67. data/lib/active_support/core_ext/module/introspection.rb +17 -27
  68. data/lib/active_support/core_ext/module/qualified_const.rb +8 -20
  69. data/lib/active_support/core_ext/module/remove_method.rb +1 -5
  70. data/lib/active_support/core_ext/module.rb +1 -3
  71. data/lib/active_support/core_ext/numeric/conversions.rb +135 -0
  72. data/lib/active_support/core_ext/numeric/time.rb +6 -6
  73. data/lib/active_support/core_ext/numeric.rb +1 -0
  74. data/lib/active_support/core_ext/object/acts_like.rb +4 -4
  75. data/lib/active_support/core_ext/object/blank.rb +7 -23
  76. data/lib/active_support/core_ext/object/deep_dup.rb +46 -0
  77. data/lib/active_support/core_ext/object/duplicable.rb +1 -30
  78. data/lib/active_support/core_ext/object/inclusion.rb +8 -7
  79. data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
  80. data/lib/active_support/core_ext/object/to_json.rb +8 -0
  81. data/lib/active_support/core_ext/object/to_param.rb +5 -2
  82. data/lib/active_support/core_ext/object/try.rb +46 -25
  83. data/lib/active_support/core_ext/object/with_options.rb +7 -8
  84. data/lib/active_support/core_ext/object.rb +1 -0
  85. data/lib/active_support/core_ext/proc.rb +3 -0
  86. data/lib/active_support/core_ext/range/conversions.rb +0 -2
  87. data/lib/active_support/core_ext/range/include_range.rb +3 -1
  88. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  89. data/lib/active_support/core_ext/range.rb +0 -2
  90. data/lib/active_support/core_ext/string/access.rb +95 -90
  91. data/lib/active_support/core_ext/string/conversions.rb +27 -38
  92. data/lib/active_support/core_ext/string/encoding.rb +6 -9
  93. data/lib/active_support/core_ext/string/filters.rb +24 -18
  94. data/lib/active_support/core_ext/string/indent.rb +43 -0
  95. data/lib/active_support/core_ext/string/inflections.rb +70 -60
  96. data/lib/active_support/core_ext/string/inquiry.rb +2 -2
  97. data/lib/active_support/core_ext/string/multibyte.rb +41 -64
  98. data/lib/active_support/core_ext/string/output_safety.rb +59 -51
  99. data/lib/active_support/core_ext/string/zones.rb +13 -0
  100. data/lib/active_support/core_ext/string.rb +2 -3
  101. data/lib/active_support/core_ext/struct.rb +6 -0
  102. data/lib/active_support/core_ext/thread.rb +74 -0
  103. data/lib/active_support/core_ext/time/calculations.rb +108 -184
  104. data/lib/active_support/core_ext/time/conversions.rb +27 -51
  105. data/lib/active_support/core_ext/time/marshal.rb +0 -27
  106. data/lib/active_support/core_ext/time/zones.rb +27 -17
  107. data/lib/active_support/core_ext/time.rb +5 -0
  108. data/lib/active_support/core_ext/uri.rb +13 -17
  109. data/lib/active_support/core_ext.rb +3 -2
  110. data/lib/active_support/dependencies/autoload.rb +47 -20
  111. data/lib/active_support/dependencies.rb +160 -141
  112. data/lib/active_support/deprecation/behaviors.rb +44 -30
  113. data/lib/active_support/deprecation/instance_delegator.rb +24 -0
  114. data/lib/active_support/deprecation/method_wrappers.rb +33 -18
  115. data/lib/active_support/deprecation/proxy_wrappers.rb +58 -13
  116. data/lib/active_support/deprecation/reporting.rb +40 -11
  117. data/lib/active_support/deprecation.rb +39 -14
  118. data/lib/active_support/descendants_tracker.rb +34 -19
  119. data/lib/active_support/duration.rb +6 -8
  120. data/lib/active_support/file_update_checker.rb +63 -47
  121. data/lib/active_support/gzip.rb +11 -5
  122. data/lib/active_support/hash_with_indifferent_access.rb +127 -42
  123. data/lib/active_support/i18n.rb +4 -0
  124. data/lib/active_support/i18n_railtie.rb +5 -22
  125. data/lib/active_support/inflections.rb +14 -12
  126. data/lib/active_support/inflector/inflections.rb +108 -71
  127. data/lib/active_support/inflector/methods.rb +181 -160
  128. data/lib/active_support/inflector/transliterate.rb +16 -17
  129. data/lib/active_support/json/decoding.rb +18 -17
  130. data/lib/active_support/json/encoding.rb +93 -39
  131. data/lib/active_support/json/variable.rb +10 -1
  132. data/lib/active_support/key_generator.rb +75 -0
  133. data/lib/active_support/lazy_load_hooks.rb +21 -19
  134. data/lib/active_support/locale/en.yml +100 -3
  135. data/lib/active_support/log_subscriber/test_helper.rb +18 -15
  136. data/lib/active_support/log_subscriber.rb +35 -52
  137. data/lib/active_support/logger.rb +57 -0
  138. data/lib/active_support/logger_silence.rb +24 -0
  139. data/lib/active_support/message_encryptor.rb +38 -35
  140. data/lib/active_support/message_verifier.rb +9 -15
  141. data/lib/active_support/multibyte/chars.rb +80 -333
  142. data/lib/active_support/multibyte/unicode.rb +74 -64
  143. data/lib/active_support/multibyte.rb +5 -28
  144. data/lib/active_support/notifications/fanout.rb +112 -18
  145. data/lib/active_support/notifications/instrumenter.rb +33 -14
  146. data/lib/active_support/notifications.rb +79 -26
  147. data/lib/active_support/number_helper.rb +637 -0
  148. data/lib/active_support/ordered_hash.rb +8 -190
  149. data/lib/active_support/ordered_options.rb +21 -23
  150. data/lib/active_support/per_thread_registry.rb +52 -0
  151. data/lib/active_support/proxy_object.rb +13 -0
  152. data/lib/active_support/rails.rb +27 -0
  153. data/lib/active_support/railtie.rb +12 -32
  154. data/lib/active_support/rescuable.rb +9 -4
  155. data/lib/active_support/string_inquirer.rb +13 -8
  156. data/lib/active_support/subscriber.rb +93 -0
  157. data/lib/active_support/tagged_logging.rb +51 -73
  158. data/lib/active_support/test_case.rb +46 -17
  159. data/lib/active_support/testing/assertions.rb +56 -26
  160. data/lib/active_support/testing/autorun.rb +5 -0
  161. data/lib/active_support/testing/constant_lookup.rb +54 -0
  162. data/lib/active_support/testing/declarative.rb +1 -1
  163. data/lib/active_support/testing/deprecation.rb +0 -19
  164. data/lib/active_support/testing/isolation.rb +29 -58
  165. data/lib/active_support/testing/pending.rb +5 -43
  166. data/lib/active_support/testing/setup_and_teardown.rb +6 -92
  167. data/lib/active_support/testing/tagged_logging.rb +25 -0
  168. data/lib/active_support/time.rb +6 -21
  169. data/lib/active_support/time_with_zone.rb +80 -43
  170. data/lib/active_support/values/time_zone.rb +81 -60
  171. data/lib/active_support/values/unicode_tables.dat +0 -0
  172. data/lib/active_support/version.rb +7 -6
  173. data/lib/active_support/xml_mini/jdom.rb +9 -11
  174. data/lib/active_support/xml_mini/libxml.rb +1 -2
  175. data/lib/active_support/xml_mini/libxmlsax.rb +2 -3
  176. data/lib/active_support/xml_mini/nokogiri.rb +1 -2
  177. data/lib/active_support/xml_mini/nokogirisax.rb +2 -3
  178. data/lib/active_support/xml_mini/rexml.rb +6 -8
  179. data/lib/active_support/xml_mini.rb +35 -17
  180. data/lib/active_support.rb +8 -21
  181. metadata +104 -72
  182. data/lib/active_support/base64.rb +0 -54
  183. data/lib/active_support/core_ext/array/random_access.rb +0 -30
  184. data/lib/active_support/core_ext/date/freeze.rb +0 -33
  185. data/lib/active_support/core_ext/exception.rb +0 -3
  186. data/lib/active_support/core_ext/file/path.rb +0 -5
  187. data/lib/active_support/core_ext/float/rounding.rb +0 -19
  188. data/lib/active_support/core_ext/float.rb +0 -1
  189. data/lib/active_support/core_ext/hash/deep_dup.rb +0 -18
  190. data/lib/active_support/core_ext/io.rb +0 -15
  191. data/lib/active_support/core_ext/module/method_names.rb +0 -14
  192. data/lib/active_support/core_ext/module/synchronization.rb +0 -45
  193. data/lib/active_support/core_ext/process/daemon.rb +0 -23
  194. data/lib/active_support/core_ext/process.rb +0 -1
  195. data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
  196. data/lib/active_support/core_ext/range/cover.rb +0 -3
  197. data/lib/active_support/core_ext/rexml.rb +0 -46
  198. data/lib/active_support/core_ext/string/interpolation.rb +0 -2
  199. data/lib/active_support/core_ext/string/xchar.rb +0 -18
  200. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
  201. data/lib/active_support/memoizable.rb +0 -116
  202. data/lib/active_support/multibyte/exceptions.rb +0 -8
  203. data/lib/active_support/multibyte/utils.rb +0 -60
  204. data/lib/active_support/ruby/shim.rb +0 -22
  205. data/lib/active_support/security_utils.rb +0 -27
  206. data/lib/active_support/testing/mochaing.rb +0 -7
  207. data/lib/active_support/testing/performance/jruby.rb +0 -115
  208. data/lib/active_support/testing/performance/rubinius.rb +0 -113
  209. data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
  210. data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
  211. data/lib/active_support/testing/performance/ruby.rb +0 -152
  212. data/lib/active_support/testing/performance.rb +0 -317
  213. data/lib/active_support/time/autoload.rb +0 -5
  214. data/lib/active_support/whiny_nil.rb +0 -24
@@ -9,12 +9,6 @@ module ActiveSupport
9
9
  included do
10
10
  include ActiveSupport::Callbacks
11
11
  define_callbacks :setup, :teardown
12
-
13
- if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions
14
- include ForMiniTest
15
- else
16
- include ForClassicTestUnit
17
- end
18
12
  end
19
13
 
20
14
  module ClassMethods
@@ -27,94 +21,14 @@ module ActiveSupport
27
21
  end
28
22
  end
29
23
 
30
- module ForMiniTest
31
- PASSTHROUGH_EXCEPTIONS = MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS rescue [NoMemoryError, SignalException, Interrupt, SystemExit]
32
- def run(runner)
33
- result = '.'
34
- begin
35
- run_callbacks :setup do
36
- result = super
37
- end
38
- rescue *PASSTHROUGH_EXCEPTIONS => e
39
- raise e
40
- rescue Exception => e
41
- result = runner.puke(self.class, method_name, e)
42
- ensure
43
- begin
44
- run_callbacks :teardown
45
- rescue *PASSTHROUGH_EXCEPTIONS => e
46
- raise e
47
- rescue Exception => e
48
- result = runner.puke(self.class, method_name, e)
49
- end
50
- end
51
- result
52
- end
24
+ def before_setup
25
+ super
26
+ run_callbacks :setup
53
27
  end
54
28
 
55
- module ForClassicTestUnit
56
- # For compatibility with Ruby < 1.8.6
57
- PASSTHROUGH_EXCEPTIONS = Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS rescue [NoMemoryError, SignalException, Interrupt, SystemExit]
58
-
59
- # This redefinition is unfortunate but test/unit shows us no alternative.
60
- # Doubly unfortunate: hax to support Mocha's hax.
61
- def run(result)
62
- return if @method_name.to_s == "default_test"
63
-
64
- mocha_counter = retrieve_mocha_counter(self, result)
65
- yield(Test::Unit::TestCase::STARTED, name)
66
- @_result = result
67
-
68
- begin
69
- begin
70
- run_callbacks :setup do
71
- setup
72
- __send__(@method_name)
73
- mocha_verify(mocha_counter) if mocha_counter
74
- end
75
- rescue Mocha::ExpectationError => e
76
- add_failure(e.message, e.backtrace)
77
- rescue Test::Unit::AssertionFailedError => e
78
- add_failure(e.message, e.backtrace)
79
- rescue Exception => e
80
- raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
81
- add_error(e)
82
- ensure
83
- begin
84
- teardown
85
- run_callbacks :teardown
86
- rescue Mocha::ExpectationError => e
87
- add_failure(e.message, e.backtrace)
88
- rescue Test::Unit::AssertionFailedError => e
89
- add_failure(e.message, e.backtrace)
90
- rescue Exception => e
91
- raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
92
- add_error(e)
93
- end
94
- end
95
- ensure
96
- mocha_teardown if mocha_counter
97
- end
98
-
99
- result.add_run
100
- yield(Test::Unit::TestCase::FINISHED, name)
101
- end
102
-
103
- protected
104
-
105
- def retrieve_mocha_counter(test_case, result) #:nodoc:
106
- if respond_to?(:mocha_verify) # using mocha
107
- if defined?(Mocha::TestCaseAdapter::AssertionCounter)
108
- Mocha::TestCaseAdapter::AssertionCounter.new(result)
109
- elsif defined?(Mocha::Integration::TestUnit::AssertionCounter)
110
- Mocha::Integration::TestUnit::AssertionCounter.new(result)
111
- elsif defined?(Mocha::MonkeyPatching::TestUnit::AssertionCounter)
112
- Mocha::MonkeyPatching::TestUnit::AssertionCounter.new(result)
113
- else
114
- Mocha::Integration::AssertionCounter.new(test_case)
115
- end
116
- end
117
- end
29
+ def after_teardown
30
+ run_callbacks :teardown
31
+ super
118
32
  end
119
33
  end
120
34
  end
@@ -0,0 +1,25 @@
1
+ module ActiveSupport
2
+ module Testing
3
+ # Logs a "PostsControllerTest: test name" heading before each test to
4
+ # make test.log easier to search and follow along with.
5
+ module TaggedLogging #:nodoc:
6
+ attr_writer :tagged_logger
7
+
8
+ def before_setup
9
+ if tagged_logger
10
+ heading = "#{self.class}: #{__name__}"
11
+ divider = '-' * heading.size
12
+ tagged_logger.info divider
13
+ tagged_logger.info heading
14
+ tagged_logger.info divider
15
+ end
16
+ super
17
+ end
18
+
19
+ private
20
+ def tagged_logger
21
+ @tagged_logger ||= (defined?(Rails.logger) && Rails.logger)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -4,32 +4,17 @@ module ActiveSupport
4
4
  autoload :Duration, 'active_support/duration'
5
5
  autoload :TimeWithZone, 'active_support/time_with_zone'
6
6
  autoload :TimeZone, 'active_support/values/time_zone'
7
-
8
- on_load_all do
9
- [Duration, TimeWithZone, TimeZone]
10
- end
11
7
  end
12
8
 
13
9
  require 'date'
14
10
  require 'time'
15
11
 
16
- require 'active_support/core_ext/time/publicize_conversion_methods'
17
- require 'active_support/core_ext/time/marshal'
18
- require 'active_support/core_ext/time/acts_like'
19
- require 'active_support/core_ext/time/calculations'
20
- require 'active_support/core_ext/time/conversions'
21
- require 'active_support/core_ext/time/zones'
22
-
23
- require 'active_support/core_ext/date/acts_like'
24
- require 'active_support/core_ext/date/freeze'
25
- require 'active_support/core_ext/date/calculations'
26
- require 'active_support/core_ext/date/conversions'
27
- require 'active_support/core_ext/date/zones'
28
-
29
- require 'active_support/core_ext/date_time/acts_like'
30
- require 'active_support/core_ext/date_time/calculations'
31
- require 'active_support/core_ext/date_time/conversions'
32
- require 'active_support/core_ext/date_time/zones'
12
+ require 'active_support/core_ext/time'
13
+ require 'active_support/core_ext/date'
14
+ require 'active_support/core_ext/date_time'
33
15
 
34
16
  require 'active_support/core_ext/integer/time'
35
17
  require 'active_support/core_ext/numeric/time'
18
+
19
+ require 'active_support/core_ext/string/conversions'
20
+ require 'active_support/core_ext/string/zones'
@@ -1,14 +1,14 @@
1
- require "active_support/values/time_zone"
1
+ require 'active_support/values/time_zone'
2
2
  require 'active_support/core_ext/object/acts_like'
3
- require 'active_support/core_ext/object/inclusion'
4
3
 
5
4
  module ActiveSupport
6
- # A Time-like class that can represent a time in any time zone. Necessary because standard Ruby Time instances are
7
- # limited to UTC and the system's <tt>ENV['TZ']</tt> zone.
5
+ # A Time-like class that can represent a time in any time zone. Necessary
6
+ # because standard Ruby Time instances are limited to UTC and the
7
+ # system's <tt>ENV['TZ']</tt> zone.
8
8
  #
9
- # You shouldn't ever need to create a TimeWithZone instance directly via <tt>new</tt> . Instead use methods
10
- # +local+, +parse+, +at+ and +now+ on TimeZone instances, and +in_time_zone+ on Time and DateTime instances.
11
- # Examples:
9
+ # You shouldn't ever need to create a TimeWithZone instance directly via +new+.
10
+ # Instead use methods +local+, +parse+, +at+ and +now+ on TimeZone instances,
11
+ # and +in_time_zone+ on Time and DateTime instances.
12
12
  #
13
13
  # Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
14
14
  # Time.zone.local(2007, 2, 10, 15, 30, 45) # => Sat, 10 Feb 2007 15:30:45 EST -05:00
@@ -19,8 +19,8 @@ module ActiveSupport
19
19
  #
20
20
  # See Time and TimeZone for further documentation of these methods.
21
21
  #
22
- # TimeWithZone instances implement the same API as Ruby Time instances, so that Time and TimeWithZone instances are interchangeable.
23
- # Examples:
22
+ # TimeWithZone instances implement the same API as Ruby Time instances, so
23
+ # that Time and TimeWithZone instances are interchangeable.
24
24
  #
25
25
  # t = Time.zone.now # => Sun, 18 May 2008 13:27:25 EDT -04:00
26
26
  # t.hour # => 13
@@ -33,10 +33,11 @@ module ActiveSupport
33
33
  # t > Time.utc(1999) # => true
34
34
  # t.is_a?(Time) # => true
35
35
  # t.is_a?(ActiveSupport::TimeWithZone) # => true
36
- #
37
36
  class TimeWithZone
37
+
38
+ # Report class name as 'Time' to thwart type checking.
38
39
  def self.name
39
- 'Time' # Report class name as 'Time' to thwart type checking
40
+ 'Time'
40
41
  end
41
42
 
42
43
  include Comparable
@@ -72,33 +73,56 @@ module ActiveSupport
72
73
  utc.in_time_zone(new_zone)
73
74
  end
74
75
 
75
- # Returns a <tt>Time.local()</tt> instance of the simultaneous time in your system's <tt>ENV['TZ']</tt> zone
76
+ # Returns a <tt>Time.local()</tt> instance of the simultaneous time in your
77
+ # system's <tt>ENV['TZ']</tt> zone.
76
78
  def localtime
77
79
  utc.respond_to?(:getlocal) ? utc.getlocal : utc.to_time.getlocal
78
80
  end
79
81
  alias_method :getlocal, :localtime
80
82
 
83
+ # Returns true if the current time is within Daylight Savings Time for the
84
+ # specified time zone.
85
+ #
86
+ # Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
87
+ # Time.zone.parse("2012-5-30").dst? # => true
88
+ # Time.zone.parse("2012-11-30").dst? # => false
81
89
  def dst?
82
90
  period.dst?
83
91
  end
84
92
  alias_method :isdst, :dst?
85
93
 
94
+ # Returns true if the current time zone is set to UTC.
95
+ #
96
+ # Time.zone = 'UTC' # => 'UTC'
97
+ # Time.zone.now.utc? # => true
98
+ # Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
99
+ # Time.zone.now.utc? # => false
86
100
  def utc?
87
101
  time_zone.name == 'UTC'
88
102
  end
89
103
  alias_method :gmt?, :utc?
90
104
 
105
+ # Returns the offset from current time to UTC time in seconds.
91
106
  def utc_offset
92
107
  period.utc_total_offset
93
108
  end
94
109
  alias_method :gmt_offset, :utc_offset
95
110
  alias_method :gmtoff, :utc_offset
96
111
 
112
+ # Returns a formatted string of the offset from UTC, or an alternative
113
+ # string if the time zone is already UTC.
114
+ #
115
+ # Time.zone = 'Eastern Time (US & Canada)' # => "Eastern Time (US & Canada)"
116
+ # Time.zone.now.formatted_offset(true) # => "-05:00"
117
+ # Time.zone.now.formatted_offset(false) # => "-0500"
118
+ # Time.zone = 'UTC' # => "UTC"
119
+ # Time.zone.now.formatted_offset(true, "0") # => "0"
97
120
  def formatted_offset(colon = true, alternate_utc_string = nil)
98
121
  utc? && alternate_utc_string || TimeZone.seconds_to_utc_offset(utc_offset, colon)
99
122
  end
100
123
 
101
- # Time uses +zone+ to display the time zone abbreviation, so we're duck-typing it.
124
+ # Time uses +zone+ to display the time zone abbreviation, so we're
125
+ # duck-typing it.
102
126
  def zone
103
127
  period.zone_identifier.to_s
104
128
  end
@@ -116,11 +140,10 @@ module ActiveSupport
116
140
  end
117
141
  alias_method :iso8601, :xmlschema
118
142
 
119
- # Coerces time to a string for JSON encoding. The default format is ISO 8601. You can get
120
- # %Y/%m/%d %H:%M:%S +offset style by setting <tt>ActiveSupport::JSON::Encoding.use_standard_json_time_format</tt>
121
- # to false.
122
- #
123
- # ==== Examples
143
+ # Coerces time to a string for JSON encoding. The default format is ISO 8601.
144
+ # You can get %Y/%m/%d %H:%M:%S +offset style by setting
145
+ # <tt>ActiveSupport::JSON::Encoding.use_standard_json_time_format</tt>
146
+ # to +false+.
124
147
  #
125
148
  # # With ActiveSupport::JSON::Encoding.use_standard_json_time_format = true
126
149
  # Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
@@ -129,10 +152,9 @@ module ActiveSupport
129
152
  # # With ActiveSupport::JSON::Encoding.use_standard_json_time_format = false
130
153
  # Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
131
154
  # # => "2005/02/01 15:15:10 +0000"
132
- #
133
155
  def as_json(options = nil)
134
156
  if ActiveSupport::JSON::Encoding.use_standard_json_time_format
135
- xmlschema
157
+ xmlschema(3)
136
158
  else
137
159
  %(#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
138
160
  end
@@ -146,16 +168,18 @@ module ActiveSupport
146
168
  end
147
169
  end
148
170
 
149
- def to_yaml(options = {})
150
- return super if defined?(YAML::ENGINE) && !YAML::ENGINE.syck?
151
-
152
- utc.to_yaml(options)
153
- end
154
-
171
+ # Returns a string of the object's date and time in the format used by
172
+ # HTTP requests.
173
+ #
174
+ # Time.zone.now.httpdate # => "Tue, 01 Jan 2013 04:39:43 GMT"
155
175
  def httpdate
156
176
  utc.httpdate
157
177
  end
158
178
 
179
+ # Returns a string of the object's date and time in the RFC 2822 standard
180
+ # format.
181
+ #
182
+ # Time.zone.now.rfc2822 # => "Tue, 01 Jan 2013 04:51:39 +0000"
159
183
  def rfc2822
160
184
  to_s(:rfc822)
161
185
  end
@@ -174,14 +198,14 @@ module ActiveSupport
174
198
  end
175
199
  alias_method :to_formatted_s, :to_s
176
200
 
177
- # Replaces <tt>%Z</tt> and <tt>%z</tt> directives with +zone+ and +formatted_offset+, respectively, before passing to
178
- # Time#strftime, so that zone information is correct
201
+ # Replaces <tt>%Z</tt> and <tt>%z</tt> directives with +zone+ and
202
+ # +formatted_offset+, respectively, before passing to Time#strftime, so
203
+ # that zone information is correct
179
204
  def strftime(format)
180
- format = format.gsub('%Z', zone).
181
- gsub('%z', formatted_offset(false)).
182
- gsub('%:z', formatted_offset(true)).
183
- gsub('%::z', formatted_offset(true) + ":00")
184
-
205
+ format = format.gsub('%Z', zone)
206
+ .gsub('%z', formatted_offset(false))
207
+ .gsub('%:z', formatted_offset(true))
208
+ .gsub('%::z', formatted_offset(true) + ":00")
185
209
  time.strftime(format)
186
210
  end
187
211
 
@@ -190,18 +214,24 @@ module ActiveSupport
190
214
  utc <=> other
191
215
  end
192
216
 
217
+ # Returns true if the current object's time is within the specified
218
+ # +min+ and +max+ time.
193
219
  def between?(min, max)
194
220
  utc.between?(min, max)
195
221
  end
196
222
 
223
+ # Returns true if the current object's time is in the past.
197
224
  def past?
198
225
  utc.past?
199
226
  end
200
227
 
228
+ # Returns true if the current object's time falls within
229
+ # the current day.
201
230
  def today?
202
231
  time.today?
203
232
  end
204
233
 
234
+ # Returns true if the current object's time is in the future.
205
235
  def future?
206
236
  utc.future?
207
237
  end
@@ -287,9 +317,13 @@ module ActiveSupport
287
317
  end
288
318
  alias_method :tv_sec, :to_i
289
319
 
290
- # A TimeWithZone acts like a Time, so just return +self+.
320
+ def to_r
321
+ utc.to_r
322
+ end
323
+
324
+ # Return an instance of Time in the system timezone.
291
325
  def to_time
292
- utc
326
+ utc.to_time
293
327
  end
294
328
 
295
329
  def to_datetime
@@ -320,16 +354,20 @@ module ActiveSupport
320
354
  initialize(variables[0].utc, ::Time.find_zone(variables[1]), variables[2].utc)
321
355
  end
322
356
 
323
- # Ensure proxy class responds to all methods that underlying time instance responds to.
324
- def respond_to?(sym, include_priv = false)
357
+ # Ensure proxy class responds to all methods that underlying time instance
358
+ # responds to.
359
+ def respond_to_missing?(sym, include_priv)
325
360
  # consistently respond false to acts_like?(:date), regardless of whether #time is a Time or DateTime
326
- return false if sym.to_s == 'acts_like_date?'
327
- super || time.respond_to?(sym, include_priv)
361
+ return false if sym.to_sym == :acts_like_date?
362
+ time.respond_to?(sym, include_priv)
328
363
  end
329
364
 
330
- # Send the missing method to +time+ instance, and wrap result in a new TimeWithZone with the existing +time_zone+.
365
+ # Send the missing method to +time+ instance, and wrap result in a new
366
+ # TimeWithZone with the existing +time_zone+.
331
367
  def method_missing(sym, *args, &block)
332
368
  wrap_with_time_zone time.__send__(sym, *args, &block)
369
+ rescue NoMethodError => e
370
+ raise e, e.message.sub(time.inspect, self.inspect), e.backtrace
333
371
  end
334
372
 
335
373
  private
@@ -347,12 +385,11 @@ module ActiveSupport
347
385
  end
348
386
 
349
387
  def transfer_time_values_to_utc_constructor(time)
350
- usec = time.respond_to?(:nsec) ? Rational(time.nsec, 1000) : (time.respond_to?(:usec) ? time.usec : 0)
351
- ::Time.utc_time(time.year, time.month, time.day, time.hour, time.min, time.sec, usec)
388
+ ::Time.utc(time.year, time.month, time.day, time.hour, time.min, time.sec, Rational(time.nsec, 1000))
352
389
  end
353
390
 
354
391
  def duration_of_variable_length?(obj)
355
- ActiveSupport::Duration === obj && obj.parts.any? {|p| p[0].in?([:years, :months, :days]) }
392
+ ActiveSupport::Duration === obj && obj.parts.any? {|p| [:years, :months, :days].include?(p[0]) }
356
393
  end
357
394
 
358
395
  def wrap_with_time_zone(time)