swivel 0.0.146 → 0.0.149
Sign up to get free protection for your applications and to get access to all the features.
- data/README +4 -1
- data/Rakefile +1 -1
- data/lib/swivel.rb +1 -1
- data/lib/swivel2/benchmarking.rb +1 -0
- data/lib/swivel2/config.rb +45 -0
- data/lib/swivel2/connection.rb +89 -0
- data/lib/swivel2/formats.rb +11 -0
- data/lib/swivel2/logging.rb +1 -0
- data/lib/swivel2/performance.rb +21 -0
- data/lib/swivel2/response.rb +5 -0
- data/lib/swivel2/swivelrc.default +5 -0
- data/vendor/activeresource-2.0.2-/CHANGELOG +223 -0
- data/vendor/activeresource-2.0.2-/README +165 -0
- data/vendor/activeresource-2.0.2-/Rakefile +133 -0
- data/vendor/activeresource-2.0.2-/lib/active_resource.rb +47 -0
- data/vendor/activeresource-2.0.2-/lib/active_resource/base.rb +872 -0
- data/vendor/activeresource-2.0.2-/lib/active_resource/connection.rb +172 -0
- data/vendor/activeresource-2.0.2-/lib/active_resource/custom_methods.rb +105 -0
- data/vendor/activeresource-2.0.2-/lib/active_resource/formats.rb +14 -0
- data/vendor/activeresource-2.0.2-/lib/active_resource/formats/json_format.rb +23 -0
- data/vendor/activeresource-2.0.2-/lib/active_resource/formats/xml_format.rb +34 -0
- data/vendor/activeresource-2.0.2-/lib/active_resource/http_mock.rb +147 -0
- data/vendor/activeresource-2.0.2-/lib/active_resource/validations.rb +288 -0
- data/vendor/activeresource-2.0.2-/lib/active_resource/version.rb +9 -0
- data/vendor/activeresource-2.0.2-/lib/activeresource.rb +1 -0
- data/vendor/activeresource-2.0.2-/test/abstract_unit.rb +10 -0
- data/vendor/activeresource-2.0.2-/test/authorization_test.rb +82 -0
- data/vendor/activeresource-2.0.2-/test/base/custom_methods_test.rb +96 -0
- data/vendor/activeresource-2.0.2-/test/base/equality_test.rb +43 -0
- data/vendor/activeresource-2.0.2-/test/base/load_test.rb +111 -0
- data/vendor/activeresource-2.0.2-/test/base_errors_test.rb +48 -0
- data/vendor/activeresource-2.0.2-/test/base_test.rb +454 -0
- data/vendor/activeresource-2.0.2-/test/connection_test.rb +170 -0
- data/vendor/activeresource-2.0.2-/test/fixtures/beast.rb +14 -0
- data/vendor/activeresource-2.0.2-/test/fixtures/person.rb +3 -0
- data/vendor/activeresource-2.0.2-/test/fixtures/street_address.rb +4 -0
- data/vendor/activeresource-2.0.2-/test/format_test.rb +42 -0
- data/vendor/activeresource-2.0.2-/test/setter_trap.rb +27 -0
- data/vendor/activesupport-2.0.2-/CHANGELOG +986 -0
- data/vendor/activesupport-2.0.2-/README +43 -0
- data/vendor/activesupport-2.0.2-/lib/active_support.rb +49 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/basic_object.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/buffered_logger.rb +107 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/clean_logger.rb +127 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext.rb +4 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/array.rb +13 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/array/access.rb +28 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/array/conversions.rb +94 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/array/extract_options.rb +19 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/array/grouping.rb +68 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/array/random_access.rb +12 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/bigdecimal.rb +2 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/bigdecimal/conversions.rb +6 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/blank.rb +50 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/cgi.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +14 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/class.rb +4 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/class/attribute_accessors.rb +48 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/class/delegating_attributes.rb +40 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/class/inheritable_attributes.rb +140 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/class/removal.rb +24 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date.rb +10 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date/behavior.rb +13 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date/calculations.rb +188 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date/conversions.rb +98 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date_time.rb +10 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date_time/calculations.rb +77 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/date_time/conversions.rb +74 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/duplicable.rb +37 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/enumerable.rb +63 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/exception.rb +33 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/file.rb +21 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/float.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/float/rounding.rb +24 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash.rb +13 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/conversions.rb +242 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/diff.rb +19 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/except.rb +24 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/indifferent_access.rb +102 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/keys.rb +54 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/reverse_merge.rb +25 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/slice.rb +28 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/integer.rb +7 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/integer/even_odd.rb +24 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/integer/inflections.rb +21 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/kernel.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/kernel/daemonizing.rb +15 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/kernel/debugger.rb +13 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/kernel/reporting.rb +51 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/kernel/requires.rb +24 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/load_error.rb +38 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/logger.rb +16 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module.rb +8 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/aliasing.rb +70 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/attr_accessor_with_default.rb +31 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/attr_internal.rb +31 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/attribute_accessors.rb +48 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/delegation.rb +62 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/inclusion.rb +11 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/introspection.rb +35 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/module/loading.rb +13 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/name_error.rb +17 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/numeric.rb +7 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/numeric/bytes.rb +44 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/numeric/time.rb +91 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/object.rb +4 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/object/conversions.rb +14 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/object/extending.rb +58 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/object/instance_variables.rb +22 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/object/misc.rb +59 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/pathname.rb +7 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/pathname/clean_within.rb +14 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/proc.rb +12 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/range.rb +11 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/range/blockless_step.rb +22 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/range/conversions.rb +23 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/range/include_range.rb +22 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/range/overlaps.rb +12 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string.rb +23 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/access.rb +58 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/conversions.rb +28 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/inflections.rb +153 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/iterators.rb +17 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/starts_ends_with.rb +27 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/unicode.rb +42 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/string/xchar.rb +11 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/symbol.rb +14 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/test.rb +1 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/test/unit/assertions.rb +62 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/time.rb +19 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/time/behavior.rb +13 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/time/calculations.rb +224 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/core_ext/time/conversions.rb +94 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/dependencies.rb +540 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/deprecation.rb +204 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/duration.rb +96 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/inflections.rb +53 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/inflector.rb +282 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json.rb +31 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/decoding.rb +60 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/date.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/date_time.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/enumerable.rb +12 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/false_class.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/hash.rb +50 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/nil_class.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/numeric.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/object.rb +6 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/regexp.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/string.rb +30 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/symbol.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/time.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoders/true_class.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/encoding.rb +38 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/json/variable.rb +10 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/multibyte.rb +9 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/multibyte/chars.rb +141 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/multibyte/generators/generate_tables.rb +149 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/multibyte/handlers/passthru_handler.rb +9 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/multibyte/handlers/utf8_handler.rb +564 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/multibyte/handlers/utf8_handler_proc.rb +43 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/option_merger.rb +25 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/ordered_options.rb +49 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/test_case.rb +5 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/testing.rb +1 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/testing/default.rb +12 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/values/time_zone.rb +181 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/values/unicode_tables.dat +0 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/vendor.rb +14 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/blankslate.rb +113 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder.rb +13 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder/blankslate.rb +20 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder/css.rb +250 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder/xchar.rb +115 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder/xmlbase.rb +139 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder/xmlevents.rb +63 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/vendor/builder-2.1.2/builder/xmlmarkup.rb +328 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb +1021 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/version.rb +9 -0
- data/vendor/activesupport-2.0.2-/lib/active_support/whiny_nil.rb +38 -0
- data/vendor/activesupport-2.0.2-/lib/activesupport.rb +1 -0
- metadata +222 -2
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'active_support/core_ext/date/behavior'
|
3
|
+
require 'active_support/core_ext/date/calculations'
|
4
|
+
require 'active_support/core_ext/date/conversions'
|
5
|
+
|
6
|
+
class Date#:nodoc:
|
7
|
+
include ActiveSupport::CoreExtensions::Date::Behavior
|
8
|
+
include ActiveSupport::CoreExtensions::Date::Calculations
|
9
|
+
include ActiveSupport::CoreExtensions::Date::Conversions
|
10
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ActiveSupport #:nodoc:
|
2
|
+
module CoreExtensions #:nodoc:
|
3
|
+
module Date #:nodoc:
|
4
|
+
module Behavior
|
5
|
+
# Enable more predictable duck-typing on Date-like classes. See
|
6
|
+
# Object#acts_like?.
|
7
|
+
def acts_like_date?
|
8
|
+
true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
module ActiveSupport #:nodoc:
|
2
|
+
module CoreExtensions #:nodoc:
|
3
|
+
module Date #:nodoc:
|
4
|
+
# Enables the use of time calculations within Time itself
|
5
|
+
module Calculations
|
6
|
+
def self.included(base) #:nodoc:
|
7
|
+
base.extend ClassMethods
|
8
|
+
|
9
|
+
base.instance_eval do
|
10
|
+
alias_method :plus_without_duration, :+
|
11
|
+
alias_method :+, :plus_with_duration
|
12
|
+
|
13
|
+
alias_method :minus_without_duration, :-
|
14
|
+
alias_method :-, :minus_with_duration
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def yesterday
|
20
|
+
::Date.today.yesterday
|
21
|
+
end
|
22
|
+
|
23
|
+
def tomorrow
|
24
|
+
::Date.today.tomorrow
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
29
|
+
# and then subtracts the specified number of seconds
|
30
|
+
def ago(seconds)
|
31
|
+
to_time.since(-seconds)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
35
|
+
# and then adds the specified number of seconds
|
36
|
+
def since(seconds)
|
37
|
+
to_time.since(seconds)
|
38
|
+
end
|
39
|
+
alias :in :since
|
40
|
+
|
41
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
42
|
+
def beginning_of_day
|
43
|
+
to_time
|
44
|
+
end
|
45
|
+
alias :midnight :beginning_of_day
|
46
|
+
alias :at_midnight :beginning_of_day
|
47
|
+
alias :at_beginning_of_day :beginning_of_day
|
48
|
+
|
49
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
|
50
|
+
def end_of_day
|
51
|
+
to_time.end_of_day
|
52
|
+
end
|
53
|
+
|
54
|
+
def plus_with_duration(other) #:nodoc:
|
55
|
+
if ActiveSupport::Duration === other
|
56
|
+
other.since(self)
|
57
|
+
else
|
58
|
+
plus_without_duration(other)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def minus_with_duration(other) #:nodoc:
|
63
|
+
if ActiveSupport::Duration === other
|
64
|
+
plus_with_duration(-other)
|
65
|
+
else
|
66
|
+
minus_without_duration(other)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
|
71
|
+
# any of these keys: :years, :months, :weeks, :days.
|
72
|
+
def advance(options)
|
73
|
+
d = self
|
74
|
+
d = d >> options.delete(:years) * 12 if options[:years]
|
75
|
+
d = d >> options.delete(:months) if options[:months]
|
76
|
+
d = d + options.delete(:weeks) * 7 if options[:weeks]
|
77
|
+
d = d + options.delete(:days) if options[:days]
|
78
|
+
d
|
79
|
+
end
|
80
|
+
|
81
|
+
# Returns a new Date where one or more of the elements have been changed according to the +options+ parameter.
|
82
|
+
#
|
83
|
+
# Examples:
|
84
|
+
#
|
85
|
+
# Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 1)
|
86
|
+
# Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12)
|
87
|
+
def change(options)
|
88
|
+
::Date.new(
|
89
|
+
options[:year] || self.year,
|
90
|
+
options[:month] || self.month,
|
91
|
+
options[:day] || self.day
|
92
|
+
)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Returns a new Date/DateTime representing the time a number of specified months ago
|
96
|
+
def months_ago(months)
|
97
|
+
advance(:months => -months)
|
98
|
+
end
|
99
|
+
|
100
|
+
# Returns a new Date/DateTime representing the time a number of specified months in the future
|
101
|
+
def months_since(months)
|
102
|
+
advance(:months => months)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Returns a new Date/DateTime representing the time a number of specified years ago
|
106
|
+
def years_ago(years)
|
107
|
+
advance(:years => -years)
|
108
|
+
end
|
109
|
+
|
110
|
+
# Returns a new Date/DateTime representing the time a number of specified years in the future
|
111
|
+
def years_since(years)
|
112
|
+
advance(:years => years)
|
113
|
+
end
|
114
|
+
|
115
|
+
# Short-hand for years_ago(1)
|
116
|
+
def last_year
|
117
|
+
years_ago(1)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Short-hand for years_since(1)
|
121
|
+
def next_year
|
122
|
+
years_since(1)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Short-hand for months_ago(1)
|
126
|
+
def last_month
|
127
|
+
months_ago(1)
|
128
|
+
end
|
129
|
+
|
130
|
+
# Short-hand for months_since(1)
|
131
|
+
def next_month
|
132
|
+
months_since(1)
|
133
|
+
end
|
134
|
+
|
135
|
+
# Returns a new Date/DateTime representing the "start" of this week (i.e, Monday; DateTime objects will have time set to 0:00)
|
136
|
+
def beginning_of_week
|
137
|
+
days_to_monday = self.wday!=0 ? self.wday-1 : 6
|
138
|
+
result = self - days_to_monday
|
139
|
+
self.acts_like?(:time) ? result.midnight : result
|
140
|
+
end
|
141
|
+
alias :monday :beginning_of_week
|
142
|
+
alias :at_beginning_of_week :beginning_of_week
|
143
|
+
|
144
|
+
# Returns a new Date/DateTime representing the start of the given day in next week (default is Monday).
|
145
|
+
def next_week(day = :monday)
|
146
|
+
days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
|
147
|
+
result = (self + 7).beginning_of_week + days_into_week[day]
|
148
|
+
self.acts_like?(:time) ? result.change(:hour => 0) : result
|
149
|
+
end
|
150
|
+
|
151
|
+
# 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)
|
152
|
+
def beginning_of_month
|
153
|
+
self.acts_like?(:time) ? change(:day => 1,:hour => 0, :min => 0, :sec => 0) : change(:day => 1)
|
154
|
+
end
|
155
|
+
alias :at_beginning_of_month :beginning_of_month
|
156
|
+
|
157
|
+
# 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)
|
158
|
+
def end_of_month
|
159
|
+
last_day = ::Time.days_in_month( self.month, self.year )
|
160
|
+
self.acts_like?(:time) ? change(:day => last_day, :hour => 23, :min => 59, :sec => 59) : change(:day => last_day)
|
161
|
+
end
|
162
|
+
alias :at_end_of_month :end_of_month
|
163
|
+
|
164
|
+
# 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)
|
165
|
+
def beginning_of_quarter
|
166
|
+
beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
|
167
|
+
end
|
168
|
+
alias :at_beginning_of_quarter :beginning_of_quarter
|
169
|
+
|
170
|
+
# Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00)
|
171
|
+
def beginning_of_year
|
172
|
+
self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0) : change(:month => 1, :day => 1)
|
173
|
+
end
|
174
|
+
alias :at_beginning_of_year :beginning_of_year
|
175
|
+
|
176
|
+
# Convenience method which returns a new Date/DateTime representing the time 1 day ago
|
177
|
+
def yesterday
|
178
|
+
self - 1
|
179
|
+
end
|
180
|
+
|
181
|
+
# Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time
|
182
|
+
def tomorrow
|
183
|
+
self + 1
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
module ActiveSupport #:nodoc:
|
2
|
+
module CoreExtensions #:nodoc:
|
3
|
+
module Date #:nodoc:
|
4
|
+
# Getting dates in different convenient string representations and other objects
|
5
|
+
module Conversions
|
6
|
+
DATE_FORMATS = {
|
7
|
+
:short => "%e %b",
|
8
|
+
:long => "%B %e, %Y",
|
9
|
+
:db => "%Y-%m-%d",
|
10
|
+
:long_ordinal => lambda { |date| date.strftime("%B #{date.day.ordinalize}, %Y") }, # => "April 25th, 2007"
|
11
|
+
:rfc822 => "%e %b %Y"
|
12
|
+
}
|
13
|
+
|
14
|
+
def self.included(base) #:nodoc:
|
15
|
+
base.instance_eval do
|
16
|
+
alias_method :to_default_s, :to_s
|
17
|
+
alias_method :to_s, :to_formatted_s
|
18
|
+
alias_method :default_inspect, :inspect
|
19
|
+
alias_method :inspect, :readable_inspect
|
20
|
+
|
21
|
+
# Ruby 1.9 has Date#to_time which converts to localtime only.
|
22
|
+
remove_method :to_time if base.instance_methods.include?(:to_time)
|
23
|
+
|
24
|
+
# Ruby 1.9 has Date#xmlschema which converts to a string without the time component.
|
25
|
+
remove_method :xmlschema if base.instance_methods.include?(:xmlschema)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Convert to a formatted string - see DATE_FORMATS for predefined formats.
|
30
|
+
# You can also add your own formats to the DATE_FORMATS constant and use them with this method.
|
31
|
+
#
|
32
|
+
# This method is also aliased as <tt>to_s</tt>.
|
33
|
+
#
|
34
|
+
# ==== Examples:
|
35
|
+
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
36
|
+
#
|
37
|
+
# date.to_formatted_s(:db) # => "2007-11-10"
|
38
|
+
# date.to_s(:db) # => "2007-11-10"
|
39
|
+
#
|
40
|
+
# date.to_formatted_s(:short) # => "10 Nov"
|
41
|
+
# date.to_formatted_s(:long) # => "November 10, 2007"
|
42
|
+
# date.to_formatted_s(:long_ordinal) # => "November 10th, 2007"
|
43
|
+
# date.to_formatted_s(:rfc822) # => "10 Nov 2007"
|
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
|
+
|
56
|
+
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
|
57
|
+
def readable_inspect
|
58
|
+
strftime("%a, %d %b %Y")
|
59
|
+
end
|
60
|
+
|
61
|
+
# A method to keep Time, Date and DateTime instances interchangeable on conversions.
|
62
|
+
# In this case, it simply returns +self+.
|
63
|
+
def to_date
|
64
|
+
self
|
65
|
+
end if RUBY_VERSION < '1.9'
|
66
|
+
|
67
|
+
# Converts a Date instance to a Time, where the time is set to the beginning of the day.
|
68
|
+
# The timezone can be either :local or :utc (default :local).
|
69
|
+
#
|
70
|
+
# ==== Examples:
|
71
|
+
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
72
|
+
#
|
73
|
+
# date.to_time # => Sat Nov 10 00:00:00 0800 2007
|
74
|
+
# date.to_time(:local) # => Sat Nov 10 00:00:00 0800 2007
|
75
|
+
#
|
76
|
+
# date.to_time(:utc) # => Sat Nov 10 00:00:00 UTC 2007
|
77
|
+
def to_time(form = :local)
|
78
|
+
::Time.send("#{form}_time", year, month, day)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Converts a Date instance to a DateTime, where the time is set to the beginning of the day
|
82
|
+
# and UTC offset is set to 0.
|
83
|
+
#
|
84
|
+
# ==== Example:
|
85
|
+
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
86
|
+
#
|
87
|
+
# date.to_datetime # => Sat, 10 Nov 2007 00:00:00 0000
|
88
|
+
def to_datetime
|
89
|
+
::DateTime.civil(year, month, day, 0, 0, 0, 0)
|
90
|
+
end if RUBY_VERSION < '1.9'
|
91
|
+
|
92
|
+
def xmlschema
|
93
|
+
to_time.xmlschema
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'active_support/core_ext/time/behavior'
|
3
|
+
require 'active_support/core_ext/date_time/calculations'
|
4
|
+
require 'active_support/core_ext/date_time/conversions'
|
5
|
+
|
6
|
+
class DateTime
|
7
|
+
include ActiveSupport::CoreExtensions::Time::Behavior
|
8
|
+
include ActiveSupport::CoreExtensions::DateTime::Calculations
|
9
|
+
include ActiveSupport::CoreExtensions::DateTime::Conversions
|
10
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'rational'
|
2
|
+
|
3
|
+
module ActiveSupport #:nodoc:
|
4
|
+
module CoreExtensions #:nodoc:
|
5
|
+
module DateTime #:nodoc:
|
6
|
+
# Enables the use of time calculations within DateTime itself
|
7
|
+
module Calculations
|
8
|
+
def self.included(base) #:nodoc:
|
9
|
+
base.extend ClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
# DateTimes aren't aware of DST rules, so use a consistent non-DST offset when creating a DateTime with an offset in the local zone
|
14
|
+
def local_offset
|
15
|
+
::Time.local(2007).utc_offset.to_r / 86400
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Seconds since midnight: DateTime.now.seconds_since_midnight
|
20
|
+
def seconds_since_midnight
|
21
|
+
self.sec + (self.min * 60) + (self.hour * 3600)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns a new DateTime where one or more of the elements have been changed according to the +options+ parameter. The time options
|
25
|
+
# (hour, minute, sec) reset cascadingly, so if only the hour is passed, then minute and sec is set to 0. If the hour and
|
26
|
+
# minute is passed, then sec is set to 0.
|
27
|
+
def change(options)
|
28
|
+
::DateTime.civil(
|
29
|
+
options[:year] || self.year,
|
30
|
+
options[:month] || self.month,
|
31
|
+
options[:day] || self.day,
|
32
|
+
options[:hour] || self.hour,
|
33
|
+
options[:min] || (options[:hour] ? 0 : self.min),
|
34
|
+
options[:sec] || ((options[:hour] || options[:min]) ? 0 : self.sec),
|
35
|
+
options[:offset] || self.offset,
|
36
|
+
options[:start] || self.start
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Uses Date to provide precise Time calculations for years, months, and days. The +options+ parameter takes a hash with
|
41
|
+
# any of these keys: :years, :months, :weeks, :days, :hours, :minutes, :seconds.
|
42
|
+
def advance(options)
|
43
|
+
d = to_date.advance(options)
|
44
|
+
datetime_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
|
45
|
+
seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
|
46
|
+
seconds_to_advance == 0 ? datetime_advanced_by_date : datetime_advanced_by_date.since(seconds_to_advance)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Returns a new DateTime representing the time a number of seconds ago
|
50
|
+
# Do not use this method in combination with x.months, use months_ago instead!
|
51
|
+
def ago(seconds)
|
52
|
+
self.since(-seconds)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns a new DateTime representing the time a number of seconds since the instance time
|
56
|
+
# Do not use this method in combination with x.months, use months_since instead!
|
57
|
+
def since(seconds)
|
58
|
+
self + Rational(seconds.round, 86400)
|
59
|
+
end
|
60
|
+
alias :in :since
|
61
|
+
|
62
|
+
# Returns a new DateTime representing the start of the day (0:00)
|
63
|
+
def beginning_of_day
|
64
|
+
change(:hour => 0)
|
65
|
+
end
|
66
|
+
alias :midnight :beginning_of_day
|
67
|
+
alias :at_midnight :beginning_of_day
|
68
|
+
alias :at_beginning_of_day :beginning_of_day
|
69
|
+
|
70
|
+
# Returns a new DateTime representing the end of the day (23:59:59)
|
71
|
+
def end_of_day
|
72
|
+
change(:hour => 23, :min => 59, :sec => 59)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module ActiveSupport #:nodoc:
|
2
|
+
module CoreExtensions #:nodoc:
|
3
|
+
module DateTime #:nodoc:
|
4
|
+
# Getting datetimes in different convenient string representations and other objects
|
5
|
+
module Conversions
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
alias_method :to_datetime_default_s, :to_s
|
9
|
+
alias_method :to_s, :to_formatted_s
|
10
|
+
alias_method :default_inspect, :inspect
|
11
|
+
alias_method :inspect, :readable_inspect
|
12
|
+
|
13
|
+
# Ruby 1.9 has DateTime#to_time which internally relies on Time. We define our own #to_time which allows
|
14
|
+
# DateTimes outside the range of what can be created with Time.
|
15
|
+
remove_method :to_time if base.instance_methods.include?(:to_time)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Convert to a formatted string - see DATE_FORMATS for predefined formats.
|
20
|
+
# You can also add your own formats to the DATE_FORMATS constant and use them with this method.
|
21
|
+
#
|
22
|
+
# This method is also aliased as <tt>to_s</tt>.
|
23
|
+
#
|
24
|
+
# === Examples:
|
25
|
+
# datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0) # => Tue, 04 Dec 2007 00:00:00 +0000
|
26
|
+
#
|
27
|
+
# datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00"
|
28
|
+
# datetime.to_s(:db) # => "2007-12-04 00:00:00"
|
29
|
+
# datetime.to_s(:number) # => "20071204000000"
|
30
|
+
# datetime.to_formatted_s(:short) # => "04 Dec 00:00"
|
31
|
+
# datetime.to_formatted_s(:long) # => "December 04, 2007 00:00"
|
32
|
+
# datetime.to_formatted_s(:long_ordinal) # => "December 4th, 2007 00:00"
|
33
|
+
# datetime.to_formatted_s(:rfc822) # => "Tue, 04 Dec 2007 00:00:00 +0000"
|
34
|
+
def to_formatted_s(format = :default)
|
35
|
+
if formatter = ::Time::DATE_FORMATS[format]
|
36
|
+
if formatter.respond_to?(:call)
|
37
|
+
formatter.call(self).to_s
|
38
|
+
else
|
39
|
+
strftime(formatter)
|
40
|
+
end
|
41
|
+
else
|
42
|
+
to_datetime_default_s
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000"
|
47
|
+
def readable_inspect
|
48
|
+
to_s(:rfc822)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Converts self to a Ruby Date object; time portion is discarded
|
52
|
+
def to_date
|
53
|
+
::Date.new(year, month, day)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Attempts to convert self to a Ruby Time object; returns self if out of range of Ruby Time class
|
57
|
+
# If self has an offset other than 0, self will just be returned unaltered, since there's no clean way to map it to a Time
|
58
|
+
def to_time
|
59
|
+
self.offset == 0 ? ::Time.utc_time(year, month, day, hour, min, sec) : self
|
60
|
+
end
|
61
|
+
|
62
|
+
# To be able to keep Times, Dates and DateTimes interchangeable on conversions
|
63
|
+
def to_datetime
|
64
|
+
self
|
65
|
+
end
|
66
|
+
|
67
|
+
# Converts datetime to an appropriate format for use in XML
|
68
|
+
def xmlschema
|
69
|
+
strftime("%Y-%m-%dT%H:%M:%S%Z")
|
70
|
+
end if RUBY_VERSION < '1.9'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|