csd 0.0.15 → 0.0.16

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 (256) hide show
  1. data/.gitignore +1 -0
  2. data/COPYING +367 -0
  3. data/Rakefile +10 -10
  4. data/VERSION +1 -1
  5. data/bin/ai +19 -0
  6. data/csd.gemspec +257 -35
  7. data/lib/active_support.rb +75 -0
  8. data/lib/active_support/all.rb +3 -0
  9. data/lib/active_support/backtrace_cleaner.rb +94 -0
  10. data/lib/active_support/base64.rb +42 -0
  11. data/lib/active_support/basic_object.rb +21 -0
  12. data/lib/active_support/benchmarkable.rb +60 -0
  13. data/lib/active_support/buffered_logger.rb +132 -0
  14. data/lib/active_support/builder.rb +6 -0
  15. data/lib/active_support/cache.rb +626 -0
  16. data/lib/active_support/cache/compressed_mem_cache_store.rb +13 -0
  17. data/lib/active_support/cache/file_store.rb +188 -0
  18. data/lib/active_support/cache/mem_cache_store.rb +191 -0
  19. data/lib/active_support/cache/memory_store.rb +159 -0
  20. data/lib/active_support/cache/strategy/local_cache.rb +164 -0
  21. data/lib/active_support/cache/synchronized_memory_store.rb +11 -0
  22. data/lib/active_support/callbacks.rb +600 -0
  23. data/lib/active_support/concern.rb +29 -0
  24. data/lib/active_support/configurable.rb +36 -0
  25. data/lib/active_support/core_ext.rb +3 -0
  26. data/lib/active_support/core_ext/array.rb +7 -0
  27. data/lib/active_support/core_ext/array/access.rb +46 -0
  28. data/lib/active_support/core_ext/array/conversions.rb +164 -0
  29. data/lib/active_support/core_ext/array/extract_options.rb +29 -0
  30. data/lib/active_support/core_ext/array/grouping.rb +100 -0
  31. data/lib/active_support/core_ext/array/random_access.rb +20 -0
  32. data/lib/active_support/core_ext/array/uniq_by.rb +17 -0
  33. data/lib/active_support/core_ext/array/wrap.rb +22 -0
  34. data/lib/active_support/core_ext/benchmark.rb +7 -0
  35. data/lib/active_support/core_ext/big_decimal.rb +1 -0
  36. data/lib/active_support/core_ext/big_decimal/conversions.rb +27 -0
  37. data/lib/active_support/core_ext/cgi.rb +1 -0
  38. data/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +19 -0
  39. data/lib/active_support/core_ext/class.rb +4 -0
  40. data/lib/active_support/core_ext/class/attribute.rb +67 -0
  41. data/lib/active_support/core_ext/class/attribute_accessors.rb +63 -0
  42. data/lib/active_support/core_ext/class/delegating_attributes.rb +44 -0
  43. data/lib/active_support/core_ext/class/inheritable_attributes.rb +232 -0
  44. data/lib/active_support/core_ext/class/subclasses.rb +55 -0
  45. data/lib/active_support/core_ext/date/acts_like.rb +8 -0
  46. data/lib/active_support/core_ext/date/calculations.rb +240 -0
  47. data/lib/active_support/core_ext/date/conversions.rb +99 -0
  48. data/lib/active_support/core_ext/date/freeze.rb +31 -0
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +13 -0
  50. data/lib/active_support/core_ext/date_time/calculations.rb +113 -0
  51. data/lib/active_support/core_ext/date_time/conversions.rb +102 -0
  52. data/lib/active_support/core_ext/date_time/zones.rb +17 -0
  53. data/lib/active_support/core_ext/enumerable.rb +119 -0
  54. data/lib/active_support/core_ext/exception.rb +3 -0
  55. data/lib/active_support/core_ext/file.rb +2 -0
  56. data/lib/active_support/core_ext/file/atomic.rb +41 -0
  57. data/lib/active_support/core_ext/file/path.rb +5 -0
  58. data/lib/active_support/core_ext/float.rb +1 -0
  59. data/lib/active_support/core_ext/float/rounding.rb +19 -0
  60. data/lib/active_support/core_ext/hash.rb +8 -0
  61. data/lib/active_support/core_ext/hash/conversions.rb +150 -0
  62. data/lib/active_support/core_ext/hash/deep_merge.rb +16 -0
  63. data/lib/active_support/core_ext/hash/diff.rb +13 -0
  64. data/lib/active_support/core_ext/hash/except.rb +24 -0
  65. data/lib/active_support/core_ext/hash/indifferent_access.rb +14 -0
  66. data/lib/active_support/core_ext/hash/keys.rb +45 -0
  67. data/lib/active_support/core_ext/hash/reverse_merge.rb +28 -0
  68. data/lib/active_support/core_ext/hash/slice.rb +38 -0
  69. data/lib/active_support/core_ext/integer.rb +3 -0
  70. data/lib/active_support/core_ext/integer/inflections.rb +14 -0
  71. data/lib/active_support/core_ext/integer/multiple.rb +6 -0
  72. data/lib/active_support/core_ext/integer/time.rb +39 -0
  73. data/lib/active_support/core_ext/kernel.rb +5 -0
  74. data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
  75. data/lib/active_support/core_ext/kernel/debugger.rb +16 -0
  76. data/lib/active_support/core_ext/kernel/reporting.rb +62 -0
  77. data/lib/active_support/core_ext/kernel/requires.rb +26 -0
  78. data/lib/active_support/core_ext/kernel/singleton_class.rb +13 -0
  79. data/lib/active_support/core_ext/load_error.rb +23 -0
  80. data/lib/active_support/core_ext/logger.rb +146 -0
  81. data/lib/active_support/core_ext/module.rb +12 -0
  82. data/lib/active_support/core_ext/module/aliasing.rb +70 -0
  83. data/lib/active_support/core_ext/module/anonymous.rb +24 -0
  84. data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +31 -0
  85. data/lib/active_support/core_ext/module/attr_internal.rb +32 -0
  86. data/lib/active_support/core_ext/module/attribute_accessors.rb +66 -0
  87. data/lib/active_support/core_ext/module/delegation.rb +146 -0
  88. data/lib/active_support/core_ext/module/deprecation.rb +9 -0
  89. data/lib/active_support/core_ext/module/introspection.rb +88 -0
  90. data/lib/active_support/core_ext/module/method_names.rb +14 -0
  91. data/lib/active_support/core_ext/module/reachable.rb +10 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +6 -0
  93. data/lib/active_support/core_ext/module/synchronization.rb +42 -0
  94. data/lib/active_support/core_ext/name_error.rb +18 -0
  95. data/lib/active_support/core_ext/numeric.rb +2 -0
  96. data/lib/active_support/core_ext/numeric/bytes.rb +44 -0
  97. data/lib/active_support/core_ext/numeric/time.rb +77 -0
  98. data/lib/active_support/core_ext/object.rb +14 -0
  99. data/lib/active_support/core_ext/object/acts_like.rb +10 -0
  100. data/lib/active_support/core_ext/object/blank.rb +76 -0
  101. data/lib/active_support/core_ext/object/conversions.rb +4 -0
  102. data/lib/active_support/core_ext/object/duplicable.rb +65 -0
  103. data/lib/active_support/core_ext/object/extending.rb +11 -0
  104. data/lib/active_support/core_ext/object/instance_variables.rb +67 -0
  105. data/lib/active_support/core_ext/object/misc.rb +2 -0
  106. data/lib/active_support/core_ext/object/returning.rb +42 -0
  107. data/lib/active_support/core_ext/object/to_param.rb +49 -0
  108. data/lib/active_support/core_ext/object/to_query.rb +27 -0
  109. data/lib/active_support/core_ext/object/try.rb +36 -0
  110. data/lib/active_support/core_ext/object/with_options.rb +26 -0
  111. data/lib/active_support/core_ext/proc.rb +14 -0
  112. data/lib/active_support/core_ext/process.rb +1 -0
  113. data/lib/active_support/core_ext/process/daemon.rb +23 -0
  114. data/lib/active_support/core_ext/range.rb +4 -0
  115. data/lib/active_support/core_ext/range/blockless_step.rb +29 -0
  116. data/lib/active_support/core_ext/range/conversions.rb +21 -0
  117. data/lib/active_support/core_ext/range/include_range.rb +21 -0
  118. data/lib/active_support/core_ext/range/overlaps.rb +8 -0
  119. data/lib/active_support/core_ext/regexp.rb +5 -0
  120. data/lib/active_support/core_ext/rexml.rb +46 -0
  121. data/lib/active_support/core_ext/string.rb +12 -0
  122. data/lib/active_support/core_ext/string/access.rb +99 -0
  123. data/lib/active_support/core_ext/string/behavior.rb +7 -0
  124. data/lib/active_support/core_ext/string/conversions.rb +61 -0
  125. data/lib/active_support/core_ext/string/encoding.rb +11 -0
  126. data/lib/active_support/core_ext/string/exclude.rb +6 -0
  127. data/lib/active_support/core_ext/string/filters.rb +49 -0
  128. data/lib/active_support/core_ext/string/inflections.rb +149 -0
  129. data/lib/active_support/core_ext/string/interpolation.rb +2 -0
  130. data/lib/active_support/core_ext/string/multibyte.rb +72 -0
  131. data/lib/active_support/core_ext/string/output_safety.rb +109 -0
  132. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
  133. data/lib/active_support/core_ext/string/xchar.rb +18 -0
  134. data/lib/active_support/core_ext/time/acts_like.rb +8 -0
  135. data/lib/active_support/core_ext/time/calculations.rb +282 -0
  136. data/lib/active_support/core_ext/time/conversions.rb +85 -0
  137. data/lib/active_support/core_ext/time/marshal.rb +56 -0
  138. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +10 -0
  139. data/lib/active_support/core_ext/time/zones.rb +78 -0
  140. data/lib/active_support/core_ext/uri.rb +22 -0
  141. data/lib/active_support/dependencies.rb +628 -0
  142. data/lib/active_support/dependencies/autoload.rb +50 -0
  143. data/lib/active_support/deprecation.rb +18 -0
  144. data/lib/active_support/deprecation/behaviors.rb +38 -0
  145. data/lib/active_support/deprecation/method_wrappers.rb +29 -0
  146. data/lib/active_support/deprecation/proxy_wrappers.rb +74 -0
  147. data/lib/active_support/deprecation/reporting.rb +56 -0
  148. data/lib/active_support/duration.rb +105 -0
  149. data/lib/active_support/gzip.rb +25 -0
  150. data/lib/active_support/hash_with_indifferent_access.rb +145 -0
  151. data/lib/active_support/i18n.rb +8 -0
  152. data/lib/active_support/inflections.rb +56 -0
  153. data/lib/active_support/inflector.rb +7 -0
  154. data/lib/active_support/inflector/inflections.rb +211 -0
  155. data/lib/active_support/inflector/methods.rb +141 -0
  156. data/lib/active_support/inflector/transliterate.rb +97 -0
  157. data/lib/active_support/json.rb +2 -0
  158. data/lib/active_support/json/backends/jsongem.rb +43 -0
  159. data/lib/active_support/json/backends/yajl.rb +40 -0
  160. data/lib/active_support/json/backends/yaml.rb +90 -0
  161. data/lib/active_support/json/decoding.rb +51 -0
  162. data/lib/active_support/json/encoding.rb +254 -0
  163. data/lib/active_support/json/variable.rb +11 -0
  164. data/lib/active_support/lazy_load_hooks.rb +27 -0
  165. data/lib/active_support/locale/en.yml +36 -0
  166. data/lib/active_support/memoizable.rb +103 -0
  167. data/lib/active_support/message_encryptor.rb +71 -0
  168. data/lib/active_support/message_verifier.rb +62 -0
  169. data/lib/active_support/multibyte.rb +44 -0
  170. data/lib/active_support/multibyte/chars.rb +480 -0
  171. data/lib/active_support/multibyte/exceptions.rb +8 -0
  172. data/lib/active_support/multibyte/unicode.rb +393 -0
  173. data/lib/active_support/multibyte/utils.rb +60 -0
  174. data/lib/active_support/notifications.rb +81 -0
  175. data/lib/active_support/notifications/fanout.rb +93 -0
  176. data/lib/active_support/notifications/instrumenter.rb +56 -0
  177. data/lib/active_support/option_merger.rb +25 -0
  178. data/lib/active_support/ordered_hash.rb +158 -0
  179. data/lib/active_support/ordered_options.rb +27 -0
  180. data/lib/active_support/railtie.rb +100 -0
  181. data/lib/active_support/rescuable.rb +114 -0
  182. data/lib/active_support/ruby/shim.rb +22 -0
  183. data/lib/active_support/secure_random.rb +199 -0
  184. data/lib/active_support/string_inquirer.rb +21 -0
  185. data/lib/active_support/test_case.rb +42 -0
  186. data/lib/active_support/testing/assertions.rb +82 -0
  187. data/lib/active_support/testing/declarative.rb +40 -0
  188. data/lib/active_support/testing/default.rb +9 -0
  189. data/lib/active_support/testing/deprecation.rb +55 -0
  190. data/lib/active_support/testing/isolation.rb +154 -0
  191. data/lib/active_support/testing/pending.rb +48 -0
  192. data/lib/active_support/testing/performance.rb +455 -0
  193. data/lib/active_support/testing/setup_and_teardown.rb +111 -0
  194. data/lib/active_support/time.rb +34 -0
  195. data/lib/active_support/time/autoload.rb +5 -0
  196. data/lib/active_support/time_with_zone.rb +341 -0
  197. data/lib/active_support/values/time_zone.rb +377 -0
  198. data/lib/active_support/values/unicode_tables.dat +0 -0
  199. data/lib/active_support/version.rb +10 -0
  200. data/lib/active_support/whiny_nil.rb +60 -0
  201. data/lib/active_support/xml_mini.rb +158 -0
  202. data/lib/active_support/xml_mini/jdom.rb +168 -0
  203. data/lib/active_support/xml_mini/libxml.rb +80 -0
  204. data/lib/active_support/xml_mini/libxmlsax.rb +85 -0
  205. data/lib/active_support/xml_mini/nokogiri.rb +78 -0
  206. data/lib/active_support/xml_mini/nokogirisax.rb +83 -0
  207. data/lib/active_support/xml_mini/rexml.rb +129 -0
  208. data/lib/csd.rb +82 -2
  209. data/lib/csd/application.rb +2 -0
  210. data/lib/csd/application/default.rb +51 -0
  211. data/lib/csd/application/default/base.rb +15 -0
  212. data/lib/csd/application/minisip.rb +25 -0
  213. data/lib/csd/application/minisip/about.yml +14 -0
  214. data/lib/csd/application/minisip/base.rb +161 -0
  215. data/lib/csd/application/minisip/error.rb +11 -0
  216. data/lib/csd/application/minisip/options/common.rb +0 -0
  217. data/lib/csd/application/minisip/options/compile.rb +59 -0
  218. data/lib/csd/{applications → application}/minisip/unix/base.rb +10 -11
  219. data/lib/csd/application/opensips/about.yml +2 -0
  220. data/lib/csd/applications.rb +55 -0
  221. data/lib/csd/commands.rb +88 -65
  222. data/lib/csd/error.rb +31 -0
  223. data/lib/csd/extensions.rb +1 -0
  224. data/lib/{extensions → csd/extensions}/core/array.rb +2 -2
  225. data/lib/csd/extensions/core/dir.rb +46 -0
  226. data/lib/{extensions → csd/extensions}/core/file.rb +2 -2
  227. data/lib/{extensions → csd/extensions}/core/object.rb +2 -2
  228. data/lib/csd/extensions/core/option_parser.rb +33 -0
  229. data/lib/{extensions → csd/extensions}/core/pathname.rb +12 -3
  230. data/lib/{extensions → csd/extensions}/core/string.rb +2 -2
  231. data/lib/{extensions → csd/extensions}/gem/platform.rb +6 -2
  232. data/lib/csd/global_open_struct.rb +18 -0
  233. data/lib/csd/options.rb +124 -95
  234. data/lib/csd/path.rb +31 -0
  235. data/lib/csd/ui.rb +1 -0
  236. data/lib/csd/ui/cli.rb +7 -0
  237. data/lib/csd/ui/ui.rb +46 -0
  238. data/lib/csd/version.rb +9 -0
  239. data/lib/term/ansicolor.rb +102 -0
  240. data/lib/term/ansicolor/.keep +0 -0
  241. data/lib/term/ansicolor/version.rb +10 -0
  242. data/test/functional/test_applications.rb +86 -0
  243. data/test/functional/test_commands.rb +42 -29
  244. data/test/functional/test_options.rb +98 -0
  245. data/test/helper.rb +14 -0
  246. data/test/unit/test_dir.rb +38 -0
  247. data/test/unit/test_pathname.rb +32 -0
  248. metadata +253 -40
  249. data/LICENSE +0 -20
  250. data/bin/csd +0 -8
  251. data/lib/csd/applications/base.rb +0 -33
  252. data/lib/csd/applications/minisip/base.rb +0 -125
  253. data/lib/csd/applications/minisip/init.rb +0 -20
  254. data/lib/csd/init.rb +0 -69
  255. data/lib/csd/path_container.rb +0 -15
  256. data/publish +0 -29
