activesupport 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activesupport might be problematic. Click here for more details.
- data/CHANGELOG +70 -0
- data/lib/active_support.rb +31 -0
- data/lib/active_support/binding_of_caller.rb +84 -0
- data/lib/active_support/breakpoint.rb +523 -0
- data/lib/active_support/class_attribute_accessors.rb +57 -0
- data/lib/active_support/class_inheritable_attributes.rb +117 -0
- data/lib/active_support/clean_logger.rb +10 -0
- data/lib/active_support/core_ext.rb +1 -0
- data/lib/active_support/core_ext/date.rb +6 -0
- data/lib/active_support/core_ext/date/conversions.rb +31 -0
- data/lib/active_support/core_ext/hash.rb +7 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +38 -0
- data/lib/active_support/core_ext/hash/keys.rb +53 -0
- data/lib/active_support/core_ext/numeric.rb +7 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +33 -0
- data/lib/active_support/core_ext/numeric/time.rb +59 -0
- data/lib/active_support/core_ext/object_and_class.rb +24 -0
- data/lib/active_support/core_ext/string.rb +5 -0
- data/lib/active_support/core_ext/string/inflections.rb +49 -0
- data/lib/active_support/core_ext/time.rb +7 -0
- data/lib/active_support/core_ext/time/calculations.rb +133 -0
- data/lib/active_support/core_ext/time/conversions.rb +34 -0
- data/lib/active_support/dependencies.rb +212 -0
- data/lib/active_support/inflector.rb +93 -0
- data/lib/active_support/misc.rb +8 -0
- data/lib/active_support/module_attribute_accessors.rb +57 -0
- data/lib/active_support/values/time_zone.rb +180 -0
- metadata +71 -0
@@ -0,0 +1,57 @@
|
|
1
|
+
# Extends the module object with module and instance accessors for class attributes,
|
2
|
+
# just like the native attr* accessors for instance attributes.
|
3
|
+
class Module # :nodoc:
|
4
|
+
def mattr_reader(*syms)
|
5
|
+
syms.each do |sym|
|
6
|
+
class_eval <<-EOS
|
7
|
+
if ! defined? @@#{sym.id2name}
|
8
|
+
@@#{sym.id2name} = nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.#{sym.id2name}
|
12
|
+
@@#{sym}
|
13
|
+
end
|
14
|
+
|
15
|
+
def #{sym.id2name}
|
16
|
+
@@#{sym}
|
17
|
+
end
|
18
|
+
|
19
|
+
def call_#{sym.id2name}
|
20
|
+
case @@#{sym.id2name}
|
21
|
+
when Symbol then send(@@#{sym})
|
22
|
+
when Proc then @@#{sym}.call(self)
|
23
|
+
when String then @@#{sym}
|
24
|
+
else nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
EOS
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def mattr_writer(*syms)
|
32
|
+
syms.each do |sym|
|
33
|
+
class_eval <<-EOS
|
34
|
+
if ! defined? @@#{sym.id2name}
|
35
|
+
@@#{sym.id2name} = nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.#{sym.id2name}=(obj)
|
39
|
+
@@#{sym.id2name} = obj
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.set_#{sym.id2name}(obj)
|
43
|
+
@@#{sym.id2name} = obj
|
44
|
+
end
|
45
|
+
|
46
|
+
def #{sym.id2name}=(obj)
|
47
|
+
@@#{sym} = obj
|
48
|
+
end
|
49
|
+
EOS
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def mattr_accessor(*syms)
|
54
|
+
mattr_reader(*syms)
|
55
|
+
mattr_writer(*syms)
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
# A value object representing a time zone. A time zone is simply a named
|
2
|
+
# offset (in seconds) from GMT. Note that two time zone objects are only
|
3
|
+
# equivalent if they have both the same offset, and the same name.
|
4
|
+
#
|
5
|
+
# A TimeZone instance may be used to convert a Time value to the corresponding
|
6
|
+
# time zone.
|
7
|
+
#
|
8
|
+
# The class also includes #all, which returns a list of all TimeZone objects.
|
9
|
+
class TimeZone
|
10
|
+
include Comparable
|
11
|
+
|
12
|
+
attr_reader :name, :utc_offset
|
13
|
+
|
14
|
+
# Create a new TimeZone object with the given name and offset. The offset is
|
15
|
+
# the number of seconds that this time zone is offset from UTC (GMT). Seconds
|
16
|
+
# were chosen as the offset unit because that is the unit that Ruby uses
|
17
|
+
# to represent time zone offsets (see Time#utc_offset).
|
18
|
+
def initialize(name, utc_offset)
|
19
|
+
@name = name
|
20
|
+
@utc_offset = utc_offset
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns the offset of this time zone as a formatted string, of the
|
24
|
+
# format "+HH:MM". If the offset is zero, this returns the empty
|
25
|
+
# string. If +colon+ is false, a colon will not be inserted into the
|
26
|
+
# result.
|
27
|
+
def formatted_offset( colon=true )
|
28
|
+
return "" if utc_offset == 0
|
29
|
+
sign = (utc_offset < 0 ? -1 : 1)
|
30
|
+
hours = utc_offset.abs / 3600
|
31
|
+
minutes = (utc_offset.abs % 3600) / 60
|
32
|
+
"%+03d%s%02d" % [ hours * sign, colon ? ":" : "", minutes ]
|
33
|
+
end
|
34
|
+
|
35
|
+
# Compute and return the current time, in the time zone represented by
|
36
|
+
# +self+.
|
37
|
+
def now
|
38
|
+
adjust(Time.now)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Return the current date in this time zone.
|
42
|
+
def today
|
43
|
+
now.to_date
|
44
|
+
end
|
45
|
+
|
46
|
+
# Adjust the given time to the time zone represented by +self+.
|
47
|
+
def adjust(time)
|
48
|
+
time = time.to_time
|
49
|
+
time + utc_offset - time.utc_offset
|
50
|
+
end
|
51
|
+
|
52
|
+
# Reinterprets the given time value as a time in the current time
|
53
|
+
# zone, and then adjusts it to return the corresponding time in the
|
54
|
+
# local time zone.
|
55
|
+
def unadjust(time)
|
56
|
+
time = Time.local(*time.to_time.to_a)
|
57
|
+
time - utc_offset + time.utc_offset
|
58
|
+
end
|
59
|
+
|
60
|
+
# Compare this time zone to the parameter. The two are comapred first on
|
61
|
+
# their offsets, and then by name.
|
62
|
+
def <=>(zone)
|
63
|
+
result = (utc_offset <=> zone.utc_offset)
|
64
|
+
result = (name <=> zone.name) if result == 0
|
65
|
+
result
|
66
|
+
end
|
67
|
+
|
68
|
+
# Returns a textual representation of this time zone.
|
69
|
+
def to_s
|
70
|
+
"(GMT#{formatted_offset}) #{name}"
|
71
|
+
end
|
72
|
+
|
73
|
+
@@zones = nil
|
74
|
+
|
75
|
+
class << self
|
76
|
+
# Create a new TimeZone instance with the given name and offset.
|
77
|
+
def create(name, offset)
|
78
|
+
zone = allocate
|
79
|
+
zone.send :initialize, name, offset
|
80
|
+
zone
|
81
|
+
end
|
82
|
+
|
83
|
+
# Return a TimeZone instance with the given name, or +nil+ if no
|
84
|
+
# such TimeZone instance exists. (This exists to support the use of
|
85
|
+
# this class with the #composed_of macro.)
|
86
|
+
def new(name)
|
87
|
+
self[name]
|
88
|
+
end
|
89
|
+
|
90
|
+
# Return an array of all TimeZone objects. There are multiple TimeZone
|
91
|
+
# objects per time zone, in many cases, to make it easier for users to
|
92
|
+
# find their own time zone.
|
93
|
+
def all
|
94
|
+
unless @@zones
|
95
|
+
@@zones = []
|
96
|
+
[[-43_200, "International Date Line West" ],
|
97
|
+
[-39_600, "Midway Island", "Samoa" ],
|
98
|
+
[-36_000, "Hawaii" ],
|
99
|
+
[-32_400, "Alaska" ],
|
100
|
+
[-28_800, "Pacific Time (US & Canada)", "Tijuana" ],
|
101
|
+
[-25_200, "Mountain Time (US & Canada)", "Chihuahua", "La Paz",
|
102
|
+
"Mazatlan", "Arizona" ],
|
103
|
+
[-21_600, "Central Time (US & Canada)", "Saskatchewan", "Guadalajara",
|
104
|
+
"Mexico City", "Monterrey", "Central America" ],
|
105
|
+
[-18_000, "Eastern Time (US & Canada)", "Indiana (East)", "Bogota",
|
106
|
+
"Lima", "Quito" ],
|
107
|
+
[-14_400, "Atlantic Time (Canada)", "Caracas", "La Paz", "Santiago" ],
|
108
|
+
[-12_600, "Newfoundland" ],
|
109
|
+
[-10_800, "Brasilia", "Buenos Aires", "Georgetown", "Greenland" ],
|
110
|
+
[ -7_200, "Mid-Atlantic" ],
|
111
|
+
[ -3_600, "Azores", "Cape Verde Is." ],
|
112
|
+
[ 0, "Dublin", "Edinburgh", "Lisbon", "London", "Casablanca",
|
113
|
+
"Monrovia" ],
|
114
|
+
[ 3_600, "Belgrade", "Bratislava", "Budapest", "Ljubljana", "Prague",
|
115
|
+
"Sarajevo", "Skopje", "Warsaw", "Zagreb", "Brussels",
|
116
|
+
"Copenhagen", "Madrid", "Paris", "Amsterdam", "Berlin",
|
117
|
+
"Bern", "Rome", "Stockholm", "Vienna",
|
118
|
+
"West Central Africa" ],
|
119
|
+
[ 7_200, "Bucharest", "Cairo", "Helsinki", "Kyev", "Riga", "Sofia",
|
120
|
+
"Tallinn", "Vilnius", "Athens", "Istanbul", "Minsk",
|
121
|
+
"Jerusalem", "Harare", "Pretoria" ],
|
122
|
+
[ 10_800, "Moscow", "St. Petersburg", "Volgograd", "Kuwait", "Riyadh",
|
123
|
+
"Nairobi", "Baghdad" ],
|
124
|
+
[ 12_600, "Tehran" ],
|
125
|
+
[ 14_400, "Abu Dhabi", "Muscat", "Baku", "Tbilisi", "Yerevan" ],
|
126
|
+
[ 16_200, "Kabul" ],
|
127
|
+
[ 18_000, "Ekaterinburg", "Islamabad", "Karachi", "Tashkent" ],
|
128
|
+
[ 19_800, "Chennai", "Kolkata", "Mumbai", "New Delhi" ],
|
129
|
+
[ 20_700, "Kathmandu" ],
|
130
|
+
[ 21_600, "Astana", "Dhaka", "Sri Jayawardenepura", "Almaty",
|
131
|
+
"Novosibirsk" ],
|
132
|
+
[ 23_400, "Rangoon" ],
|
133
|
+
[ 25_200, "Bangkok", "Hanoi", "Jakarta", "Krasnoyarsk" ],
|
134
|
+
[ 28_800, "Beijing", "Chongqing", "Hong Kong", "Urumqi",
|
135
|
+
"Kuala Lumpur", "Singapore", "Taipei", "Perth", "Irkutsk",
|
136
|
+
"Ulaan Bataar" ],
|
137
|
+
[ 32_400, "Seoul", "Osaka", "Sapporo", "Tokyo", "Yakutsk" ],
|
138
|
+
[ 34_200, "Darwin", "Adelaide" ],
|
139
|
+
[ 36_000, "Canberra", "Melbourne", "Sydney", "Brisbane", "Hobart",
|
140
|
+
"Vladivostok", "Guam", "Port Moresby" ],
|
141
|
+
[ 39_600, "Magadan", "Solomon Is.", "New Caledonia" ],
|
142
|
+
[ 43_200, "Fiji", "Kamchatka", "Marshall Is.", "Auckland",
|
143
|
+
"Wellington" ],
|
144
|
+
[ 46_800, "Nuku'alofa" ]].
|
145
|
+
each do |offset, *places|
|
146
|
+
places.each { |place| @@zones << create(place, offset).freeze }
|
147
|
+
end
|
148
|
+
@@zones.sort!
|
149
|
+
end
|
150
|
+
@@zones
|
151
|
+
end
|
152
|
+
|
153
|
+
# Locate a specific time zone object. If the argument is a string, it
|
154
|
+
# is interpreted to mean the name of the timezone to locate. If it is a
|
155
|
+
# numeric value it is either the hour offset, or the second offset, of the
|
156
|
+
# timezone to find. (The first one with that offset will be returned.)
|
157
|
+
# Returns +nil+ if no such time zone is known to the system.
|
158
|
+
def [](arg)
|
159
|
+
case arg
|
160
|
+
when String
|
161
|
+
all.find { |z| z.name == arg }
|
162
|
+
when Numeric
|
163
|
+
arg *= 3600 if arg.abs <= 13
|
164
|
+
all.find { |z| z.utc_offset == arg.to_i }
|
165
|
+
else
|
166
|
+
raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}"
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
# A regular expression that matches the names of all time zones in
|
171
|
+
# the USA.
|
172
|
+
US_ZONES = /US|Arizona|Indiana|Hawaii|Alaska/
|
173
|
+
|
174
|
+
# A convenience method for returning a collection of TimeZone objects
|
175
|
+
# for time zones in the USA.
|
176
|
+
def us_zones
|
177
|
+
all.find_all { |z| z.name =~ US_ZONES }
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.4
|
3
|
+
specification_version: 1
|
4
|
+
name: activesupport
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2005-02-24
|
8
|
+
summary: Support and utility classes used by the Rails framework.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: david@loudthinking.com
|
12
|
+
homepage: http://www.rubyonrails.org
|
13
|
+
rubyforge_project: activesupport
|
14
|
+
description: Utility library which carries commonly used classes and goodies from the Rails framework
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
authors:
|
28
|
+
- David Heinemeier Hansson
|
29
|
+
files:
|
30
|
+
- CHANGELOG
|
31
|
+
- lib/active_support
|
32
|
+
- lib/active_support.rb
|
33
|
+
- lib/active_support/binding_of_caller.rb
|
34
|
+
- lib/active_support/breakpoint.rb
|
35
|
+
- lib/active_support/class_attribute_accessors.rb
|
36
|
+
- lib/active_support/class_inheritable_attributes.rb
|
37
|
+
- lib/active_support/clean_logger.rb
|
38
|
+
- lib/active_support/core_ext
|
39
|
+
- lib/active_support/core_ext.rb
|
40
|
+
- lib/active_support/dependencies.rb
|
41
|
+
- lib/active_support/inflector.rb
|
42
|
+
- lib/active_support/misc.rb
|
43
|
+
- lib/active_support/module_attribute_accessors.rb
|
44
|
+
- lib/active_support/values
|
45
|
+
- lib/active_support/core_ext/date
|
46
|
+
- lib/active_support/core_ext/date.rb
|
47
|
+
- lib/active_support/core_ext/hash
|
48
|
+
- lib/active_support/core_ext/hash.rb
|
49
|
+
- lib/active_support/core_ext/numeric
|
50
|
+
- lib/active_support/core_ext/numeric.rb
|
51
|
+
- lib/active_support/core_ext/object_and_class.rb
|
52
|
+
- lib/active_support/core_ext/string
|
53
|
+
- lib/active_support/core_ext/string.rb
|
54
|
+
- lib/active_support/core_ext/time
|
55
|
+
- lib/active_support/core_ext/time.rb
|
56
|
+
- lib/active_support/core_ext/date/conversions.rb
|
57
|
+
- lib/active_support/core_ext/hash/indifferent_access.rb
|
58
|
+
- lib/active_support/core_ext/hash/keys.rb
|
59
|
+
- lib/active_support/core_ext/numeric/bytes.rb
|
60
|
+
- lib/active_support/core_ext/numeric/time.rb
|
61
|
+
- lib/active_support/core_ext/string/inflections.rb
|
62
|
+
- lib/active_support/core_ext/time/calculations.rb
|
63
|
+
- lib/active_support/core_ext/time/conversions.rb
|
64
|
+
- lib/active_support/values/time_zone.rb
|
65
|
+
test_files: []
|
66
|
+
rdoc_options: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
executables: []
|
69
|
+
extensions: []
|
70
|
+
requirements: []
|
71
|
+
dependencies: []
|