core_ext 0.0.1
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.
- checksums.yaml +7 -0
- data/README.md +3 -0
- data/lib/core_ext/array/access.rb +76 -0
- data/lib/core_ext/array/conversions.rb +211 -0
- data/lib/core_ext/array/extract_options.rb +29 -0
- data/lib/core_ext/array/grouping.rb +116 -0
- data/lib/core_ext/array/inquiry.rb +17 -0
- data/lib/core_ext/array/prepend_and_append.rb +7 -0
- data/lib/core_ext/array/wrap.rb +46 -0
- data/lib/core_ext/array.rb +7 -0
- data/lib/core_ext/array_inquirer.rb +44 -0
- data/lib/core_ext/benchmark.rb +14 -0
- data/lib/core_ext/benchmarkable.rb +49 -0
- data/lib/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/core_ext/big_decimal.rb +1 -0
- data/lib/core_ext/builder.rb +6 -0
- data/lib/core_ext/callbacks.rb +770 -0
- data/lib/core_ext/class/attribute.rb +128 -0
- data/lib/core_ext/class/attribute_accessors.rb +4 -0
- data/lib/core_ext/class/subclasses.rb +42 -0
- data/lib/core_ext/class.rb +2 -0
- data/lib/core_ext/concern.rb +142 -0
- data/lib/core_ext/configurable.rb +148 -0
- data/lib/core_ext/date/acts_like.rb +8 -0
- data/lib/core_ext/date/blank.rb +12 -0
- data/lib/core_ext/date/calculations.rb +143 -0
- data/lib/core_ext/date/conversions.rb +93 -0
- data/lib/core_ext/date/zones.rb +6 -0
- data/lib/core_ext/date.rb +5 -0
- data/lib/core_ext/date_and_time/calculations.rb +328 -0
- data/lib/core_ext/date_and_time/zones.rb +40 -0
- data/lib/core_ext/date_time/acts_like.rb +14 -0
- data/lib/core_ext/date_time/blank.rb +12 -0
- data/lib/core_ext/date_time/calculations.rb +177 -0
- data/lib/core_ext/date_time/conversions.rb +104 -0
- data/lib/core_ext/date_time/zones.rb +6 -0
- data/lib/core_ext/date_time.rb +5 -0
- data/lib/core_ext/deprecation/behaviors.rb +86 -0
- data/lib/core_ext/deprecation/instance_delegator.rb +24 -0
- data/lib/core_ext/deprecation/method_wrappers.rb +70 -0
- data/lib/core_ext/deprecation/proxy_wrappers.rb +149 -0
- data/lib/core_ext/deprecation/reporting.rb +105 -0
- data/lib/core_ext/deprecation.rb +43 -0
- data/lib/core_ext/digest/uuid.rb +51 -0
- data/lib/core_ext/duration.rb +157 -0
- data/lib/core_ext/enumerable.rb +106 -0
- data/lib/core_ext/file/atomic.rb +68 -0
- data/lib/core_ext/file.rb +1 -0
- data/lib/core_ext/hash/compact.rb +20 -0
- data/lib/core_ext/hash/conversions.rb +261 -0
- data/lib/core_ext/hash/deep_merge.rb +38 -0
- data/lib/core_ext/hash/except.rb +22 -0
- data/lib/core_ext/hash/indifferent_access.rb +23 -0
- data/lib/core_ext/hash/keys.rb +170 -0
- data/lib/core_ext/hash/reverse_merge.rb +22 -0
- data/lib/core_ext/hash/slice.rb +48 -0
- data/lib/core_ext/hash/transform_values.rb +29 -0
- data/lib/core_ext/hash.rb +9 -0
- data/lib/core_ext/hash_with_indifferent_access.rb +298 -0
- data/lib/core_ext/inflections.rb +70 -0
- data/lib/core_ext/inflector/inflections.rb +244 -0
- data/lib/core_ext/inflector/methods.rb +381 -0
- data/lib/core_ext/inflector/transliterate.rb +112 -0
- data/lib/core_ext/inflector.rb +7 -0
- data/lib/core_ext/integer/inflections.rb +29 -0
- data/lib/core_ext/integer/multiple.rb +10 -0
- data/lib/core_ext/integer/time.rb +29 -0
- data/lib/core_ext/integer.rb +3 -0
- data/lib/core_ext/json/decoding.rb +67 -0
- data/lib/core_ext/json/encoding.rb +127 -0
- data/lib/core_ext/json.rb +2 -0
- data/lib/core_ext/kernel/agnostics.rb +11 -0
- data/lib/core_ext/kernel/concern.rb +10 -0
- data/lib/core_ext/kernel/reporting.rb +41 -0
- data/lib/core_ext/kernel/singleton_class.rb +6 -0
- data/lib/core_ext/kernel.rb +4 -0
- data/lib/core_ext/load_error.rb +30 -0
- data/lib/core_ext/logger.rb +57 -0
- data/lib/core_ext/logger_silence.rb +24 -0
- data/lib/core_ext/marshal.rb +19 -0
- data/lib/core_ext/module/aliasing.rb +74 -0
- data/lib/core_ext/module/anonymous.rb +28 -0
- data/lib/core_ext/module/attr_internal.rb +36 -0
- data/lib/core_ext/module/attribute_accessors.rb +212 -0
- data/lib/core_ext/module/concerning.rb +135 -0
- data/lib/core_ext/module/delegation.rb +218 -0
- data/lib/core_ext/module/deprecation.rb +23 -0
- data/lib/core_ext/module/introspection.rb +62 -0
- data/lib/core_ext/module/method_transplanting.rb +3 -0
- data/lib/core_ext/module/qualified_const.rb +52 -0
- data/lib/core_ext/module/reachable.rb +8 -0
- data/lib/core_ext/module/remove_method.rb +35 -0
- data/lib/core_ext/module.rb +11 -0
- data/lib/core_ext/multibyte/chars.rb +231 -0
- data/lib/core_ext/multibyte/unicode.rb +388 -0
- data/lib/core_ext/multibyte.rb +21 -0
- data/lib/core_ext/name_error.rb +31 -0
- data/lib/core_ext/numeric/bytes.rb +64 -0
- data/lib/core_ext/numeric/conversions.rb +132 -0
- data/lib/core_ext/numeric/inquiry.rb +26 -0
- data/lib/core_ext/numeric/time.rb +74 -0
- data/lib/core_ext/numeric.rb +4 -0
- data/lib/core_ext/object/acts_like.rb +10 -0
- data/lib/core_ext/object/blank.rb +140 -0
- data/lib/core_ext/object/conversions.rb +4 -0
- data/lib/core_ext/object/deep_dup.rb +53 -0
- data/lib/core_ext/object/duplicable.rb +98 -0
- data/lib/core_ext/object/inclusion.rb +27 -0
- data/lib/core_ext/object/instance_variables.rb +28 -0
- data/lib/core_ext/object/json.rb +199 -0
- data/lib/core_ext/object/to_param.rb +1 -0
- data/lib/core_ext/object/to_query.rb +84 -0
- data/lib/core_ext/object/try.rb +146 -0
- data/lib/core_ext/object/with_options.rb +69 -0
- data/lib/core_ext/object.rb +14 -0
- data/lib/core_ext/option_merger.rb +25 -0
- data/lib/core_ext/ordered_hash.rb +48 -0
- data/lib/core_ext/ordered_options.rb +81 -0
- data/lib/core_ext/range/conversions.rb +34 -0
- data/lib/core_ext/range/each.rb +21 -0
- data/lib/core_ext/range/include_range.rb +23 -0
- data/lib/core_ext/range/overlaps.rb +8 -0
- data/lib/core_ext/range.rb +4 -0
- data/lib/core_ext/regexp.rb +5 -0
- data/lib/core_ext/rescuable.rb +119 -0
- data/lib/core_ext/securerandom.rb +23 -0
- data/lib/core_ext/security_utils.rb +20 -0
- data/lib/core_ext/string/access.rb +104 -0
- data/lib/core_ext/string/behavior.rb +6 -0
- data/lib/core_ext/string/conversions.rb +56 -0
- data/lib/core_ext/string/exclude.rb +11 -0
- data/lib/core_ext/string/filters.rb +102 -0
- data/lib/core_ext/string/indent.rb +43 -0
- data/lib/core_ext/string/inflections.rb +235 -0
- data/lib/core_ext/string/inquiry.rb +13 -0
- data/lib/core_ext/string/multibyte.rb +53 -0
- data/lib/core_ext/string/output_safety.rb +261 -0
- data/lib/core_ext/string/starts_ends_with.rb +4 -0
- data/lib/core_ext/string/strip.rb +23 -0
- data/lib/core_ext/string/zones.rb +14 -0
- data/lib/core_ext/string.rb +13 -0
- data/lib/core_ext/string_inquirer.rb +26 -0
- data/lib/core_ext/tagged_logging.rb +78 -0
- data/lib/core_ext/test_case.rb +88 -0
- data/lib/core_ext/testing/assertions.rb +99 -0
- data/lib/core_ext/testing/autorun.rb +12 -0
- data/lib/core_ext/testing/composite_filter.rb +54 -0
- data/lib/core_ext/testing/constant_lookup.rb +50 -0
- data/lib/core_ext/testing/declarative.rb +26 -0
- data/lib/core_ext/testing/deprecation.rb +36 -0
- data/lib/core_ext/testing/file_fixtures.rb +34 -0
- data/lib/core_ext/testing/isolation.rb +115 -0
- data/lib/core_ext/testing/method_call_assertions.rb +41 -0
- data/lib/core_ext/testing/setup_and_teardown.rb +50 -0
- data/lib/core_ext/testing/stream.rb +42 -0
- data/lib/core_ext/testing/tagged_logging.rb +25 -0
- data/lib/core_ext/testing/time_helpers.rb +134 -0
- data/lib/core_ext/time/acts_like.rb +8 -0
- data/lib/core_ext/time/calculations.rb +284 -0
- data/lib/core_ext/time/conversions.rb +66 -0
- data/lib/core_ext/time/zones.rb +95 -0
- data/lib/core_ext/time.rb +20 -0
- data/lib/core_ext/time_with_zone.rb +503 -0
- data/lib/core_ext/time_zone.rb +464 -0
- data/lib/core_ext/uri.rb +25 -0
- data/lib/core_ext/version.rb +3 -0
- data/lib/core_ext/xml_mini/jdom.rb +181 -0
- data/lib/core_ext/xml_mini/libxml.rb +79 -0
- data/lib/core_ext/xml_mini/libxmlsax.rb +85 -0
- data/lib/core_ext/xml_mini/nokogiri.rb +83 -0
- data/lib/core_ext/xml_mini/nokogirisax.rb +87 -0
- data/lib/core_ext/xml_mini/rexml.rb +130 -0
- data/lib/core_ext/xml_mini.rb +194 -0
- data/lib/core_ext.rb +3 -0
- metadata +310 -0
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
require 'tzinfo'
|
|
2
|
+
require 'core_ext/object/blank'
|
|
3
|
+
require 'core_ext/object/try'
|
|
4
|
+
|
|
5
|
+
module CoreExt
|
|
6
|
+
# The TimeZone class serves as a wrapper around TZInfo::Timezone instances.
|
|
7
|
+
# It allows us to do the following:
|
|
8
|
+
#
|
|
9
|
+
# * Limit the set of zones provided by TZInfo to a meaningful subset of 146
|
|
10
|
+
# zones.
|
|
11
|
+
# * Retrieve and display zones with a friendlier name
|
|
12
|
+
# (e.g., "Eastern Time (US & Canada)" instead of "America/New_York").
|
|
13
|
+
# * Lazily load TZInfo::Timezone instances only when they're needed.
|
|
14
|
+
# * Create CoreExt::TimeWithZone instances via TimeZone's +local+,
|
|
15
|
+
# +parse+, +at+ and +now+ methods.
|
|
16
|
+
#
|
|
17
|
+
# If you set <tt>config.time_zone</tt> in the Rails Application, you can
|
|
18
|
+
# access this TimeZone object via <tt>Time.zone</tt>:
|
|
19
|
+
#
|
|
20
|
+
# # application.rb:
|
|
21
|
+
# class Application < Rails::Application
|
|
22
|
+
# config.time_zone = 'Eastern Time (US & Canada)'
|
|
23
|
+
# end
|
|
24
|
+
#
|
|
25
|
+
# Time.zone # => #<CoreExt::TimeZone:0x514834...>
|
|
26
|
+
# Time.zone.name # => "Eastern Time (US & Canada)"
|
|
27
|
+
# Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00
|
|
28
|
+
class TimeZone
|
|
29
|
+
# Keys are Rails TimeZone names, values are TZInfo identifiers.
|
|
30
|
+
MAPPING = {
|
|
31
|
+
"International Date Line West" => "Pacific/Midway",
|
|
32
|
+
"Midway Island" => "Pacific/Midway",
|
|
33
|
+
"American Samoa" => "Pacific/Pago_Pago",
|
|
34
|
+
"Hawaii" => "Pacific/Honolulu",
|
|
35
|
+
"Alaska" => "America/Juneau",
|
|
36
|
+
"Pacific Time (US & Canada)" => "America/Los_Angeles",
|
|
37
|
+
"Tijuana" => "America/Tijuana",
|
|
38
|
+
"Mountain Time (US & Canada)" => "America/Denver",
|
|
39
|
+
"Arizona" => "America/Phoenix",
|
|
40
|
+
"Chihuahua" => "America/Chihuahua",
|
|
41
|
+
"Mazatlan" => "America/Mazatlan",
|
|
42
|
+
"Central Time (US & Canada)" => "America/Chicago",
|
|
43
|
+
"Saskatchewan" => "America/Regina",
|
|
44
|
+
"Guadalajara" => "America/Mexico_City",
|
|
45
|
+
"Mexico City" => "America/Mexico_City",
|
|
46
|
+
"Monterrey" => "America/Monterrey",
|
|
47
|
+
"Central America" => "America/Guatemala",
|
|
48
|
+
"Eastern Time (US & Canada)" => "America/New_York",
|
|
49
|
+
"Indiana (East)" => "America/Indiana/Indianapolis",
|
|
50
|
+
"Bogota" => "America/Bogota",
|
|
51
|
+
"Lima" => "America/Lima",
|
|
52
|
+
"Quito" => "America/Lima",
|
|
53
|
+
"Atlantic Time (Canada)" => "America/Halifax",
|
|
54
|
+
"Caracas" => "America/Caracas",
|
|
55
|
+
"La Paz" => "America/La_Paz",
|
|
56
|
+
"Santiago" => "America/Santiago",
|
|
57
|
+
"Newfoundland" => "America/St_Johns",
|
|
58
|
+
"Brasilia" => "America/Sao_Paulo",
|
|
59
|
+
"Buenos Aires" => "America/Argentina/Buenos_Aires",
|
|
60
|
+
"Montevideo" => "America/Montevideo",
|
|
61
|
+
"Georgetown" => "America/Guyana",
|
|
62
|
+
"Greenland" => "America/Godthab",
|
|
63
|
+
"Mid-Atlantic" => "Atlantic/South_Georgia",
|
|
64
|
+
"Azores" => "Atlantic/Azores",
|
|
65
|
+
"Cape Verde Is." => "Atlantic/Cape_Verde",
|
|
66
|
+
"Dublin" => "Europe/Dublin",
|
|
67
|
+
"Edinburgh" => "Europe/London",
|
|
68
|
+
"Lisbon" => "Europe/Lisbon",
|
|
69
|
+
"London" => "Europe/London",
|
|
70
|
+
"Casablanca" => "Africa/Casablanca",
|
|
71
|
+
"Monrovia" => "Africa/Monrovia",
|
|
72
|
+
"UTC" => "Etc/UTC",
|
|
73
|
+
"Belgrade" => "Europe/Belgrade",
|
|
74
|
+
"Bratislava" => "Europe/Bratislava",
|
|
75
|
+
"Budapest" => "Europe/Budapest",
|
|
76
|
+
"Ljubljana" => "Europe/Ljubljana",
|
|
77
|
+
"Prague" => "Europe/Prague",
|
|
78
|
+
"Sarajevo" => "Europe/Sarajevo",
|
|
79
|
+
"Skopje" => "Europe/Skopje",
|
|
80
|
+
"Warsaw" => "Europe/Warsaw",
|
|
81
|
+
"Zagreb" => "Europe/Zagreb",
|
|
82
|
+
"Brussels" => "Europe/Brussels",
|
|
83
|
+
"Copenhagen" => "Europe/Copenhagen",
|
|
84
|
+
"Madrid" => "Europe/Madrid",
|
|
85
|
+
"Paris" => "Europe/Paris",
|
|
86
|
+
"Amsterdam" => "Europe/Amsterdam",
|
|
87
|
+
"Berlin" => "Europe/Berlin",
|
|
88
|
+
"Bern" => "Europe/Berlin",
|
|
89
|
+
"Rome" => "Europe/Rome",
|
|
90
|
+
"Stockholm" => "Europe/Stockholm",
|
|
91
|
+
"Vienna" => "Europe/Vienna",
|
|
92
|
+
"West Central Africa" => "Africa/Algiers",
|
|
93
|
+
"Bucharest" => "Europe/Bucharest",
|
|
94
|
+
"Cairo" => "Africa/Cairo",
|
|
95
|
+
"Helsinki" => "Europe/Helsinki",
|
|
96
|
+
"Kyiv" => "Europe/Kiev",
|
|
97
|
+
"Riga" => "Europe/Riga",
|
|
98
|
+
"Sofia" => "Europe/Sofia",
|
|
99
|
+
"Tallinn" => "Europe/Tallinn",
|
|
100
|
+
"Vilnius" => "Europe/Vilnius",
|
|
101
|
+
"Athens" => "Europe/Athens",
|
|
102
|
+
"Istanbul" => "Europe/Istanbul",
|
|
103
|
+
"Minsk" => "Europe/Minsk",
|
|
104
|
+
"Jerusalem" => "Asia/Jerusalem",
|
|
105
|
+
"Harare" => "Africa/Harare",
|
|
106
|
+
"Pretoria" => "Africa/Johannesburg",
|
|
107
|
+
"Kaliningrad" => "Europe/Kaliningrad",
|
|
108
|
+
"Moscow" => "Europe/Moscow",
|
|
109
|
+
"St. Petersburg" => "Europe/Moscow",
|
|
110
|
+
"Volgograd" => "Europe/Volgograd",
|
|
111
|
+
"Samara" => "Europe/Samara",
|
|
112
|
+
"Kuwait" => "Asia/Kuwait",
|
|
113
|
+
"Riyadh" => "Asia/Riyadh",
|
|
114
|
+
"Nairobi" => "Africa/Nairobi",
|
|
115
|
+
"Baghdad" => "Asia/Baghdad",
|
|
116
|
+
"Tehran" => "Asia/Tehran",
|
|
117
|
+
"Abu Dhabi" => "Asia/Muscat",
|
|
118
|
+
"Muscat" => "Asia/Muscat",
|
|
119
|
+
"Baku" => "Asia/Baku",
|
|
120
|
+
"Tbilisi" => "Asia/Tbilisi",
|
|
121
|
+
"Yerevan" => "Asia/Yerevan",
|
|
122
|
+
"Kabul" => "Asia/Kabul",
|
|
123
|
+
"Ekaterinburg" => "Asia/Yekaterinburg",
|
|
124
|
+
"Islamabad" => "Asia/Karachi",
|
|
125
|
+
"Karachi" => "Asia/Karachi",
|
|
126
|
+
"Tashkent" => "Asia/Tashkent",
|
|
127
|
+
"Chennai" => "Asia/Kolkata",
|
|
128
|
+
"Kolkata" => "Asia/Kolkata",
|
|
129
|
+
"Mumbai" => "Asia/Kolkata",
|
|
130
|
+
"New Delhi" => "Asia/Kolkata",
|
|
131
|
+
"Kathmandu" => "Asia/Kathmandu",
|
|
132
|
+
"Astana" => "Asia/Dhaka",
|
|
133
|
+
"Dhaka" => "Asia/Dhaka",
|
|
134
|
+
"Sri Jayawardenepura" => "Asia/Colombo",
|
|
135
|
+
"Almaty" => "Asia/Almaty",
|
|
136
|
+
"Novosibirsk" => "Asia/Novosibirsk",
|
|
137
|
+
"Rangoon" => "Asia/Rangoon",
|
|
138
|
+
"Bangkok" => "Asia/Bangkok",
|
|
139
|
+
"Hanoi" => "Asia/Bangkok",
|
|
140
|
+
"Jakarta" => "Asia/Jakarta",
|
|
141
|
+
"Krasnoyarsk" => "Asia/Krasnoyarsk",
|
|
142
|
+
"Beijing" => "Asia/Shanghai",
|
|
143
|
+
"Chongqing" => "Asia/Chongqing",
|
|
144
|
+
"Hong Kong" => "Asia/Hong_Kong",
|
|
145
|
+
"Urumqi" => "Asia/Urumqi",
|
|
146
|
+
"Kuala Lumpur" => "Asia/Kuala_Lumpur",
|
|
147
|
+
"Singapore" => "Asia/Singapore",
|
|
148
|
+
"Taipei" => "Asia/Taipei",
|
|
149
|
+
"Perth" => "Australia/Perth",
|
|
150
|
+
"Irkutsk" => "Asia/Irkutsk",
|
|
151
|
+
"Ulaanbaatar" => "Asia/Ulaanbaatar",
|
|
152
|
+
"Seoul" => "Asia/Seoul",
|
|
153
|
+
"Osaka" => "Asia/Tokyo",
|
|
154
|
+
"Sapporo" => "Asia/Tokyo",
|
|
155
|
+
"Tokyo" => "Asia/Tokyo",
|
|
156
|
+
"Yakutsk" => "Asia/Yakutsk",
|
|
157
|
+
"Darwin" => "Australia/Darwin",
|
|
158
|
+
"Adelaide" => "Australia/Adelaide",
|
|
159
|
+
"Canberra" => "Australia/Melbourne",
|
|
160
|
+
"Melbourne" => "Australia/Melbourne",
|
|
161
|
+
"Sydney" => "Australia/Sydney",
|
|
162
|
+
"Brisbane" => "Australia/Brisbane",
|
|
163
|
+
"Hobart" => "Australia/Hobart",
|
|
164
|
+
"Vladivostok" => "Asia/Vladivostok",
|
|
165
|
+
"Guam" => "Pacific/Guam",
|
|
166
|
+
"Port Moresby" => "Pacific/Port_Moresby",
|
|
167
|
+
"Magadan" => "Asia/Magadan",
|
|
168
|
+
"Srednekolymsk" => "Asia/Srednekolymsk",
|
|
169
|
+
"Solomon Is." => "Pacific/Guadalcanal",
|
|
170
|
+
"New Caledonia" => "Pacific/Noumea",
|
|
171
|
+
"Fiji" => "Pacific/Fiji",
|
|
172
|
+
"Kamchatka" => "Asia/Kamchatka",
|
|
173
|
+
"Marshall Is." => "Pacific/Majuro",
|
|
174
|
+
"Auckland" => "Pacific/Auckland",
|
|
175
|
+
"Wellington" => "Pacific/Auckland",
|
|
176
|
+
"Nuku'alofa" => "Pacific/Tongatapu",
|
|
177
|
+
"Tokelau Is." => "Pacific/Fakaofo",
|
|
178
|
+
"Chatham Is." => "Pacific/Chatham",
|
|
179
|
+
"Samoa" => "Pacific/Apia"
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
UTC_OFFSET_WITH_COLON = '%s%02d:%02d'
|
|
183
|
+
UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.tr(':', '')
|
|
184
|
+
|
|
185
|
+
# TODO Add concurrent-ruby to support thread safe
|
|
186
|
+
# @lazy_zones_map = Concurrent::Map.new
|
|
187
|
+
@lazy_zones_map = {}
|
|
188
|
+
|
|
189
|
+
class << self
|
|
190
|
+
# Assumes self represents an offset from UTC in seconds (as returned from
|
|
191
|
+
# Time#utc_offset) and turns this into an +HH:MM formatted string.
|
|
192
|
+
#
|
|
193
|
+
# CoreExt::TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00"
|
|
194
|
+
def seconds_to_utc_offset(seconds, colon = true)
|
|
195
|
+
format = colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON
|
|
196
|
+
sign = (seconds < 0 ? '-' : '+')
|
|
197
|
+
hours = seconds.abs / 3600
|
|
198
|
+
minutes = (seconds.abs % 3600) / 60
|
|
199
|
+
format % [sign, hours, minutes]
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def find_tzinfo(name)
|
|
203
|
+
TZInfo::Timezone.new(MAPPING[name] || name)
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
alias_method :create, :new
|
|
207
|
+
|
|
208
|
+
# Returns a TimeZone instance with the given name, or +nil+ if no
|
|
209
|
+
# such TimeZone instance exists. (This exists to support the use of
|
|
210
|
+
# this class with the +composed_of+ macro.)
|
|
211
|
+
def new(name)
|
|
212
|
+
self[name]
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# Returns an array of all TimeZone objects. There are multiple
|
|
216
|
+
# TimeZone objects per time zone, in many cases, to make it easier
|
|
217
|
+
# for users to find their own time zone.
|
|
218
|
+
def all
|
|
219
|
+
@zones ||= zones_map.values.sort
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# Locate a specific time zone object. If the argument is a string, it
|
|
223
|
+
# is interpreted to mean the name of the timezone to locate. If it is a
|
|
224
|
+
# numeric value it is either the hour offset, or the second offset, of the
|
|
225
|
+
# timezone to find. (The first one with that offset will be returned.)
|
|
226
|
+
# Returns +nil+ if no such time zone is known to the system.
|
|
227
|
+
def [](arg)
|
|
228
|
+
case arg
|
|
229
|
+
when String
|
|
230
|
+
begin
|
|
231
|
+
@lazy_zones_map[arg] ||= create(arg)
|
|
232
|
+
rescue TZInfo::InvalidTimezoneIdentifier
|
|
233
|
+
nil
|
|
234
|
+
end
|
|
235
|
+
when Numeric, CoreExt::Duration
|
|
236
|
+
arg *= 3600 if arg.abs <= 13
|
|
237
|
+
all.find { |z| z.utc_offset == arg.to_i }
|
|
238
|
+
else
|
|
239
|
+
raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}"
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
# A convenience method for returning a collection of TimeZone objects
|
|
244
|
+
# for time zones in the USA.
|
|
245
|
+
def us_zones
|
|
246
|
+
@us_zones ||= all.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ }
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
private
|
|
250
|
+
def zones_map
|
|
251
|
+
@zones_map ||= begin
|
|
252
|
+
MAPPING.each_key {|place| self[place]} # load all the zones
|
|
253
|
+
@lazy_zones_map
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
include Comparable
|
|
259
|
+
attr_reader :name
|
|
260
|
+
attr_reader :tzinfo
|
|
261
|
+
|
|
262
|
+
# Create a new TimeZone object with the given name and offset. The
|
|
263
|
+
# offset is the number of seconds that this time zone is offset from UTC
|
|
264
|
+
# (GMT). Seconds were chosen as the offset unit because that is the unit
|
|
265
|
+
# that Ruby uses to represent time zone offsets (see Time#utc_offset).
|
|
266
|
+
def initialize(name, utc_offset = nil, tzinfo = nil)
|
|
267
|
+
@name = name
|
|
268
|
+
@utc_offset = utc_offset
|
|
269
|
+
@tzinfo = tzinfo || TimeZone.find_tzinfo(name)
|
|
270
|
+
@current_period = nil
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Returns the offset of this time zone from UTC in seconds.
|
|
274
|
+
def utc_offset
|
|
275
|
+
if @utc_offset
|
|
276
|
+
@utc_offset
|
|
277
|
+
else
|
|
278
|
+
@current_period ||= tzinfo.current_period if tzinfo
|
|
279
|
+
@current_period.utc_offset if @current_period
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
# Returns a formatted string of the offset from UTC, or an alternative
|
|
284
|
+
# string if the time zone is already UTC.
|
|
285
|
+
#
|
|
286
|
+
# zone = CoreExt::TimeZone['Central Time (US & Canada)']
|
|
287
|
+
# zone.formatted_offset # => "-06:00"
|
|
288
|
+
# zone.formatted_offset(false) # => "-0600"
|
|
289
|
+
def formatted_offset(colon=true, alternate_utc_string = nil)
|
|
290
|
+
utc_offset == 0 && alternate_utc_string || self.class.seconds_to_utc_offset(utc_offset, colon)
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
# Compare this time zone to the parameter. The two are compared first on
|
|
294
|
+
# their offsets, and then by name.
|
|
295
|
+
def <=>(zone)
|
|
296
|
+
return unless zone.respond_to? :utc_offset
|
|
297
|
+
result = (utc_offset <=> zone.utc_offset)
|
|
298
|
+
result = (name <=> zone.name) if result == 0
|
|
299
|
+
result
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Compare #name and TZInfo identifier to a supplied regexp, returning +true+
|
|
303
|
+
# if a match is found.
|
|
304
|
+
def =~(re)
|
|
305
|
+
re === name || re === MAPPING[name]
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
# Returns a textual representation of this time zone.
|
|
309
|
+
def to_s
|
|
310
|
+
"(GMT#{formatted_offset}) #{name}"
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
# Method for creating new CoreExt::TimeWithZone instance in time zone
|
|
314
|
+
# of +self+ from given values.
|
|
315
|
+
#
|
|
316
|
+
# Time.zone = 'Hawaii' # => "Hawaii"
|
|
317
|
+
# Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
|
|
318
|
+
def local(*args)
|
|
319
|
+
time = Time.utc(*args)
|
|
320
|
+
CoreExt::TimeWithZone.new(nil, self, time)
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
# Method for creating new CoreExt::TimeWithZone instance in time zone
|
|
324
|
+
# of +self+ from number of seconds since the Unix epoch.
|
|
325
|
+
#
|
|
326
|
+
# Time.zone = 'Hawaii' # => "Hawaii"
|
|
327
|
+
# Time.utc(2000).to_f # => 946684800.0
|
|
328
|
+
# Time.zone.at(946684800.0) # => Fri, 31 Dec 1999 14:00:00 HST -10:00
|
|
329
|
+
def at(secs)
|
|
330
|
+
Time.at(secs).utc.in_time_zone(self)
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
# Method for creating new CoreExt::TimeWithZone instance in time zone
|
|
334
|
+
# of +self+ from parsed string.
|
|
335
|
+
#
|
|
336
|
+
# Time.zone = 'Hawaii' # => "Hawaii"
|
|
337
|
+
# Time.zone.parse('1999-12-31 14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
|
|
338
|
+
#
|
|
339
|
+
# If upper components are missing from the string, they are supplied from
|
|
340
|
+
# TimeZone#now:
|
|
341
|
+
#
|
|
342
|
+
# Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
|
|
343
|
+
# Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
|
|
344
|
+
#
|
|
345
|
+
# However, if the date component is not provided, but any other upper
|
|
346
|
+
# components are supplied, then the day of the month defaults to 1:
|
|
347
|
+
#
|
|
348
|
+
# Time.zone.parse('Mar 2000') # => Wed, 01 Mar 2000 00:00:00 HST -10:00
|
|
349
|
+
def parse(str, now=now())
|
|
350
|
+
parts_to_time(Date._parse(str, false), now)
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# Parses +str+ according to +format+ and returns an CoreExt::TimeWithZone.
|
|
354
|
+
#
|
|
355
|
+
# Assumes that +str+ is a time in the time zone +self+,
|
|
356
|
+
# unless +format+ includes an explicit time zone.
|
|
357
|
+
# (This is the same behavior as +parse+.)
|
|
358
|
+
# In either case, the returned TimeWithZone has the timezone of +self+.
|
|
359
|
+
#
|
|
360
|
+
# Time.zone = 'Hawaii' # => "Hawaii"
|
|
361
|
+
# Time.zone.strptime('1999-12-31 14:00:00', '%Y-%m-%d %H:%M:%S') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
|
|
362
|
+
#
|
|
363
|
+
# If upper components are missing from the string, they are supplied from
|
|
364
|
+
# TimeZone#now:
|
|
365
|
+
#
|
|
366
|
+
# Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
|
|
367
|
+
# Time.zone.strptime('22:30:00', '%H:%M:%S') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
|
|
368
|
+
#
|
|
369
|
+
# However, if the date component is not provided, but any other upper
|
|
370
|
+
# components are supplied, then the day of the month defaults to 1:
|
|
371
|
+
#
|
|
372
|
+
# Time.zone.strptime('Mar 2000', '%b %Y') # => Wed, 01 Mar 2000 00:00:00 HST -10:00
|
|
373
|
+
def strptime(str, format, now=now())
|
|
374
|
+
parts_to_time(DateTime._strptime(str, format), now)
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
# Returns an CoreExt::TimeWithZone instance representing the current
|
|
378
|
+
# time in the time zone represented by +self+.
|
|
379
|
+
#
|
|
380
|
+
# Time.zone = 'Hawaii' # => "Hawaii"
|
|
381
|
+
# Time.zone.now # => Wed, 23 Jan 2008 20:24:27 HST -10:00
|
|
382
|
+
def now
|
|
383
|
+
time_now.utc.in_time_zone(self)
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
# Returns the current date in this time zone.
|
|
387
|
+
def today
|
|
388
|
+
tzinfo.now.to_date
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
# Returns the next date in this time zone.
|
|
392
|
+
def tomorrow
|
|
393
|
+
today + 1
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
# Returns the previous date in this time zone.
|
|
397
|
+
def yesterday
|
|
398
|
+
today - 1
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
# Adjust the given time to the simultaneous time in the time zone
|
|
402
|
+
# represented by +self+. Returns a Time.utc() instance -- if you want an
|
|
403
|
+
# CoreExt::TimeWithZone instance, use Time#in_time_zone() instead.
|
|
404
|
+
def utc_to_local(time)
|
|
405
|
+
tzinfo.utc_to_local(time)
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
# Adjust the given time to the simultaneous time in UTC. Returns a
|
|
409
|
+
# Time.utc() instance.
|
|
410
|
+
def local_to_utc(time, dst=true)
|
|
411
|
+
tzinfo.local_to_utc(time, dst)
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
# Available so that TimeZone instances respond like TZInfo::Timezone
|
|
415
|
+
# instances.
|
|
416
|
+
def period_for_utc(time)
|
|
417
|
+
tzinfo.period_for_utc(time)
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
# Available so that TimeZone instances respond like TZInfo::Timezone
|
|
421
|
+
# instances.
|
|
422
|
+
def period_for_local(time, dst=true)
|
|
423
|
+
tzinfo.period_for_local(time, dst)
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
def periods_for_local(time) #:nodoc:
|
|
427
|
+
tzinfo.periods_for_local(time)
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
def init_with(coder) #:nodoc:
|
|
431
|
+
initialize(coder['name'])
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
def encode_with(coder) #:nodoc:
|
|
435
|
+
coder.tag ="!ruby/object:#{self.class}"
|
|
436
|
+
coder.map = { 'name' => tzinfo.name }
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
private
|
|
440
|
+
def parts_to_time(parts, now)
|
|
441
|
+
return if parts.empty?
|
|
442
|
+
|
|
443
|
+
time = Time.new(
|
|
444
|
+
parts.fetch(:year, now.year),
|
|
445
|
+
parts.fetch(:mon, now.month),
|
|
446
|
+
parts.fetch(:mday, parts[:year] || parts[:mon] ? 1 : now.day),
|
|
447
|
+
parts.fetch(:hour, 0),
|
|
448
|
+
parts.fetch(:min, 0),
|
|
449
|
+
parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0),
|
|
450
|
+
parts.fetch(:offset, 0)
|
|
451
|
+
)
|
|
452
|
+
|
|
453
|
+
if parts[:offset]
|
|
454
|
+
TimeWithZone.new(time.utc, self)
|
|
455
|
+
else
|
|
456
|
+
TimeWithZone.new(nil, self, time)
|
|
457
|
+
end
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
def time_now
|
|
461
|
+
Time.now
|
|
462
|
+
end
|
|
463
|
+
end
|
|
464
|
+
end
|
data/lib/core_ext/uri.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require 'uri'
|
|
2
|
+
|
|
3
|
+
str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese.
|
|
4
|
+
parser = URI::Parser.new
|
|
5
|
+
|
|
6
|
+
unless str == parser.unescape(parser.escape(str))
|
|
7
|
+
URI::Parser.class_eval do
|
|
8
|
+
remove_method :unescape
|
|
9
|
+
def unescape(str, escaped = /%[a-fA-F\d]{2}/)
|
|
10
|
+
# TODO: Are we actually sure that ASCII == UTF-8?
|
|
11
|
+
# YK: My initial experiments say yes, but let's be sure please
|
|
12
|
+
enc = str.encoding
|
|
13
|
+
enc = Encoding::UTF_8 if enc == Encoding::US_ASCII
|
|
14
|
+
str.gsub(escaped) { |match| [match[1, 2].hex].pack('C') }.force_encoding(enc)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
module URI
|
|
20
|
+
class << self
|
|
21
|
+
def parser
|
|
22
|
+
@parser ||= URI::Parser.new
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
raise "JRuby is required to use the JDOM backend for XmlMini" unless RUBY_PLATFORM =~ /java/
|
|
2
|
+
|
|
3
|
+
require 'jruby'
|
|
4
|
+
include Java
|
|
5
|
+
|
|
6
|
+
require 'core_ext/object/blank'
|
|
7
|
+
|
|
8
|
+
java_import javax.xml.parsers.DocumentBuilder unless defined? DocumentBuilder
|
|
9
|
+
java_import javax.xml.parsers.DocumentBuilderFactory unless defined? DocumentBuilderFactory
|
|
10
|
+
java_import java.io.StringReader unless defined? StringReader
|
|
11
|
+
java_import org.xml.sax.InputSource unless defined? InputSource
|
|
12
|
+
java_import org.xml.sax.Attributes unless defined? Attributes
|
|
13
|
+
java_import org.w3c.dom.Node unless defined? Node
|
|
14
|
+
|
|
15
|
+
module CoreExt
|
|
16
|
+
module XmlMini_JDOM #:nodoc:
|
|
17
|
+
extend self
|
|
18
|
+
|
|
19
|
+
CONTENT_KEY = '__content__'.freeze
|
|
20
|
+
|
|
21
|
+
NODE_TYPE_NAMES = %w{ATTRIBUTE_NODE CDATA_SECTION_NODE COMMENT_NODE DOCUMENT_FRAGMENT_NODE
|
|
22
|
+
DOCUMENT_NODE DOCUMENT_TYPE_NODE ELEMENT_NODE ENTITY_NODE ENTITY_REFERENCE_NODE NOTATION_NODE
|
|
23
|
+
PROCESSING_INSTRUCTION_NODE TEXT_NODE}
|
|
24
|
+
|
|
25
|
+
node_type_map = {}
|
|
26
|
+
NODE_TYPE_NAMES.each { |type| node_type_map[Node.send(type)] = type }
|
|
27
|
+
|
|
28
|
+
# Parse an XML Document string or IO into a simple hash using Java's jdom.
|
|
29
|
+
# data::
|
|
30
|
+
# XML Document string or IO to parse
|
|
31
|
+
def parse(data)
|
|
32
|
+
if data.respond_to?(:read)
|
|
33
|
+
data = data.read
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
if data.blank?
|
|
37
|
+
{}
|
|
38
|
+
else
|
|
39
|
+
@dbf = DocumentBuilderFactory.new_instance
|
|
40
|
+
# secure processing of java xml
|
|
41
|
+
# http://www.ibm.com/developerworks/xml/library/x-tipcfsx/index.html
|
|
42
|
+
@dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
|
|
43
|
+
@dbf.setFeature("http://xml.org/sax/features/external-general-entities", false)
|
|
44
|
+
@dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false)
|
|
45
|
+
@dbf.setFeature(javax.xml.XMLConstants::FEATURE_SECURE_PROCESSING, true)
|
|
46
|
+
xml_string_reader = StringReader.new(data)
|
|
47
|
+
xml_input_source = InputSource.new(xml_string_reader)
|
|
48
|
+
doc = @dbf.new_document_builder.parse(xml_input_source)
|
|
49
|
+
merge_element!({CONTENT_KEY => ''}, doc.document_element, XmlMini.depth)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
# Convert an XML element and merge into the hash
|
|
56
|
+
#
|
|
57
|
+
# hash::
|
|
58
|
+
# Hash to merge the converted element into.
|
|
59
|
+
# element::
|
|
60
|
+
# XML element to merge into hash
|
|
61
|
+
def merge_element!(hash, element, depth)
|
|
62
|
+
raise 'Document too deep!' if depth == 0
|
|
63
|
+
delete_empty(hash)
|
|
64
|
+
merge!(hash, element.tag_name, collapse(element, depth))
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def delete_empty(hash)
|
|
68
|
+
hash.delete(CONTENT_KEY) if hash[CONTENT_KEY] == ''
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Actually converts an XML document element into a data structure.
|
|
72
|
+
#
|
|
73
|
+
# element::
|
|
74
|
+
# The document element to be collapsed.
|
|
75
|
+
def collapse(element, depth)
|
|
76
|
+
hash = get_attributes(element)
|
|
77
|
+
|
|
78
|
+
child_nodes = element.child_nodes
|
|
79
|
+
if child_nodes.length > 0
|
|
80
|
+
(0...child_nodes.length).each do |i|
|
|
81
|
+
child = child_nodes.item(i)
|
|
82
|
+
merge_element!(hash, child, depth - 1) unless child.node_type == Node.TEXT_NODE
|
|
83
|
+
end
|
|
84
|
+
merge_texts!(hash, element) unless empty_content?(element)
|
|
85
|
+
hash
|
|
86
|
+
else
|
|
87
|
+
merge_texts!(hash, element)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Merge all the texts of an element into the hash
|
|
92
|
+
#
|
|
93
|
+
# hash::
|
|
94
|
+
# Hash to add the converted element to.
|
|
95
|
+
# element::
|
|
96
|
+
# XML element whose texts are to me merged into the hash
|
|
97
|
+
def merge_texts!(hash, element)
|
|
98
|
+
delete_empty(hash)
|
|
99
|
+
text_children = texts(element)
|
|
100
|
+
if text_children.join.empty?
|
|
101
|
+
hash
|
|
102
|
+
else
|
|
103
|
+
# must use value to prevent double-escaping
|
|
104
|
+
merge!(hash, CONTENT_KEY, text_children.join)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Adds a new key/value pair to an existing Hash. If the key to be added
|
|
109
|
+
# already exists and the existing value associated with key is not
|
|
110
|
+
# an Array, it will be wrapped in an Array. Then the new value is
|
|
111
|
+
# appended to that Array.
|
|
112
|
+
#
|
|
113
|
+
# hash::
|
|
114
|
+
# Hash to add key/value pair to.
|
|
115
|
+
# key::
|
|
116
|
+
# Key to be added.
|
|
117
|
+
# value::
|
|
118
|
+
# Value to be associated with key.
|
|
119
|
+
def merge!(hash, key, value)
|
|
120
|
+
if hash.has_key?(key)
|
|
121
|
+
if hash[key].instance_of?(Array)
|
|
122
|
+
hash[key] << value
|
|
123
|
+
else
|
|
124
|
+
hash[key] = [hash[key], value]
|
|
125
|
+
end
|
|
126
|
+
elsif value.instance_of?(Array)
|
|
127
|
+
hash[key] = [value]
|
|
128
|
+
else
|
|
129
|
+
hash[key] = value
|
|
130
|
+
end
|
|
131
|
+
hash
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Converts the attributes array of an XML element into a hash.
|
|
135
|
+
# Returns an empty Hash if node has no attributes.
|
|
136
|
+
#
|
|
137
|
+
# element::
|
|
138
|
+
# XML element to extract attributes from.
|
|
139
|
+
def get_attributes(element)
|
|
140
|
+
attribute_hash = {}
|
|
141
|
+
attributes = element.attributes
|
|
142
|
+
(0...attributes.length).each do |i|
|
|
143
|
+
attribute_hash[CONTENT_KEY] ||= ''
|
|
144
|
+
attribute_hash[attributes.item(i).name] = attributes.item(i).value
|
|
145
|
+
end
|
|
146
|
+
attribute_hash
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Determines if a document element has text content
|
|
150
|
+
#
|
|
151
|
+
# element::
|
|
152
|
+
# XML element to be checked.
|
|
153
|
+
def texts(element)
|
|
154
|
+
texts = []
|
|
155
|
+
child_nodes = element.child_nodes
|
|
156
|
+
(0...child_nodes.length).each do |i|
|
|
157
|
+
item = child_nodes.item(i)
|
|
158
|
+
if item.node_type == Node.TEXT_NODE
|
|
159
|
+
texts << item.get_data
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
texts
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Determines if a document element has text content
|
|
166
|
+
#
|
|
167
|
+
# element::
|
|
168
|
+
# XML element to be checked.
|
|
169
|
+
def empty_content?(element)
|
|
170
|
+
text = ''
|
|
171
|
+
child_nodes = element.child_nodes
|
|
172
|
+
(0...child_nodes.length).each do |i|
|
|
173
|
+
item = child_nodes.item(i)
|
|
174
|
+
if item.node_type == Node.TEXT_NODE
|
|
175
|
+
text << item.get_data.strip
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
text.strip.length == 0
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|