@@ -0,0 +1,4 @@
1
+ class String
2
+ alias_method :starts_with?, :start_with?
3
+ alias_method :ends_with?, :end_with?
4
+ end
@@ -0,0 +1,18 @@
1
+ begin
2
+ # See http://fast-xs.rubyforge.org/ by Eric Wong.
3
+ # Also included with hpricot.
4
+ require 'fast_xs'
5
+ rescue LoadError
6
+ # fast_xs extension unavailable
7
+ else
8
+ begin
9
+ require 'builder'
10
+ rescue LoadError
11
+ # builder demands the first shot at defining String#to_xs
12
+ end
13
+
14
+ class String
15
+ alias_method :original_xs, :to_xs if method_defined?(:to_xs)
16
+ alias_method :to_xs, :fast_xs
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_support/core_ext/object/acts_like'
2
+
3
+ class Time
4
+ # Duck-types as a Time-like class. See Object#acts_like?.
5
+ def acts_like_time?
6
+ true
7
+ end
8
+ end
@@ -0,0 +1,282 @@
1
+ require 'active_support/duration'
2
+ require 'active_support/core_ext/date/acts_like'
3
+ require 'active_support/core_ext/date/calculations'
4
+ require 'active_support/core_ext/date_time/conversions'
5
+
6
+ class Time
7
+ COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
8
+ DAYS_INTO_WEEK = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }
9
+
10
+ class << self
11
+ # Overriding case equality method so that it returns true for ActiveSupport::TimeWithZone instances
12
+ def ===(other)
13
+ other.is_a?(::Time)
14
+ end
15
+
16
+ # Return the number of days in the given month.
17
+ # If no year is specified, it will use the current year.
18
+ def days_in_month(month, year = now.year)
19
+ return 29 if month == 2 && ::Date.gregorian_leap?(year)
20
+ COMMON_YEAR_DAYS_IN_MONTH[month]
21
+ end
22
+
23
+ # Returns a new Time if requested year can be accommodated by Ruby's Time class
24
+ # (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture);
25
+ # otherwise returns a DateTime
26
+ def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
27
+ time = ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
28
+ # This check is needed because Time.utc(y) returns a time object in the 2000s for 0 <= y <= 138.
29
+ time.year == year ? time : ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
30
+ rescue
31
+ ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
32
+ end
33
+
34
+ # Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:utc</tt>.
35
+ def utc_time(*args)
36
+ time_with_datetime_fallback(:utc, *args)
37
+ end
38
+
39
+ # Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:local</tt>.
40
+ def local_time(*args)
41
+ time_with_datetime_fallback(:local, *args)
42
+ end
43
+ end
44
+
45
+ # Tells whether the Time object's time lies in the past
46
+ def past?
47
+ self < ::Time.current
48
+ end
49
+
50
+ # Tells whether the Time object's time is today
51
+ def today?
52
+ to_date == ::Date.current
53
+ end
54
+
55
+ # Tells whether the Time object's time lies in the future
56
+ def future?
57
+ self > ::Time.current
58
+ end
59
+
60
+ # Seconds since midnight: Time.now.seconds_since_midnight
61
+ def seconds_since_midnight
62
+ to_i - change(:hour => 0).to_i + (usec / 1.0e+6)
63
+ end
64
+
65
+ # Returns a new Time where one or more of the elements have been changed according to the +options+ parameter. The time options
66
+ # (hour, minute, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and
67
+ # minute is passed, then sec and usec is set to 0.
68
+ def change(options)
69
+ ::Time.send(
70
+ utc? ? :utc_time : :local_time,
71
+ options[:year] || year,
72
+ options[:month] || month,
73
+ options[:day] || day,
74
+ options[:hour] || hour,
75
+ options[:min] || (options[:hour] ? 0 : min),
76
+ options[:sec] || ((options[:hour] || options[:min]) ? 0 : sec),
77
+ options[:usec] || ((options[:hour] || options[:min] || options[:sec]) ? 0 : usec)
78
+ )
79
+ end
80
+
81
+ # Uses Date to provide precise Time calculations for years, months, and days.
82
+ # The +options+ parameter takes a hash with any of these keys: <tt>:years</tt>,
83
+ # <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>,
84
+ # <tt>:minutes</tt>, <tt>:seconds</tt>.
85
+ def advance(options)
86
+ unless options[:weeks].nil?
87
+ options[:weeks], partial_weeks = options[:weeks].divmod(1)
88
+ options[:days] = (options[:days] || 0) + 7 * partial_weeks
89
+ end
90
+
91
+ unless options[:days].nil?
92
+ options[:days], partial_days = options[:days].divmod(1)
93
+ options[:hours] = (options[:hours] || 0) + 24 * partial_days
94
+ end
95
+
96
+ d = to_date.advance(options)
97
+ time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
98
+ seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
99
+ seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance)
100
+ end
101
+
102
+ # Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension
103
+ def ago(seconds)
104
+ since(-seconds)
105
+ end
106
+
107
+ # Returns a new Time representing the time a number of seconds since the instance time
108
+ def since(seconds)
109
+ self + seconds
110
+ rescue
111
+ to_datetime.since(seconds)
112
+ end
113
+ alias :in :since
114
+
115
+ # Returns a new Time representing the time a number of specified months ago
116
+ def months_ago(months)
117
+ advance(:months => -months)
118
+ end
119
+
120
+ # Returns a new Time representing the time a number of specified months in the future
121
+ def months_since(months)
122
+ advance(:months => months)
123
+ end
124
+
125
+ # Returns a new Time representing the time a number of specified years ago
126
+ def years_ago(years)
127
+ advance(:years => -years)
128
+ end
129
+
130
+ # Returns a new Time representing the time a number of specified years in the future
131
+ def years_since(years)
132
+ advance(:years => years)
133
+ end
134
+
135
+ # Short-hand for years_ago(1)
136
+ def prev_year
137
+ years_ago(1)
138
+ end
139
+
140
+ # Short-hand for years_since(1)
141
+ def next_year
142
+ years_since(1)
143
+ end
144
+
145
+ # Short-hand for months_ago(1)
146
+ def prev_month
147
+ months_ago(1)
148
+ end
149
+
150
+ # Short-hand for months_since(1)
151
+ def next_month
152
+ months_since(1)
153
+ end
154
+
155
+ # Returns a new Time representing the "start" of this week (Monday, 0:00)
156
+ def beginning_of_week
157
+ days_to_monday = wday!=0 ? wday-1 : 6
158
+ (self - days_to_monday.days).midnight
159
+ end
160
+ alias :monday :beginning_of_week
161
+ alias :at_beginning_of_week :beginning_of_week
162
+
163
+ # Returns a new Time representing the end of this week, (end of Sunday)
164
+ def end_of_week
165
+ days_to_sunday = wday!=0 ? 7-wday : 0
166
+ (self + days_to_sunday.days).end_of_day
167
+ end
168
+ alias :at_end_of_week :end_of_week
169
+
170
+ # Returns a new Time representing the start of the given day in next week (default is Monday).
171
+ def next_week(day = :monday)
172
+ since(1.week).beginning_of_week.since(DAYS_INTO_WEEK[day].day).change(:hour => 0)
173
+ end
174
+
175
+ # Returns a new Time representing the start of the day (0:00)
176
+ def beginning_of_day
177
+ #(self - seconds_since_midnight).change(:usec => 0)
178
+ change(:hour => 0, :min => 0, :sec => 0, :usec => 0)
179
+ end
180
+ alias :midnight :beginning_of_day
181
+ alias :at_midnight :beginning_of_day
182
+ alias :at_beginning_of_day :beginning_of_day
183
+
184
+ # Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9)
185
+ def end_of_day
186
+ change(:hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
187
+ end
188
+
189
+ # Returns a new Time representing the start of the month (1st of the month, 0:00)
190
+ def beginning_of_month
191
+ #self - ((self.mday-1).days + self.seconds_since_midnight)
192
+ change(:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
193
+ end
194
+ alias :at_beginning_of_month :beginning_of_month
195
+
196
+ # Returns a new Time representing the end of the month (end of the last day of the month)
197
+ def end_of_month
198
+ #self - ((self.mday-1).days + self.seconds_since_midnight)
199
+ last_day = ::Time.days_in_month(month, year)
200
+ change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
201
+ end
202
+ alias :at_end_of_month :end_of_month
203
+
204
+ # Returns a new Time representing the start of the quarter (1st of january, april, july, october, 0:00)
205
+ def beginning_of_quarter
206
+ beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= month })
207
+ end
208
+ alias :at_beginning_of_quarter :beginning_of_quarter
209
+
210
+ # Returns a new Time representing the end of the quarter (end of the last day of march, june, september, december)
211
+ def end_of_quarter
212
+ beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= month }).end_of_month
213
+ end
214
+ alias :at_end_of_quarter :end_of_quarter
215
+
216
+ # Returns a new Time representing the start of the year (1st of january, 0:00)
217
+ def beginning_of_year
218
+ change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0, :usec => 0)
219
+ end
220
+ alias :at_beginning_of_year :beginning_of_year
221
+
222
+ # Returns a new Time representing the end of the year (end of the 31st of december)
223
+ def end_of_year
224
+ change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
225
+ end
226
+ alias :at_end_of_year :end_of_year
227
+
228
+ # Convenience method which returns a new Time representing the time 1 day ago
229
+ def yesterday
230
+ advance(:days => -1)
231
+ end
232
+
233
+ # Convenience method which returns a new Time representing the time 1 day since the instance time
234
+ def tomorrow
235
+ advance(:days => 1)
236
+ end
237
+
238
+ def plus_with_duration(other) #:nodoc:
239
+ if ActiveSupport::Duration === other
240
+ other.since(self)
241
+ else
242
+ plus_without_duration(other)
243
+ end
244
+ end
245
+ alias_method :plus_without_duration, :+
246
+ alias_method :+, :plus_with_duration
247
+
248
+ def minus_with_duration(other) #:nodoc:
249
+ if ActiveSupport::Duration === other
250
+ other.until(self)
251
+ else
252
+ minus_without_duration(other)
253
+ end
254
+ end
255
+ alias_method :minus_without_duration, :-
256
+ alias_method :-, :minus_with_duration
257
+
258
+ # Time#- can also be used to determine the number of seconds between two Time instances.
259
+ # We're layering on additional behavior so that ActiveSupport::TimeWithZone instances
260
+ # are coerced into values that Time#- will recognize
261
+ def minus_with_coercion(other)
262
+ other = other.comparable_time if other.respond_to?(:comparable_time)
263
+ other.is_a?(DateTime) ? to_f - other.to_f : minus_without_coercion(other)
264
+ end
265
+ alias_method :minus_without_coercion, :-
266
+ alias_method :-, :minus_with_coercion
267
+
268
+ # Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances
269
+ # can be chronologically compared with a Time
270
+ def compare_with_coercion(other)
271
+ # if other is an ActiveSupport::TimeWithZone, coerce a Time instance from it so we can do <=> comparison
272
+ other = other.comparable_time if other.respond_to?(:comparable_time)
273
+ if other.acts_like?(:date)
274
+ # other is a Date/DateTime, so coerce self #to_datetime and hand off to DateTime#<=>
275
+ to_datetime.compare_without_coercion(other)
276
+ else
277
+ compare_without_coercion(other)
278
+ end
279
+ end
280
+ alias_method :compare_without_coercion, :<=>
281
+ alias_method :<=>, :compare_with_coercion
282
+ end
@@ -0,0 +1,85 @@
1
+ require 'active_support/inflector'
2
+ require 'active_support/core_ext/time/publicize_conversion_methods'
3
+ require 'active_support/values/time_zone'
4
+
5
+ class Time
6
+ DATE_FORMATS = {
7
+ :db => "%Y-%m-%d %H:%M:%S",
8
+ :number => "%Y%m%d%H%M%S",
9
+ :time => "%H:%M",
10
+ :short => "%d %b %H:%M",
11
+ :long => "%B %d, %Y %H:%M",
12
+ :long_ordinal => lambda { |time| time.strftime("%B #{ActiveSupport::Inflector.ordinalize(time.day)}, %Y %H:%M") },
13
+ :rfc822 => lambda { |time| time.strftime("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") }
14
+ }
15
+
16
+ # Converts to a formatted string. See DATE_FORMATS for builtin formats.
17
+ #
18
+ # This method is aliased to <tt>to_s</tt>.
19
+ #
20
+ # time = Time.now # => Thu Jan 18 06:10:17 CST 2007
21
+ #
22
+ # time.to_formatted_s(:time) # => "06:10:17"
23
+ # time.to_s(:time) # => "06:10:17"
24
+ #
25
+ # time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
26
+ # time.to_formatted_s(:number) # => "20070118061017"
27
+ # time.to_formatted_s(:short) # => "18 Jan 06:10"
28
+ # time.to_formatted_s(:long) # => "January 18, 2007 06:10"
29
+ # time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
30
+ # time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
31
+ #
32
+ # == Adding your own time formats to +to_formatted_s+
33
+ # You can add your own formats to the Time::DATE_FORMATS hash.
34
+ # Use the format name as the hash key and either a strftime string
35
+ # or Proc instance that takes a time argument as the value.
36
+ #
37
+ # # config/initializers/time_formats.rb
38
+ # Time::DATE_FORMATS[:month_and_year] = "%B %Y"
39
+ # Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") }
40
+ def to_formatted_s(format = :default)
41
+ if formatter = DATE_FORMATS[format]
42
+ formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
43
+ else
44
+ to_default_s
45
+ end
46
+ end
47
+ alias_method :to_default_s, :to_s
48
+ alias_method :to_s, :to_formatted_s
49
+
50
+ # Returns the UTC offset as an +HH:MM formatted string.
51
+ #
52
+ # Time.local(2000).formatted_offset # => "-06:00"
53
+ # Time.local(2000).formatted_offset(false) # => "-0600"
54
+ def formatted_offset(colon = true, alternate_utc_string = nil)
55
+ utc? && alternate_utc_string || ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset, colon)
56
+ end
57
+
58
+ # Converts a Time object to a Date, dropping hour, minute, and second precision.
59
+ #
60
+ # my_time = Time.now # => Mon Nov 12 22:59:51 -0500 2007
61
+ # my_time.to_date # => Mon, 12 Nov 2007
62
+ #
63
+ # your_time = Time.parse("1/13/2009 1:13:03 P.M.") # => Tue Jan 13 13:13:03 -0500 2009
64
+ # your_time.to_date # => Tue, 13 Jan 2009
65
+ def to_date
66
+ ::Date.new(year, month, day)
67
+ end unless method_defined?(:to_date)
68
+
69
+ # A method to keep Time, Date and DateTime instances interchangeable on conversions.
70
+ # In this case, it simply returns +self+.
71
+ def to_time
72
+ self
73
+ end unless method_defined?(:to_time)
74
+
75
+ # Converts a Time instance to a Ruby DateTime instance, preserving UTC offset.
76
+ #
77
+ # my_time = Time.now # => Mon Nov 12 23:04:21 -0500 2007
78
+ # my_time.to_datetime # => Mon, 12 Nov 2007 23:04:21 -0500
79
+ #
80
+ # your_time = Time.parse("1/13/2009 1:13:03 P.M.") # => Tue Jan 13 13:13:03 -0500 2009
81
+ # your_time.to_datetime # => Tue, 13 Jan 2009 13:13:03 -0500
82
+ def to_datetime
83
+ ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400))
84
+ end unless method_defined?(:to_datetime)
85
+ end
@@ -0,0 +1,56 @@
1
+ # Pre-1.9 versions of Ruby have a bug with marshaling Time instances, where utc instances are
2
+ # unmarshalled in the local zone, instead of utc. We're layering behavior on the _dump and _load
3
+ # methods so that utc instances can be flagged on dump, and coerced back to utc on load.
4
+ if !Marshal.load(Marshal.dump(Time.now.utc)).utc?
5
+ class Time
6
+ class << self
7
+ alias_method :_load_without_utc_flag, :_load
8
+ def _load(marshaled_time)
9
+ time = _load_without_utc_flag(marshaled_time)
10
+ time.instance_eval do
11
+ if defined?(@marshal_with_utc_coercion)
12
+ val = remove_instance_variable("@marshal_with_utc_coercion")
13
+ end
14
+ val ? utc : self
15
+ end
16
+ end
17
+ end
18
+
19
+ alias_method :_dump_without_utc_flag, :_dump
20
+ def _dump(*args)
21
+ obj = dup
22
+ obj.instance_variable_set('@marshal_with_utc_coercion', utc?)
23
+ obj._dump_without_utc_flag(*args)
24
+ end
25
+ end
26
+ end
27
+
28
+ # Ruby 1.9.2 adds utc_offset and zone to Time, but marshaling only
29
+ # preserves utc_offset. Preserve zone also, even though it may not
30
+ # work in some edge cases.
31
+ if Time.local(2010).zone != Marshal.load(Marshal.dump(Time.local(2010))).zone
32
+ class Time
33
+ class << self
34
+ alias_method :_load_without_zone, :_load
35
+ def _load(marshaled_time)
36
+ time = _load_without_zone(marshaled_time)
37
+ time.instance_eval do
38
+ if zone = defined?(@_zone) && remove_instance_variable('@_zone')
39
+ ary = to_a
40
+ ary[-1] = zone
41
+ utc? ? Time.utc(*ary) : Time.local(*ary)
42
+ else
43
+ self
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ alias_method :_dump_without_zone, :_dump
50
+ def _dump(*args)
51
+ obj = dup
52
+ obj.instance_variable_set('@_zone', zone)
53
+ obj._dump_without_zone(*args)
54
+ end
55
+ end
56
+ end