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.
- data/.gitignore +1 -0
- data/COPYING +367 -0
- data/Rakefile +10 -10
- data/VERSION +1 -1
- data/bin/ai +19 -0
- data/csd.gemspec +257 -35
- data/lib/active_support.rb +75 -0
- data/lib/active_support/all.rb +3 -0
- data/lib/active_support/backtrace_cleaner.rb +94 -0
- data/lib/active_support/base64.rb +42 -0
- data/lib/active_support/basic_object.rb +21 -0
- data/lib/active_support/benchmarkable.rb +60 -0
- data/lib/active_support/buffered_logger.rb +132 -0
- data/lib/active_support/builder.rb +6 -0
- data/lib/active_support/cache.rb +626 -0
- data/lib/active_support/cache/compressed_mem_cache_store.rb +13 -0
- data/lib/active_support/cache/file_store.rb +188 -0
- data/lib/active_support/cache/mem_cache_store.rb +191 -0
- data/lib/active_support/cache/memory_store.rb +159 -0
- data/lib/active_support/cache/strategy/local_cache.rb +164 -0
- data/lib/active_support/cache/synchronized_memory_store.rb +11 -0
- data/lib/active_support/callbacks.rb +600 -0
- data/lib/active_support/concern.rb +29 -0
- data/lib/active_support/configurable.rb +36 -0
- data/lib/active_support/core_ext.rb +3 -0
- data/lib/active_support/core_ext/array.rb +7 -0
- data/lib/active_support/core_ext/array/access.rb +46 -0
- data/lib/active_support/core_ext/array/conversions.rb +164 -0
- data/lib/active_support/core_ext/array/extract_options.rb +29 -0
- data/lib/active_support/core_ext/array/grouping.rb +100 -0
- data/lib/active_support/core_ext/array/random_access.rb +20 -0
- data/lib/active_support/core_ext/array/uniq_by.rb +17 -0
- data/lib/active_support/core_ext/array/wrap.rb +22 -0
- data/lib/active_support/core_ext/benchmark.rb +7 -0
- data/lib/active_support/core_ext/big_decimal.rb +1 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +27 -0
- data/lib/active_support/core_ext/cgi.rb +1 -0
- data/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +19 -0
- data/lib/active_support/core_ext/class.rb +4 -0
- data/lib/active_support/core_ext/class/attribute.rb +67 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +63 -0
- data/lib/active_support/core_ext/class/delegating_attributes.rb +44 -0
- data/lib/active_support/core_ext/class/inheritable_attributes.rb +232 -0
- data/lib/active_support/core_ext/class/subclasses.rb +55 -0
- data/lib/active_support/core_ext/date/acts_like.rb +8 -0
- data/lib/active_support/core_ext/date/calculations.rb +240 -0
- data/lib/active_support/core_ext/date/conversions.rb +99 -0
- data/lib/active_support/core_ext/date/freeze.rb +31 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +13 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +113 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +102 -0
- data/lib/active_support/core_ext/date_time/zones.rb +17 -0
- data/lib/active_support/core_ext/enumerable.rb +119 -0
- data/lib/active_support/core_ext/exception.rb +3 -0
- data/lib/active_support/core_ext/file.rb +2 -0
- data/lib/active_support/core_ext/file/atomic.rb +41 -0
- data/lib/active_support/core_ext/file/path.rb +5 -0
- data/lib/active_support/core_ext/float.rb +1 -0
- data/lib/active_support/core_ext/float/rounding.rb +19 -0
- data/lib/active_support/core_ext/hash.rb +8 -0
- data/lib/active_support/core_ext/hash/conversions.rb +150 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +16 -0
- data/lib/active_support/core_ext/hash/diff.rb +13 -0
- data/lib/active_support/core_ext/hash/except.rb +24 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +14 -0
- data/lib/active_support/core_ext/hash/keys.rb +45 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +28 -0
- data/lib/active_support/core_ext/hash/slice.rb +38 -0
- data/lib/active_support/core_ext/integer.rb +3 -0
- data/lib/active_support/core_ext/integer/inflections.rb +14 -0
- data/lib/active_support/core_ext/integer/multiple.rb +6 -0
- data/lib/active_support/core_ext/integer/time.rb +39 -0
- data/lib/active_support/core_ext/kernel.rb +5 -0
- data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +16 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +62 -0
- data/lib/active_support/core_ext/kernel/requires.rb +26 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +13 -0
- data/lib/active_support/core_ext/load_error.rb +23 -0
- data/lib/active_support/core_ext/logger.rb +146 -0
- data/lib/active_support/core_ext/module.rb +12 -0
- data/lib/active_support/core_ext/module/aliasing.rb +70 -0
- data/lib/active_support/core_ext/module/anonymous.rb +24 -0
- data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +31 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +32 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +66 -0
- data/lib/active_support/core_ext/module/delegation.rb +146 -0
- data/lib/active_support/core_ext/module/deprecation.rb +9 -0
- data/lib/active_support/core_ext/module/introspection.rb +88 -0
- data/lib/active_support/core_ext/module/method_names.rb +14 -0
- data/lib/active_support/core_ext/module/reachable.rb +10 -0
- data/lib/active_support/core_ext/module/remove_method.rb +6 -0
- data/lib/active_support/core_ext/module/synchronization.rb +42 -0
- data/lib/active_support/core_ext/name_error.rb +18 -0
- data/lib/active_support/core_ext/numeric.rb +2 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +44 -0
- data/lib/active_support/core_ext/numeric/time.rb +77 -0
- data/lib/active_support/core_ext/object.rb +14 -0
- data/lib/active_support/core_ext/object/acts_like.rb +10 -0
- data/lib/active_support/core_ext/object/blank.rb +76 -0
- data/lib/active_support/core_ext/object/conversions.rb +4 -0
- data/lib/active_support/core_ext/object/duplicable.rb +65 -0
- data/lib/active_support/core_ext/object/extending.rb +11 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +67 -0
- data/lib/active_support/core_ext/object/misc.rb +2 -0
- data/lib/active_support/core_ext/object/returning.rb +42 -0
- data/lib/active_support/core_ext/object/to_param.rb +49 -0
- data/lib/active_support/core_ext/object/to_query.rb +27 -0
- data/lib/active_support/core_ext/object/try.rb +36 -0
- data/lib/active_support/core_ext/object/with_options.rb +26 -0
- data/lib/active_support/core_ext/proc.rb +14 -0
- data/lib/active_support/core_ext/process.rb +1 -0
- data/lib/active_support/core_ext/process/daemon.rb +23 -0
- data/lib/active_support/core_ext/range.rb +4 -0
- data/lib/active_support/core_ext/range/blockless_step.rb +29 -0
- data/lib/active_support/core_ext/range/conversions.rb +21 -0
- data/lib/active_support/core_ext/range/include_range.rb +21 -0
- data/lib/active_support/core_ext/range/overlaps.rb +8 -0
- data/lib/active_support/core_ext/regexp.rb +5 -0
- data/lib/active_support/core_ext/rexml.rb +46 -0
- data/lib/active_support/core_ext/string.rb +12 -0
- data/lib/active_support/core_ext/string/access.rb +99 -0
- data/lib/active_support/core_ext/string/behavior.rb +7 -0
- data/lib/active_support/core_ext/string/conversions.rb +61 -0
- data/lib/active_support/core_ext/string/encoding.rb +11 -0
- data/lib/active_support/core_ext/string/exclude.rb +6 -0
- data/lib/active_support/core_ext/string/filters.rb +49 -0
- data/lib/active_support/core_ext/string/inflections.rb +149 -0
- data/lib/active_support/core_ext/string/interpolation.rb +2 -0
- data/lib/active_support/core_ext/string/multibyte.rb +72 -0
- data/lib/active_support/core_ext/string/output_safety.rb +109 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
- data/lib/active_support/core_ext/string/xchar.rb +18 -0
- data/lib/active_support/core_ext/time/acts_like.rb +8 -0
- data/lib/active_support/core_ext/time/calculations.rb +282 -0
- data/lib/active_support/core_ext/time/conversions.rb +85 -0
- data/lib/active_support/core_ext/time/marshal.rb +56 -0
- data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +10 -0
- data/lib/active_support/core_ext/time/zones.rb +78 -0
- data/lib/active_support/core_ext/uri.rb +22 -0
- data/lib/active_support/dependencies.rb +628 -0
- data/lib/active_support/dependencies/autoload.rb +50 -0
- data/lib/active_support/deprecation.rb +18 -0
- data/lib/active_support/deprecation/behaviors.rb +38 -0
- data/lib/active_support/deprecation/method_wrappers.rb +29 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +74 -0
- data/lib/active_support/deprecation/reporting.rb +56 -0
- data/lib/active_support/duration.rb +105 -0
- data/lib/active_support/gzip.rb +25 -0
- data/lib/active_support/hash_with_indifferent_access.rb +145 -0
- data/lib/active_support/i18n.rb +8 -0
- data/lib/active_support/inflections.rb +56 -0
- data/lib/active_support/inflector.rb +7 -0
- data/lib/active_support/inflector/inflections.rb +211 -0
- data/lib/active_support/inflector/methods.rb +141 -0
- data/lib/active_support/inflector/transliterate.rb +97 -0
- data/lib/active_support/json.rb +2 -0
- data/lib/active_support/json/backends/jsongem.rb +43 -0
- data/lib/active_support/json/backends/yajl.rb +40 -0
- data/lib/active_support/json/backends/yaml.rb +90 -0
- data/lib/active_support/json/decoding.rb +51 -0
- data/lib/active_support/json/encoding.rb +254 -0
- data/lib/active_support/json/variable.rb +11 -0
- data/lib/active_support/lazy_load_hooks.rb +27 -0
- data/lib/active_support/locale/en.yml +36 -0
- data/lib/active_support/memoizable.rb +103 -0
- data/lib/active_support/message_encryptor.rb +71 -0
- data/lib/active_support/message_verifier.rb +62 -0
- data/lib/active_support/multibyte.rb +44 -0
- data/lib/active_support/multibyte/chars.rb +480 -0
- data/lib/active_support/multibyte/exceptions.rb +8 -0
- data/lib/active_support/multibyte/unicode.rb +393 -0
- data/lib/active_support/multibyte/utils.rb +60 -0
- data/lib/active_support/notifications.rb +81 -0
- data/lib/active_support/notifications/fanout.rb +93 -0
- data/lib/active_support/notifications/instrumenter.rb +56 -0
- data/lib/active_support/option_merger.rb +25 -0
- data/lib/active_support/ordered_hash.rb +158 -0
- data/lib/active_support/ordered_options.rb +27 -0
- data/lib/active_support/railtie.rb +100 -0
- data/lib/active_support/rescuable.rb +114 -0
- data/lib/active_support/ruby/shim.rb +22 -0
- data/lib/active_support/secure_random.rb +199 -0
- data/lib/active_support/string_inquirer.rb +21 -0
- data/lib/active_support/test_case.rb +42 -0
- data/lib/active_support/testing/assertions.rb +82 -0
- data/lib/active_support/testing/declarative.rb +40 -0
- data/lib/active_support/testing/default.rb +9 -0
- data/lib/active_support/testing/deprecation.rb +55 -0
- data/lib/active_support/testing/isolation.rb +154 -0
- data/lib/active_support/testing/pending.rb +48 -0
- data/lib/active_support/testing/performance.rb +455 -0
- data/lib/active_support/testing/setup_and_teardown.rb +111 -0
- data/lib/active_support/time.rb +34 -0
- data/lib/active_support/time/autoload.rb +5 -0
- data/lib/active_support/time_with_zone.rb +341 -0
- data/lib/active_support/values/time_zone.rb +377 -0
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +10 -0
- data/lib/active_support/whiny_nil.rb +60 -0
- data/lib/active_support/xml_mini.rb +158 -0
- data/lib/active_support/xml_mini/jdom.rb +168 -0
- data/lib/active_support/xml_mini/libxml.rb +80 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +85 -0
- data/lib/active_support/xml_mini/nokogiri.rb +78 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +83 -0
- data/lib/active_support/xml_mini/rexml.rb +129 -0
- data/lib/csd.rb +82 -2
- data/lib/csd/application.rb +2 -0
- data/lib/csd/application/default.rb +51 -0
- data/lib/csd/application/default/base.rb +15 -0
- data/lib/csd/application/minisip.rb +25 -0
- data/lib/csd/application/minisip/about.yml +14 -0
- data/lib/csd/application/minisip/base.rb +161 -0
- data/lib/csd/application/minisip/error.rb +11 -0
- data/lib/csd/application/minisip/options/common.rb +0 -0
- data/lib/csd/application/minisip/options/compile.rb +59 -0
- data/lib/csd/{applications → application}/minisip/unix/base.rb +10 -11
- data/lib/csd/application/opensips/about.yml +2 -0
- data/lib/csd/applications.rb +55 -0
- data/lib/csd/commands.rb +88 -65
- data/lib/csd/error.rb +31 -0
- data/lib/csd/extensions.rb +1 -0
- data/lib/{extensions → csd/extensions}/core/array.rb +2 -2
- data/lib/csd/extensions/core/dir.rb +46 -0
- data/lib/{extensions → csd/extensions}/core/file.rb +2 -2
- data/lib/{extensions → csd/extensions}/core/object.rb +2 -2
- data/lib/csd/extensions/core/option_parser.rb +33 -0
- data/lib/{extensions → csd/extensions}/core/pathname.rb +12 -3
- data/lib/{extensions → csd/extensions}/core/string.rb +2 -2
- data/lib/{extensions → csd/extensions}/gem/platform.rb +6 -2
- data/lib/csd/global_open_struct.rb +18 -0
- data/lib/csd/options.rb +124 -95
- data/lib/csd/path.rb +31 -0
- data/lib/csd/ui.rb +1 -0
- data/lib/csd/ui/cli.rb +7 -0
- data/lib/csd/ui/ui.rb +46 -0
- data/lib/csd/version.rb +9 -0
- data/lib/term/ansicolor.rb +102 -0
- data/lib/term/ansicolor/.keep +0 -0
- data/lib/term/ansicolor/version.rb +10 -0
- data/test/functional/test_applications.rb +86 -0
- data/test/functional/test_commands.rb +42 -29
- data/test/functional/test_options.rb +98 -0
- data/test/helper.rb +14 -0
- data/test/unit/test_dir.rb +38 -0
- data/test/unit/test_pathname.rb +32 -0
- metadata +253 -40
- data/LICENSE +0 -20
- data/bin/csd +0 -8
- data/lib/csd/applications/base.rb +0 -33
- data/lib/csd/applications/minisip/base.rb +0 -125
- data/lib/csd/applications/minisip/init.rb +0 -20
- data/lib/csd/init.rb +0 -69
- data/lib/csd/path_container.rb +0 -15
- data/publish +0 -29
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'active_support/core_ext/module/anonymous'
|
2
|
+
require 'active_support/core_ext/module/reachable'
|
3
|
+
|
4
|
+
class Class #:nodoc:
|
5
|
+
# Returns an array with the names of the subclasses of +self+ as strings.
|
6
|
+
#
|
7
|
+
# Integer.subclasses # => ["Bignum", "Fixnum"]
|
8
|
+
def subclasses
|
9
|
+
Class.subclasses_of(self).map { |o| o.to_s }
|
10
|
+
end
|
11
|
+
|
12
|
+
# Rubinius
|
13
|
+
if defined?(Class.__subclasses__)
|
14
|
+
def descendents
|
15
|
+
subclasses = []
|
16
|
+
__subclasses__.each {|k| subclasses << k; subclasses.concat k.descendents }
|
17
|
+
subclasses
|
18
|
+
end
|
19
|
+
else
|
20
|
+
# MRI
|
21
|
+
begin
|
22
|
+
ObjectSpace.each_object(Class.new) {}
|
23
|
+
|
24
|
+
def descendents
|
25
|
+
subclasses = []
|
26
|
+
ObjectSpace.each_object(class << self; self; end) do |k|
|
27
|
+
subclasses << k unless k == self
|
28
|
+
end
|
29
|
+
subclasses
|
30
|
+
end
|
31
|
+
# JRuby
|
32
|
+
rescue StandardError
|
33
|
+
def descendents
|
34
|
+
subclasses = []
|
35
|
+
ObjectSpace.each_object(Class) do |k|
|
36
|
+
subclasses << k if k < self
|
37
|
+
end
|
38
|
+
subclasses.uniq!
|
39
|
+
subclasses
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Exclude this class unless it's a subclass of our supers and is defined.
|
45
|
+
# We check defined? in case we find a removed class that has yet to be
|
46
|
+
# garbage collected. This also fails for anonymous classes -- please
|
47
|
+
# submit a patch if you have a workaround.
|
48
|
+
def self.subclasses_of(*superclasses) #:nodoc:
|
49
|
+
subclasses = []
|
50
|
+
superclasses.each do |klass|
|
51
|
+
subclasses.concat klass.descendents.select {|k| k.anonymous? || k.reachable?}
|
52
|
+
end
|
53
|
+
subclasses
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,240 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'active_support/duration'
|
3
|
+
require 'active_support/core_ext/time/zones'
|
4
|
+
require 'active_support/core_ext/object/acts_like'
|
5
|
+
|
6
|
+
class Date
|
7
|
+
if RUBY_VERSION < '1.9'
|
8
|
+
undef :>>
|
9
|
+
|
10
|
+
# Backported from 1.9. The one in 1.8 leads to incorrect next_month and
|
11
|
+
# friends for dates where the calendar reform is involved. It additionally
|
12
|
+
# prevents an infinite loop fixed in r27013.
|
13
|
+
def >>(n)
|
14
|
+
y, m = (year * 12 + (mon - 1) + n).divmod(12)
|
15
|
+
m, = (m + 1) .divmod(1)
|
16
|
+
d = mday
|
17
|
+
until jd2 = self.class.valid_civil?(y, m, d, start)
|
18
|
+
d -= 1
|
19
|
+
raise ArgumentError, 'invalid date' unless d > 0
|
20
|
+
end
|
21
|
+
self + (jd2 - jd)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class << self
|
26
|
+
# Returns a new Date representing the date 1 day ago (i.e. yesterday's date).
|
27
|
+
def yesterday
|
28
|
+
::Date.current.yesterday
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns a new Date representing the date 1 day after today (i.e. tomorrow's date).
|
32
|
+
def tomorrow
|
33
|
+
::Date.current.tomorrow
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today.
|
37
|
+
def current
|
38
|
+
::Time.zone_default ? ::Time.zone.today : ::Date.today
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Tells whether the Date object's date lies in the past
|
43
|
+
def past?
|
44
|
+
self < ::Date.current
|
45
|
+
end
|
46
|
+
|
47
|
+
# Tells whether the Date object's date is today
|
48
|
+
def today?
|
49
|
+
self.to_date == ::Date.current # we need the to_date because of DateTime
|
50
|
+
end
|
51
|
+
|
52
|
+
# Tells whether the Date object's date lies in the future
|
53
|
+
def future?
|
54
|
+
self > ::Date.current
|
55
|
+
end
|
56
|
+
|
57
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
58
|
+
# and then subtracts the specified number of seconds
|
59
|
+
def ago(seconds)
|
60
|
+
to_time.since(-seconds)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
64
|
+
# and then adds the specified number of seconds
|
65
|
+
def since(seconds)
|
66
|
+
to_time.since(seconds)
|
67
|
+
end
|
68
|
+
alias :in :since
|
69
|
+
|
70
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
71
|
+
def beginning_of_day
|
72
|
+
to_time
|
73
|
+
end
|
74
|
+
alias :midnight :beginning_of_day
|
75
|
+
alias :at_midnight :beginning_of_day
|
76
|
+
alias :at_beginning_of_day :beginning_of_day
|
77
|
+
|
78
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
|
79
|
+
def end_of_day
|
80
|
+
to_time.end_of_day
|
81
|
+
end
|
82
|
+
|
83
|
+
def plus_with_duration(other) #:nodoc:
|
84
|
+
if ActiveSupport::Duration === other
|
85
|
+
other.since(self)
|
86
|
+
else
|
87
|
+
plus_without_duration(other)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
alias_method :plus_without_duration, :+
|
91
|
+
alias_method :+, :plus_with_duration
|
92
|
+
|
93
|
+
def minus_with_duration(other) #:nodoc:
|
94
|
+
if ActiveSupport::Duration === other
|
95
|
+
plus_with_duration(-other)
|
96
|
+
else
|
97
|
+
minus_without_duration(other)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
alias_method :minus_without_duration, :-
|
101
|
+
alias_method :-, :minus_with_duration
|
102
|
+
|
103
|
+
# Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
|
104
|
+
# any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
|
105
|
+
def advance(options)
|
106
|
+
options = options.dup
|
107
|
+
d = self
|
108
|
+
d = d >> options.delete(:years) * 12 if options[:years]
|
109
|
+
d = d >> options.delete(:months) if options[:months]
|
110
|
+
d = d + options.delete(:weeks) * 7 if options[:weeks]
|
111
|
+
d = d + options.delete(:days) if options[:days]
|
112
|
+
d
|
113
|
+
end
|
114
|
+
|
115
|
+
# Returns a new Date where one or more of the elements have been changed according to the +options+ parameter.
|
116
|
+
#
|
117
|
+
# Examples:
|
118
|
+
#
|
119
|
+
# Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 1)
|
120
|
+
# Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12)
|
121
|
+
def change(options)
|
122
|
+
::Date.new(
|
123
|
+
options[:year] || self.year,
|
124
|
+
options[:month] || self.month,
|
125
|
+
options[:day] || self.day
|
126
|
+
)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Returns a new Date/DateTime representing the time a number of specified months ago
|
130
|
+
def months_ago(months)
|
131
|
+
advance(:months => -months)
|
132
|
+
end
|
133
|
+
|
134
|
+
# Returns a new Date/DateTime representing the time a number of specified months in the future
|
135
|
+
def months_since(months)
|
136
|
+
advance(:months => months)
|
137
|
+
end
|
138
|
+
|
139
|
+
# Returns a new Date/DateTime representing the time a number of specified years ago
|
140
|
+
def years_ago(years)
|
141
|
+
advance(:years => -years)
|
142
|
+
end
|
143
|
+
|
144
|
+
# Returns a new Date/DateTime representing the time a number of specified years in the future
|
145
|
+
def years_since(years)
|
146
|
+
advance(:years => years)
|
147
|
+
end
|
148
|
+
|
149
|
+
# Shorthand for years_ago(1)
|
150
|
+
def prev_year
|
151
|
+
years_ago(1)
|
152
|
+
end unless method_defined?(:prev_year)
|
153
|
+
|
154
|
+
# Short-hand for years_since(1)
|
155
|
+
def next_year
|
156
|
+
years_since(1)
|
157
|
+
end unless method_defined?(:next_year)
|
158
|
+
|
159
|
+
# Short-hand for months_ago(1)
|
160
|
+
def prev_month
|
161
|
+
months_ago(1)
|
162
|
+
end unless method_defined?(:prev_month)
|
163
|
+
|
164
|
+
# Short-hand for months_since(1)
|
165
|
+
def next_month
|
166
|
+
months_since(1)
|
167
|
+
end unless method_defined?(:next_month)
|
168
|
+
|
169
|
+
# Returns a new Date/DateTime representing the "start" of this week (i.e, Monday; DateTime objects will have time set to 0:00)
|
170
|
+
def beginning_of_week
|
171
|
+
days_to_monday = self.wday!=0 ? self.wday-1 : 6
|
172
|
+
result = self - days_to_monday
|
173
|
+
self.acts_like?(:time) ? result.midnight : result
|
174
|
+
end
|
175
|
+
alias :monday :beginning_of_week
|
176
|
+
alias :at_beginning_of_week :beginning_of_week
|
177
|
+
|
178
|
+
# Returns a new Date/DateTime representing the end of this week (Sunday, DateTime objects will have time set to 23:59:59)
|
179
|
+
def end_of_week
|
180
|
+
days_to_sunday = self.wday!=0 ? 7-self.wday : 0
|
181
|
+
result = self + days_to_sunday.days
|
182
|
+
self.acts_like?(:time) ? result.end_of_day : result
|
183
|
+
end
|
184
|
+
alias :sunday :end_of_week
|
185
|
+
alias :at_end_of_week :end_of_week
|
186
|
+
|
187
|
+
# Returns a new Date/DateTime representing the start of the given day in next week (default is Monday).
|
188
|
+
def next_week(day = :monday)
|
189
|
+
days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
|
190
|
+
result = (self + 7).beginning_of_week + days_into_week[day]
|
191
|
+
self.acts_like?(:time) ? result.change(:hour => 0) : result
|
192
|
+
end
|
193
|
+
|
194
|
+
# 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)
|
195
|
+
def beginning_of_month
|
196
|
+
self.acts_like?(:time) ? change(:day => 1,:hour => 0, :min => 0, :sec => 0) : change(:day => 1)
|
197
|
+
end
|
198
|
+
alias :at_beginning_of_month :beginning_of_month
|
199
|
+
|
200
|
+
# 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)
|
201
|
+
def end_of_month
|
202
|
+
last_day = ::Time.days_in_month( self.month, self.year )
|
203
|
+
self.acts_like?(:time) ? change(:day => last_day, :hour => 23, :min => 59, :sec => 59) : change(:day => last_day)
|
204
|
+
end
|
205
|
+
alias :at_end_of_month :end_of_month
|
206
|
+
|
207
|
+
# 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)
|
208
|
+
def beginning_of_quarter
|
209
|
+
beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
|
210
|
+
end
|
211
|
+
alias :at_beginning_of_quarter :beginning_of_quarter
|
212
|
+
|
213
|
+
# 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)
|
214
|
+
def end_of_quarter
|
215
|
+
beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month
|
216
|
+
end
|
217
|
+
alias :at_end_of_quarter :end_of_quarter
|
218
|
+
|
219
|
+
# Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00)
|
220
|
+
def beginning_of_year
|
221
|
+
self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0) : change(:month => 1, :day => 1)
|
222
|
+
end
|
223
|
+
alias :at_beginning_of_year :beginning_of_year
|
224
|
+
|
225
|
+
# Returns a new Time representing the end of the year (31st of december; DateTime objects will have time set to 23:59:59)
|
226
|
+
def end_of_year
|
227
|
+
self.acts_like?(:time) ? change(:month => 12,:day => 31,:hour => 23, :min => 59, :sec => 59) : change(:month => 12, :day => 31)
|
228
|
+
end
|
229
|
+
alias :at_end_of_year :end_of_year
|
230
|
+
|
231
|
+
# Convenience method which returns a new Date/DateTime representing the time 1 day ago
|
232
|
+
def yesterday
|
233
|
+
self - 1
|
234
|
+
end
|
235
|
+
|
236
|
+
# Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time
|
237
|
+
def tomorrow
|
238
|
+
self + 1
|
239
|
+
end
|
240
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
require 'active_support/core_ext/time/calculations'
|
4
|
+
|
5
|
+
class Date
|
6
|
+
DATE_FORMATS = {
|
7
|
+
:short => "%e %b",
|
8
|
+
:long => "%B %e, %Y",
|
9
|
+
:db => "%Y-%m-%d",
|
10
|
+
:number => "%Y%m%d",
|
11
|
+
:long_ordinal => lambda { |date| date.strftime("%B #{ActiveSupport::Inflector.ordinalize(date.day)}, %Y") }, # => "April 25th, 2007"
|
12
|
+
:rfc822 => "%e %b %Y"
|
13
|
+
}
|
14
|
+
|
15
|
+
# Ruby 1.9 has Date#to_time which converts to localtime only.
|
16
|
+
remove_method :to_time if instance_methods.include?(:to_time)
|
17
|
+
|
18
|
+
# Ruby 1.9 has Date#xmlschema which converts to a string without the time component.
|
19
|
+
remove_method :xmlschema if instance_methods.include?(:xmlschema)
|
20
|
+
|
21
|
+
# Convert to a formatted string. See DATE_FORMATS for predefined formats.
|
22
|
+
#
|
23
|
+
# This method is aliased to <tt>to_s</tt>.
|
24
|
+
#
|
25
|
+
# ==== Examples
|
26
|
+
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
27
|
+
#
|
28
|
+
# date.to_formatted_s(:db) # => "2007-11-10"
|
29
|
+
# date.to_s(:db) # => "2007-11-10"
|
30
|
+
#
|
31
|
+
# date.to_formatted_s(:short) # => "10 Nov"
|
32
|
+
# date.to_formatted_s(:long) # => "November 10, 2007"
|
33
|
+
# date.to_formatted_s(:long_ordinal) # => "November 10th, 2007"
|
34
|
+
# date.to_formatted_s(:rfc822) # => "10 Nov 2007"
|
35
|
+
#
|
36
|
+
# == Adding your own time formats to to_formatted_s
|
37
|
+
# You can add your own formats to the Date::DATE_FORMATS hash.
|
38
|
+
# Use the format name as the hash key and either a strftime string
|
39
|
+
# or Proc instance that takes a date argument as the value.
|
40
|
+
#
|
41
|
+
# # config/initializers/time_formats.rb
|
42
|
+
# Date::DATE_FORMATS[:month_and_year] = "%B %Y"
|
43
|
+
# Date::DATE_FORMATS[:short_ordinal] = lambda { |date| date.strftime("%B #{date.day.ordinalize}") }
|
44
|
+
def to_formatted_s(format = :default)
|
45
|
+
if formatter = DATE_FORMATS[format]
|
46
|
+
if formatter.respond_to?(:call)
|
47
|
+
formatter.call(self).to_s
|
48
|
+
else
|
49
|
+
strftime(formatter)
|
50
|
+
end
|
51
|
+
else
|
52
|
+
to_default_s
|
53
|
+
end
|
54
|
+
end
|
55
|
+
alias_method :to_default_s, :to_s
|
56
|
+
alias_method :to_s, :to_formatted_s
|
57
|
+
|
58
|
+
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
|
59
|
+
def readable_inspect
|
60
|
+
strftime("%a, %d %b %Y")
|
61
|
+
end
|
62
|
+
alias_method :default_inspect, :inspect
|
63
|
+
alias_method :inspect, :readable_inspect
|
64
|
+
|
65
|
+
# A method to keep Time, Date and DateTime instances interchangeable on conversions.
|
66
|
+
# In this case, it simply returns +self+.
|
67
|
+
def to_date
|
68
|
+
self
|
69
|
+
end if RUBY_VERSION < '1.9'
|
70
|
+
|
71
|
+
# Converts a Date instance to a Time, where the time is set to the beginning of the day.
|
72
|
+
# The timezone can be either :local or :utc (default :local).
|
73
|
+
#
|
74
|
+
# ==== Examples
|
75
|
+
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
76
|
+
#
|
77
|
+
# date.to_time # => Sat Nov 10 00:00:00 0800 2007
|
78
|
+
# date.to_time(:local) # => Sat Nov 10 00:00:00 0800 2007
|
79
|
+
#
|
80
|
+
# date.to_time(:utc) # => Sat Nov 10 00:00:00 UTC 2007
|
81
|
+
def to_time(form = :local)
|
82
|
+
::Time.send("#{form}_time", year, month, day)
|
83
|
+
end
|
84
|
+
|
85
|
+
# Converts a Date instance to a DateTime, where the time is set to the beginning of the day
|
86
|
+
# and UTC offset is set to 0.
|
87
|
+
#
|
88
|
+
# ==== Examples
|
89
|
+
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
90
|
+
#
|
91
|
+
# date.to_datetime # => Sat, 10 Nov 2007 00:00:00 0000
|
92
|
+
def to_datetime
|
93
|
+
::DateTime.civil(year, month, day, 0, 0, 0, 0)
|
94
|
+
end if RUBY_VERSION < '1.9'
|
95
|
+
|
96
|
+
def xmlschema
|
97
|
+
to_time.xmlschema
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Date memoizes some instance methods using metaprogramming to wrap
|
2
|
+
# the methods with one that caches the result in an instance variable.
|
3
|
+
#
|
4
|
+
# If a Date is frozen but the memoized method hasn't been called, the
|
5
|
+
# first call will result in a frozen object error since the memo
|
6
|
+
# instance variable is uninitialized.
|
7
|
+
#
|
8
|
+
# Work around by eagerly memoizing before freezing.
|
9
|
+
#
|
10
|
+
# Ruby 1.9 uses a preinitialized instance variable so it's unaffected.
|
11
|
+
# This hack is as close as we can get to feature detection:
|
12
|
+
if RUBY_VERSION < '1.9'
|
13
|
+
require 'date'
|
14
|
+
begin
|
15
|
+
::Date.today.freeze.jd
|
16
|
+
rescue => frozen_object_error
|
17
|
+
if frozen_object_error.message =~ /frozen/
|
18
|
+
class Date #:nodoc:
|
19
|
+
def freeze
|
20
|
+
self.class.private_instance_methods(false).each do |m|
|
21
|
+
if m.to_s =~ /\A__\d+__\Z/
|
22
|
+
instance_variable_set(:"@#{m}", [send(m)])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'active_support/core_ext/object/acts_like'
|
2
|
+
|
3
|
+
class DateTime
|
4
|
+
# Duck-types as a Date-like class. See Object#acts_like?.
|
5
|
+
def acts_like_date?
|
6
|
+
true
|
7
|
+
end
|
8
|
+
|
9
|
+
# Duck-types as a Time-like class. See Object#acts_like?.
|
10
|
+
def acts_like_time?
|
11
|
+
true
|
12
|
+
end
|
13
|
+
end
|