activesupport 3.1.0 → 5.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +798 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +13 -7
- data/lib/active_support/array_inquirer.rb +44 -0
- data/lib/active_support/backtrace_cleaner.rb +38 -34
- data/lib/active_support/benchmarkable.rb +17 -28
- data/lib/active_support/cache/file_store.rb +85 -70
- data/lib/active_support/cache/mem_cache_store.rb +75 -66
- data/lib/active_support/cache/memory_store.rb +31 -23
- data/lib/active_support/cache/null_store.rb +41 -0
- data/lib/active_support/cache/strategy/local_cache.rb +73 -70
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
- data/lib/active_support/cache.rb +360 -294
- data/lib/active_support/callbacks.rb +563 -393
- data/lib/active_support/concern.rb +42 -34
- data/lib/active_support/concurrency/latch.rb +19 -0
- data/lib/active_support/concurrency/share_lock.rb +186 -0
- data/lib/active_support/configurable.rb +70 -12
- data/lib/active_support/core_ext/array/access.rb +53 -9
- data/lib/active_support/core_ext/array/conversions.rb +109 -62
- data/lib/active_support/core_ext/array/extract_options.rb +2 -2
- data/lib/active_support/core_ext/array/grouping.rb +39 -32
- data/lib/active_support/core_ext/array/inquiry.rb +17 -0
- data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
- data/lib/active_support/core_ext/array/wrap.rb +16 -18
- data/lib/active_support/core_ext/array.rb +2 -2
- data/lib/active_support/core_ext/benchmark.rb +7 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -36
- data/lib/active_support/core_ext/class/attribute.rb +47 -34
- data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -79
- data/lib/active_support/core_ext/class/subclasses.rb +12 -7
- data/lib/active_support/core_ext/class.rb +0 -3
- data/lib/active_support/core_ext/date/blank.rb +12 -0
- data/lib/active_support/core_ext/date/calculations.rb +57 -167
- data/lib/active_support/core_ext/date/conversions.rb +31 -42
- data/lib/active_support/core_ext/date/zones.rb +2 -10
- data/lib/active_support/core_ext/date.rb +5 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +1 -0
- data/lib/active_support/core_ext/date_time/blank.rb +12 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +132 -65
- data/lib/active_support/core_ext/date_time/compatibility.rb +5 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +36 -34
- data/lib/active_support/core_ext/date_time.rb +5 -0
- data/lib/active_support/core_ext/digest/uuid.rb +51 -0
- data/lib/active_support/core_ext/enumerable.rb +81 -74
- data/lib/active_support/core_ext/file/atomic.rb +53 -26
- data/lib/active_support/core_ext/file.rb +0 -1
- data/lib/active_support/core_ext/hash/compact.rb +20 -0
- data/lib/active_support/core_ext/hash/conversions.rb +175 -70
- data/lib/active_support/core_ext/hash/deep_merge.rb +30 -8
- data/lib/active_support/core_ext/hash/except.rb +11 -12
- data/lib/active_support/core_ext/hash/indifferent_access.rb +7 -8
- data/lib/active_support/core_ext/hash/keys.rb +147 -24
- data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
- data/lib/active_support/core_ext/hash/slice.rb +22 -14
- data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
- data/lib/active_support/core_ext/hash.rb +2 -2
- data/lib/active_support/core_ext/integer/inflections.rb +13 -1
- data/lib/active_support/core_ext/integer/multiple.rb +4 -0
- data/lib/active_support/core_ext/integer/time.rb +12 -22
- data/lib/active_support/core_ext/kernel/agnostics.rb +2 -2
- data/lib/active_support/core_ext/kernel/concern.rb +12 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +2 -15
- data/lib/active_support/core_ext/kernel/reporting.rb +12 -62
- data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
- data/lib/active_support/core_ext/kernel.rb +2 -3
- data/lib/active_support/core_ext/load_error.rb +14 -7
- data/lib/active_support/core_ext/marshal.rb +22 -0
- data/lib/active_support/core_ext/module/aliasing.rb +16 -12
- data/lib/active_support/core_ext/module/anonymous.rb +12 -8
- data/lib/active_support/core_ext/module/attr_internal.rb +2 -5
- data/lib/active_support/core_ext/module/attribute_accessors.rb +165 -13
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
- data/lib/active_support/core_ext/module/concerning.rb +135 -0
- data/lib/active_support/core_ext/module/delegation.rb +141 -68
- data/lib/active_support/core_ext/module/deprecation.rb +17 -3
- data/lib/active_support/core_ext/module/introspection.rb +9 -31
- data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
- data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
- data/lib/active_support/core_ext/module/reachable.rb +1 -3
- data/lib/active_support/core_ext/module/remove_method.rb +24 -5
- data/lib/active_support/core_ext/module.rb +3 -3
- data/lib/active_support/core_ext/name_error.rb +15 -2
- data/lib/active_support/core_ext/numeric/bytes.rb +20 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
- data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
- data/lib/active_support/core_ext/numeric/time.rb +31 -36
- data/lib/active_support/core_ext/numeric.rb +2 -0
- data/lib/active_support/core_ext/object/acts_like.rb +4 -4
- data/lib/active_support/core_ext/object/blank.rb +52 -18
- data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
- data/lib/active_support/core_ext/object/duplicable.rb +12 -20
- data/lib/active_support/core_ext/object/inclusion.rb +13 -1
- data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
- data/lib/active_support/core_ext/object/json.rb +205 -0
- data/lib/active_support/core_ext/object/to_param.rb +1 -55
- data/lib/active_support/core_ext/object/to_query.rb +66 -9
- data/lib/active_support/core_ext/object/try.rb +124 -33
- data/lib/active_support/core_ext/object/with_options.rb +37 -11
- data/lib/active_support/core_ext/object.rb +2 -1
- data/lib/active_support/core_ext/range/conversions.rb +17 -7
- data/lib/active_support/core_ext/range/each.rb +21 -0
- data/lib/active_support/core_ext/range/include_range.rb +20 -18
- data/lib/active_support/core_ext/range/overlaps.rb +1 -1
- data/lib/active_support/core_ext/range.rb +1 -2
- data/lib/active_support/core_ext/securerandom.rb +23 -0
- data/lib/active_support/core_ext/string/access.rb +95 -90
- data/lib/active_support/core_ext/string/behavior.rb +1 -1
- data/lib/active_support/core_ext/string/conversions.rb +41 -38
- data/lib/active_support/core_ext/string/exclude.rb +6 -1
- data/lib/active_support/core_ext/string/filters.rb +70 -17
- data/lib/active_support/core_ext/string/indent.rb +43 -0
- data/lib/active_support/core_ext/string/inflections.rb +139 -59
- data/lib/active_support/core_ext/string/inquiry.rb +2 -2
- data/lib/active_support/core_ext/string/multibyte.rb +46 -65
- data/lib/active_support/core_ext/string/output_safety.rb +153 -56
- data/lib/active_support/core_ext/string/strip.rb +3 -6
- data/lib/active_support/core_ext/string/zones.rb +14 -0
- data/lib/active_support/core_ext/string.rb +2 -3
- data/lib/active_support/core_ext/struct.rb +3 -0
- data/lib/active_support/core_ext/time/calculations.rb +173 -173
- data/lib/active_support/core_ext/time/compatibility.rb +5 -0
- data/lib/active_support/core_ext/time/conversions.rb +33 -29
- data/lib/active_support/core_ext/time/marshal.rb +2 -56
- data/lib/active_support/core_ext/time/zones.rb +57 -32
- data/lib/active_support/core_ext/time.rb +5 -0
- data/lib/active_support/core_ext/uri.rb +13 -19
- data/lib/active_support/core_ext.rb +3 -2
- data/lib/active_support/dependencies/autoload.rb +47 -20
- data/lib/active_support/dependencies/interlock.rb +51 -0
- data/lib/active_support/dependencies.rb +315 -265
- data/lib/active_support/deprecation/behaviors.rb +71 -30
- data/lib/active_support/deprecation/instance_delegator.rb +24 -0
- data/lib/active_support/deprecation/method_wrappers.rb +59 -18
- data/lib/active_support/deprecation/proxy_wrappers.rb +82 -14
- data/lib/active_support/deprecation/reporting.rb +61 -14
- data/lib/active_support/deprecation.rb +38 -13
- data/lib/active_support/descendants_tracker.rb +34 -19
- data/lib/active_support/duration/iso8601_parser.rb +122 -0
- data/lib/active_support/duration/iso8601_serializer.rb +51 -0
- data/lib/active_support/duration.rb +85 -14
- data/lib/active_support/evented_file_update_checker.rb +194 -0
- data/lib/active_support/execution_wrapper.rb +117 -0
- data/lib/active_support/executor.rb +6 -0
- data/lib/active_support/file_update_checker.rb +138 -17
- data/lib/active_support/gem_version.rb +15 -0
- data/lib/active_support/gzip.rb +11 -5
- data/lib/active_support/hash_with_indifferent_access.rb +199 -49
- data/lib/active_support/i18n.rb +6 -2
- data/lib/active_support/i18n_railtie.rb +40 -21
- data/lib/active_support/inflections.rb +22 -13
- data/lib/active_support/inflector/inflections.rb +175 -144
- data/lib/active_support/inflector/methods.rb +328 -91
- data/lib/active_support/inflector/transliterate.rb +51 -37
- data/lib/active_support/json/decoding.rb +31 -22
- data/lib/active_support/json/encoding.rb +88 -248
- data/lib/active_support/key_generator.rb +71 -0
- data/lib/active_support/lazy_load_hooks.rb +27 -25
- data/lib/active_support/locale/en.yml +102 -3
- data/lib/active_support/log_subscriber/test_helper.rb +24 -21
- data/lib/active_support/log_subscriber.rb +36 -49
- data/lib/active_support/logger.rb +106 -0
- data/lib/active_support/logger_silence.rb +28 -0
- data/lib/active_support/logger_thread_safe_level.rb +31 -0
- data/lib/active_support/message_encryptor.rb +72 -36
- data/lib/active_support/message_verifier.rb +96 -24
- data/lib/active_support/multibyte/chars.rb +88 -333
- data/lib/active_support/multibyte/unicode.rb +156 -136
- data/lib/active_support/multibyte.rb +5 -28
- data/lib/active_support/notifications/fanout.rb +115 -19
- data/lib/active_support/notifications/instrumenter.rb +52 -15
- data/lib/active_support/notifications.rb +168 -33
- data/lib/active_support/number_helper/number_converter.rb +182 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
- data/lib/active_support/number_helper.rb +368 -0
- data/lib/active_support/option_merger.rb +1 -1
- data/lib/active_support/ordered_hash.rb +18 -183
- data/lib/active_support/ordered_options.rb +44 -24
- data/lib/active_support/per_thread_registry.rb +58 -0
- data/lib/active_support/proxy_object.rb +13 -0
- data/lib/active_support/rails.rb +27 -0
- data/lib/active_support/railtie.rb +25 -34
- data/lib/active_support/reloader.rb +129 -0
- data/lib/active_support/rescuable.rb +98 -48
- data/lib/active_support/security_utils.rb +27 -0
- data/lib/active_support/string_inquirer.rb +14 -9
- data/lib/active_support/subscriber.rb +120 -0
- data/lib/active_support/tagged_logging.rb +78 -0
- data/lib/active_support/test_case.rb +69 -17
- data/lib/active_support/testing/assertions.rb +43 -41
- data/lib/active_support/testing/autorun.rb +12 -0
- data/lib/active_support/testing/constant_lookup.rb +50 -0
- data/lib/active_support/testing/declarative.rb +7 -21
- data/lib/active_support/testing/deprecation.rb +14 -33
- data/lib/active_support/testing/file_fixtures.rb +34 -0
- data/lib/active_support/testing/isolation.rb +53 -95
- data/lib/active_support/testing/method_call_assertions.rb +41 -0
- data/lib/active_support/testing/setup_and_teardown.rb +21 -82
- data/lib/active_support/testing/stream.rb +42 -0
- data/lib/active_support/testing/tagged_logging.rb +25 -0
- data/lib/active_support/testing/time_helpers.rb +134 -0
- data/lib/active_support/time.rb +6 -23
- data/lib/active_support/time_with_zone.rb +239 -92
- data/lib/active_support/values/time_zone.rb +236 -160
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +5 -7
- data/lib/active_support/xml_mini/jdom.rb +19 -13
- data/lib/active_support/xml_mini/libxml.rb +3 -4
- data/lib/active_support/xml_mini/libxmlsax.rb +2 -3
- data/lib/active_support/xml_mini/nokogiri.rb +3 -4
- data/lib/active_support/xml_mini/nokogirisax.rb +2 -3
- data/lib/active_support/xml_mini/rexml.rb +8 -10
- data/lib/active_support/xml_mini.rb +66 -34
- data/lib/active_support.rb +40 -23
- metadata +185 -134
- data/CHANGELOG +0 -1534
- data/lib/active_support/base64.rb +0 -42
- data/lib/active_support/basic_object.rb +0 -21
- data/lib/active_support/buffered_logger.rb +0 -137
- data/lib/active_support/cache/compressed_mem_cache_store.rb +0 -13
- data/lib/active_support/cache/synchronized_memory_store.rb +0 -11
- data/lib/active_support/core_ext/array/random_access.rb +0 -30
- data/lib/active_support/core_ext/array/uniq_by.rb +0 -16
- data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -44
- data/lib/active_support/core_ext/class/inheritable_attributes.rb +0 -178
- data/lib/active_support/core_ext/date/freeze.rb +0 -31
- data/lib/active_support/core_ext/date_time/zones.rb +0 -21
- data/lib/active_support/core_ext/exception.rb +0 -3
- data/lib/active_support/core_ext/file/path.rb +0 -5
- data/lib/active_support/core_ext/float/rounding.rb +0 -19
- data/lib/active_support/core_ext/float.rb +0 -1
- data/lib/active_support/core_ext/hash/deep_dup.rb +0 -11
- data/lib/active_support/core_ext/hash/diff.rb +0 -13
- data/lib/active_support/core_ext/kernel/requires.rb +0 -28
- data/lib/active_support/core_ext/logger.rb +0 -81
- data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +0 -31
- data/lib/active_support/core_ext/module/method_names.rb +0 -14
- data/lib/active_support/core_ext/module/synchronization.rb +0 -43
- data/lib/active_support/core_ext/object/to_json.rb +0 -19
- data/lib/active_support/core_ext/proc.rb +0 -14
- data/lib/active_support/core_ext/process/daemon.rb +0 -23
- data/lib/active_support/core_ext/process.rb +0 -1
- data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
- data/lib/active_support/core_ext/range/cover.rb +0 -3
- data/lib/active_support/core_ext/rexml.rb +0 -46
- data/lib/active_support/core_ext/string/encoding.rb +0 -11
- data/lib/active_support/core_ext/string/interpolation.rb +0 -2
- data/lib/active_support/core_ext/string/xchar.rb +0 -18
- data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
- data/lib/active_support/file_watcher.rb +0 -36
- data/lib/active_support/json/variable.rb +0 -9
- data/lib/active_support/memoizable.rb +0 -105
- data/lib/active_support/multibyte/exceptions.rb +0 -8
- data/lib/active_support/multibyte/utils.rb +0 -60
- data/lib/active_support/ruby/shim.rb +0 -22
- data/lib/active_support/secure_random.rb +0 -6
- data/lib/active_support/testing/mochaing.rb +0 -7
- data/lib/active_support/testing/pending.rb +0 -52
- data/lib/active_support/testing/performance/jruby.rb +0 -115
- data/lib/active_support/testing/performance/rubinius.rb +0 -113
- data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
- data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
- data/lib/active_support/testing/performance/ruby.rb +0 -152
- data/lib/active_support/testing/performance.rb +0 -317
- data/lib/active_support/time/autoload.rb +0 -5
- data/lib/active_support/whiny_nil.rb +0 -60
@@ -1,34 +1,36 @@
|
|
1
|
+
require 'tzinfo'
|
2
|
+
require 'concurrent/map'
|
1
3
|
require 'active_support/core_ext/object/blank'
|
2
|
-
|
3
|
-
|
4
|
-
# The TimeZone class serves as a wrapper around TZInfo::Timezone instances. It allows us to do the following:
|
5
|
-
#
|
6
|
-
# * Limit the set of zones provided by TZInfo to a meaningful subset of 142 zones.
|
7
|
-
# * Retrieve and display zones with a friendlier name (e.g., "Eastern Time (US & Canada)" instead of "America/New_York").
|
8
|
-
# * Lazily load TZInfo::Timezone instances only when they're needed.
|
9
|
-
# * Create ActiveSupport::TimeWithZone instances via TimeZone's +local+, +parse+, +at+ and +now+ methods.
|
10
|
-
#
|
11
|
-
# If you set <tt>config.time_zone</tt> in the Rails Application, you can access this TimeZone object via <tt>Time.zone</tt>:
|
12
|
-
#
|
13
|
-
# # application.rb:
|
14
|
-
# class Application < Rails::Application
|
15
|
-
# config.time_zone = "Eastern Time (US & Canada)"
|
16
|
-
# end
|
17
|
-
#
|
18
|
-
# Time.zone # => #<TimeZone:0x514834...>
|
19
|
-
# Time.zone.name # => "Eastern Time (US & Canada)"
|
20
|
-
# Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00
|
21
|
-
#
|
22
|
-
# The version of TZInfo bundled with Active Support only includes the definitions necessary to support the zones
|
23
|
-
# defined by the TimeZone class. If you need to use zones that aren't defined by TimeZone, you'll need to install the TZInfo gem
|
24
|
-
# (if a recent version of the gem is installed locally, this will be used instead of the bundled version.)
|
4
|
+
|
25
5
|
module ActiveSupport
|
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 ActiveSupport::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 # => #<ActiveSupport::TimeZone:0x514834...>
|
26
|
+
# Time.zone.name # => "Eastern Time (US & Canada)"
|
27
|
+
# Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00
|
26
28
|
class TimeZone
|
27
|
-
# Keys are Rails TimeZone names, values are TZInfo identifiers
|
29
|
+
# Keys are Rails TimeZone names, values are TZInfo identifiers.
|
28
30
|
MAPPING = {
|
29
31
|
"International Date Line West" => "Pacific/Midway",
|
30
32
|
"Midway Island" => "Pacific/Midway",
|
31
|
-
"Samoa"
|
33
|
+
"American Samoa" => "Pacific/Pago_Pago",
|
32
34
|
"Hawaii" => "Pacific/Honolulu",
|
33
35
|
"Alaska" => "America/Juneau",
|
34
36
|
"Pacific Time (US & Canada)" => "America/Los_Angeles",
|
@@ -55,6 +57,7 @@ module ActiveSupport
|
|
55
57
|
"Newfoundland" => "America/St_Johns",
|
56
58
|
"Brasilia" => "America/Sao_Paulo",
|
57
59
|
"Buenos Aires" => "America/Argentina/Buenos_Aires",
|
60
|
+
"Montevideo" => "America/Montevideo",
|
58
61
|
"Georgetown" => "America/Guyana",
|
59
62
|
"Greenland" => "America/Godthab",
|
60
63
|
"Mid-Atlantic" => "Atlantic/South_Georgia",
|
@@ -82,7 +85,8 @@ module ActiveSupport
|
|
82
85
|
"Paris" => "Europe/Paris",
|
83
86
|
"Amsterdam" => "Europe/Amsterdam",
|
84
87
|
"Berlin" => "Europe/Berlin",
|
85
|
-
"Bern" => "Europe/
|
88
|
+
"Bern" => "Europe/Zurich",
|
89
|
+
"Zurich" => "Europe/Zurich",
|
86
90
|
"Rome" => "Europe/Rome",
|
87
91
|
"Stockholm" => "Europe/Stockholm",
|
88
92
|
"Vienna" => "Europe/Vienna",
|
@@ -101,9 +105,11 @@ module ActiveSupport
|
|
101
105
|
"Jerusalem" => "Asia/Jerusalem",
|
102
106
|
"Harare" => "Africa/Harare",
|
103
107
|
"Pretoria" => "Africa/Johannesburg",
|
108
|
+
"Kaliningrad" => "Europe/Kaliningrad",
|
104
109
|
"Moscow" => "Europe/Moscow",
|
105
110
|
"St. Petersburg" => "Europe/Moscow",
|
106
|
-
"Volgograd" => "Europe/
|
111
|
+
"Volgograd" => "Europe/Volgograd",
|
112
|
+
"Samara" => "Europe/Samara",
|
107
113
|
"Kuwait" => "Asia/Kuwait",
|
108
114
|
"Riyadh" => "Asia/Riyadh",
|
109
115
|
"Nairobi" => "Africa/Nairobi",
|
@@ -143,7 +149,7 @@ module ActiveSupport
|
|
143
149
|
"Taipei" => "Asia/Taipei",
|
144
150
|
"Perth" => "Australia/Perth",
|
145
151
|
"Irkutsk" => "Asia/Irkutsk",
|
146
|
-
"
|
152
|
+
"Ulaanbaatar" => "Asia/Ulaanbaatar",
|
147
153
|
"Seoul" => "Asia/Seoul",
|
148
154
|
"Osaka" => "Asia/Tokyo",
|
149
155
|
"Sapporo" => "Asia/Tokyo",
|
@@ -160,30 +166,104 @@ module ActiveSupport
|
|
160
166
|
"Guam" => "Pacific/Guam",
|
161
167
|
"Port Moresby" => "Pacific/Port_Moresby",
|
162
168
|
"Magadan" => "Asia/Magadan",
|
163
|
-
"
|
169
|
+
"Srednekolymsk" => "Asia/Srednekolymsk",
|
170
|
+
"Solomon Is." => "Pacific/Guadalcanal",
|
164
171
|
"New Caledonia" => "Pacific/Noumea",
|
165
172
|
"Fiji" => "Pacific/Fiji",
|
166
173
|
"Kamchatka" => "Asia/Kamchatka",
|
167
174
|
"Marshall Is." => "Pacific/Majuro",
|
168
175
|
"Auckland" => "Pacific/Auckland",
|
169
176
|
"Wellington" => "Pacific/Auckland",
|
170
|
-
"Nuku'alofa" => "Pacific/Tongatapu"
|
171
|
-
|
172
|
-
|
177
|
+
"Nuku'alofa" => "Pacific/Tongatapu",
|
178
|
+
"Tokelau Is." => "Pacific/Fakaofo",
|
179
|
+
"Chatham Is." => "Pacific/Chatham",
|
180
|
+
"Samoa" => "Pacific/Apia"
|
181
|
+
}
|
173
182
|
|
174
183
|
UTC_OFFSET_WITH_COLON = '%s%02d:%02d'
|
175
|
-
UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.
|
184
|
+
UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.tr(':', '')
|
176
185
|
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
186
|
+
@lazy_zones_map = Concurrent::Map.new
|
187
|
+
@country_zones = Concurrent::Map.new
|
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
|
+
# ActiveSupport::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, ActiveSupport::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
|
+
country_zones(:us)
|
247
|
+
end
|
248
|
+
|
249
|
+
# A convenience method for returning a collection of TimeZone objects
|
250
|
+
# for time zones in the country specified by its ISO 3166-1 Alpha2 code.
|
251
|
+
def country_zones(country_code)
|
252
|
+
code = country_code.to_s.upcase
|
253
|
+
@country_zones[code] ||=
|
254
|
+
TZInfo::Country.get(code).zone_identifiers.map do |tz_id|
|
255
|
+
name = MAPPING.key(tz_id)
|
256
|
+
name && self[name]
|
257
|
+
end.compact.sort!
|
258
|
+
end
|
259
|
+
|
260
|
+
private
|
261
|
+
def zones_map
|
262
|
+
@zones_map ||= begin
|
263
|
+
MAPPING.each_key {|place| self[place]} # load all the zones
|
264
|
+
@lazy_zones_map
|
265
|
+
end
|
266
|
+
end
|
187
267
|
end
|
188
268
|
|
189
269
|
include Comparable
|
@@ -192,28 +272,29 @@ module ActiveSupport
|
|
192
272
|
|
193
273
|
# Create a new TimeZone object with the given name and offset. The
|
194
274
|
# offset is the number of seconds that this time zone is offset from UTC
|
195
|
-
# (GMT). Seconds were chosen as the offset unit because that is the unit
|
196
|
-
# Ruby uses to represent time zone offsets (see Time#utc_offset).
|
275
|
+
# (GMT). Seconds were chosen as the offset unit because that is the unit
|
276
|
+
# that Ruby uses to represent time zone offsets (see Time#utc_offset).
|
197
277
|
def initialize(name, utc_offset = nil, tzinfo = nil)
|
198
|
-
self.class.send(:require_tzinfo)
|
199
|
-
|
200
278
|
@name = name
|
201
279
|
@utc_offset = utc_offset
|
202
280
|
@tzinfo = tzinfo || TimeZone.find_tzinfo(name)
|
203
|
-
@current_period = nil
|
204
281
|
end
|
205
282
|
|
283
|
+
# Returns the offset of this time zone from UTC in seconds.
|
206
284
|
def utc_offset
|
207
285
|
if @utc_offset
|
208
286
|
@utc_offset
|
209
287
|
else
|
210
|
-
|
211
|
-
@current_period.try(:utc_offset)
|
288
|
+
tzinfo.current_period.utc_offset if tzinfo && tzinfo.current_period
|
212
289
|
end
|
213
290
|
end
|
214
291
|
|
215
|
-
# Returns
|
216
|
-
#
|
292
|
+
# Returns a formatted string of the offset from UTC, or an alternative
|
293
|
+
# string if the time zone is already UTC.
|
294
|
+
#
|
295
|
+
# zone = ActiveSupport::TimeZone['Central Time (US & Canada)']
|
296
|
+
# zone.formatted_offset # => "-06:00"
|
297
|
+
# zone.formatted_offset(false) # => "-0600"
|
217
298
|
def formatted_offset(colon=true, alternate_utc_string = nil)
|
218
299
|
utc_offset == 0 && alternate_utc_string || self.class.seconds_to_utc_offset(utc_offset, colon)
|
219
300
|
end
|
@@ -221,15 +302,16 @@ module ActiveSupport
|
|
221
302
|
# Compare this time zone to the parameter. The two are compared first on
|
222
303
|
# their offsets, and then by name.
|
223
304
|
def <=>(zone)
|
305
|
+
return unless zone.respond_to? :utc_offset
|
224
306
|
result = (utc_offset <=> zone.utc_offset)
|
225
307
|
result = (name <=> zone.name) if result == 0
|
226
308
|
result
|
227
309
|
end
|
228
310
|
|
229
|
-
# Compare #name and TZInfo identifier to a supplied regexp, returning true
|
311
|
+
# Compare #name and TZInfo identifier to a supplied regexp, returning +true+
|
230
312
|
# if a match is found.
|
231
313
|
def =~(re)
|
232
|
-
|
314
|
+
re === name || re === MAPPING[name]
|
233
315
|
end
|
234
316
|
|
235
317
|
# Returns a textual representation of this time zone.
|
@@ -237,161 +319,155 @@ module ActiveSupport
|
|
237
319
|
"(GMT#{formatted_offset}) #{name}"
|
238
320
|
end
|
239
321
|
|
240
|
-
# Method for creating new ActiveSupport::TimeWithZone instance in time zone
|
322
|
+
# Method for creating new ActiveSupport::TimeWithZone instance in time zone
|
323
|
+
# of +self+ from given values.
|
241
324
|
#
|
242
|
-
# Time.zone =
|
243
|
-
# Time.zone.local(2007, 2, 1, 15, 30, 45)
|
325
|
+
# Time.zone = 'Hawaii' # => "Hawaii"
|
326
|
+
# Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00
|
244
327
|
def local(*args)
|
245
|
-
time = Time.
|
328
|
+
time = Time.utc(*args)
|
246
329
|
ActiveSupport::TimeWithZone.new(nil, self, time)
|
247
330
|
end
|
248
331
|
|
249
|
-
# Method for creating new ActiveSupport::TimeWithZone instance in time zone
|
332
|
+
# Method for creating new ActiveSupport::TimeWithZone instance in time zone
|
333
|
+
# of +self+ from number of seconds since the Unix epoch.
|
250
334
|
#
|
251
|
-
# Time.zone =
|
335
|
+
# Time.zone = 'Hawaii' # => "Hawaii"
|
252
336
|
# Time.utc(2000).to_f # => 946684800.0
|
253
337
|
# Time.zone.at(946684800.0) # => Fri, 31 Dec 1999 14:00:00 HST -10:00
|
254
338
|
def at(secs)
|
255
|
-
|
256
|
-
utc.in_time_zone(self)
|
339
|
+
Time.at(secs).utc.in_time_zone(self)
|
257
340
|
end
|
258
341
|
|
259
|
-
# Method for creating new ActiveSupport::TimeWithZone instance in time zone
|
342
|
+
# Method for creating new ActiveSupport::TimeWithZone instance in time zone
|
343
|
+
# of +self+ from parsed string.
|
260
344
|
#
|
261
|
-
# Time.zone =
|
262
|
-
# Time.zone.parse('1999-12-31 14:00:00')
|
345
|
+
# Time.zone = 'Hawaii' # => "Hawaii"
|
346
|
+
# Time.zone.parse('1999-12-31 14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
|
263
347
|
#
|
264
|
-
# If upper components are missing from the string, they are supplied from
|
348
|
+
# If upper components are missing from the string, they are supplied from
|
349
|
+
# TimeZone#now:
|
265
350
|
#
|
266
|
-
# Time.zone.now
|
267
|
-
# Time.zone.parse('22:30:00')
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
351
|
+
# Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
|
352
|
+
# Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
|
353
|
+
#
|
354
|
+
# However, if the date component is not provided, but any other upper
|
355
|
+
# components are supplied, then the day of the month defaults to 1:
|
356
|
+
#
|
357
|
+
# Time.zone.parse('Mar 2000') # => Wed, 01 Mar 2000 00:00:00 HST -10:00
|
358
|
+
def parse(str, now=now())
|
359
|
+
parts_to_time(Date._parse(str, false), now)
|
360
|
+
end
|
361
|
+
|
362
|
+
# Parses +str+ according to +format+ and returns an ActiveSupport::TimeWithZone.
|
363
|
+
#
|
364
|
+
# Assumes that +str+ is a time in the time zone +self+,
|
365
|
+
# unless +format+ includes an explicit time zone.
|
366
|
+
# (This is the same behavior as +parse+.)
|
367
|
+
# In either case, the returned TimeWithZone has the timezone of +self+.
|
368
|
+
#
|
369
|
+
# Time.zone = 'Hawaii' # => "Hawaii"
|
370
|
+
# 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
|
371
|
+
#
|
372
|
+
# If upper components are missing from the string, they are supplied from
|
373
|
+
# TimeZone#now:
|
374
|
+
#
|
375
|
+
# Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00
|
376
|
+
# Time.zone.strptime('22:30:00', '%H:%M:%S') # => Fri, 31 Dec 1999 22:30:00 HST -10:00
|
377
|
+
#
|
378
|
+
# However, if the date component is not provided, but any other upper
|
379
|
+
# components are supplied, then the day of the month defaults to 1:
|
380
|
+
#
|
381
|
+
# Time.zone.strptime('Mar 2000', '%b %Y') # => Wed, 01 Mar 2000 00:00:00 HST -10:00
|
382
|
+
def strptime(str, format, now=now())
|
383
|
+
parts_to_time(DateTime._strptime(str, format), now)
|
277
384
|
end
|
278
385
|
|
279
|
-
# Returns an ActiveSupport::TimeWithZone instance representing the current
|
280
|
-
# in the time zone represented by +self+.
|
386
|
+
# Returns an ActiveSupport::TimeWithZone instance representing the current
|
387
|
+
# time in the time zone represented by +self+.
|
281
388
|
#
|
282
389
|
# Time.zone = 'Hawaii' # => "Hawaii"
|
283
390
|
# Time.zone.now # => Wed, 23 Jan 2008 20:24:27 HST -10:00
|
284
391
|
def now
|
285
|
-
|
392
|
+
time_now.utc.in_time_zone(self)
|
286
393
|
end
|
287
394
|
|
288
|
-
#
|
395
|
+
# Returns the current date in this time zone.
|
289
396
|
def today
|
290
397
|
tzinfo.now.to_date
|
291
398
|
end
|
292
399
|
|
293
|
-
#
|
294
|
-
|
400
|
+
# Returns the next date in this time zone.
|
401
|
+
def tomorrow
|
402
|
+
today + 1
|
403
|
+
end
|
404
|
+
|
405
|
+
# Returns the previous date in this time zone.
|
406
|
+
def yesterday
|
407
|
+
today - 1
|
408
|
+
end
|
409
|
+
|
410
|
+
# Adjust the given time to the simultaneous time in the time zone
|
411
|
+
# represented by +self+. Returns a Time.utc() instance -- if you want an
|
412
|
+
# ActiveSupport::TimeWithZone instance, use Time#in_time_zone() instead.
|
295
413
|
def utc_to_local(time)
|
296
414
|
tzinfo.utc_to_local(time)
|
297
415
|
end
|
298
416
|
|
299
|
-
# Adjust the given time to the simultaneous time in UTC. Returns a
|
417
|
+
# Adjust the given time to the simultaneous time in UTC. Returns a
|
418
|
+
# Time.utc() instance.
|
300
419
|
def local_to_utc(time, dst=true)
|
301
420
|
tzinfo.local_to_utc(time, dst)
|
302
421
|
end
|
303
422
|
|
304
|
-
# Available so that TimeZone instances respond like TZInfo::Timezone
|
423
|
+
# Available so that TimeZone instances respond like TZInfo::Timezone
|
424
|
+
# instances.
|
305
425
|
def period_for_utc(time)
|
306
426
|
tzinfo.period_for_utc(time)
|
307
427
|
end
|
308
428
|
|
309
|
-
# Available so that TimeZone instances respond like TZInfo::Timezone
|
429
|
+
# Available so that TimeZone instances respond like TZInfo::Timezone
|
430
|
+
# instances.
|
310
431
|
def period_for_local(time, dst=true)
|
311
432
|
tzinfo.period_for_local(time, dst)
|
312
433
|
end
|
313
434
|
|
314
|
-
|
315
|
-
|
316
|
-
require 'active_support/tzinfo' unless defined?(::TZInfo)
|
317
|
-
::TZInfo::TimezoneProxy.new(MAPPING[name] || name)
|
435
|
+
def periods_for_local(time) #:nodoc:
|
436
|
+
tzinfo.periods_for_local(time)
|
318
437
|
end
|
319
438
|
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
# Return a TimeZone instance with the given name, or +nil+ if no
|
324
|
-
# such TimeZone instance exists. (This exists to support the use of
|
325
|
-
# this class with the +composed_of+ macro.)
|
326
|
-
def new(name)
|
327
|
-
self[name]
|
328
|
-
end
|
329
|
-
|
330
|
-
# Return an array of all TimeZone objects. There are multiple
|
331
|
-
# TimeZone objects per time zone, in many cases, to make it easier
|
332
|
-
# for users to find their own time zone.
|
333
|
-
def all
|
334
|
-
@zones ||= zones_map.values.sort
|
335
|
-
end
|
439
|
+
def init_with(coder) #:nodoc:
|
440
|
+
initialize(coder['name'])
|
441
|
+
end
|
336
442
|
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
443
|
+
def encode_with(coder) #:nodoc:
|
444
|
+
coder.tag ="!ruby/object:#{self.class}"
|
445
|
+
coder.map = { 'name' => tzinfo.name }
|
446
|
+
end
|
341
447
|
|
342
|
-
|
448
|
+
private
|
449
|
+
def parts_to_time(parts, now)
|
450
|
+
return if parts.empty?
|
451
|
+
|
452
|
+
time = Time.new(
|
453
|
+
parts.fetch(:year, now.year),
|
454
|
+
parts.fetch(:mon, now.month),
|
455
|
+
parts.fetch(:mday, parts[:year] || parts[:mon] ? 1 : now.day),
|
456
|
+
parts.fetch(:hour, 0),
|
457
|
+
parts.fetch(:min, 0),
|
458
|
+
parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0),
|
459
|
+
parts.fetch(:offset, 0)
|
460
|
+
)
|
461
|
+
|
462
|
+
if parts[:offset]
|
463
|
+
TimeWithZone.new(time.utc, self)
|
464
|
+
else
|
465
|
+
TimeWithZone.new(nil, self, time)
|
343
466
|
end
|
344
467
|
end
|
345
468
|
|
346
|
-
|
347
|
-
|
348
|
-
# numeric value it is either the hour offset, or the second offset, of the
|
349
|
-
# timezone to find. (The first one with that offset will be returned.)
|
350
|
-
# Returns +nil+ if no such time zone is known to the system.
|
351
|
-
def [](arg)
|
352
|
-
case arg
|
353
|
-
when String
|
354
|
-
begin
|
355
|
-
lazy_zones_map[arg] ||= lookup(arg).tap { |tz| tz.utc_offset }
|
356
|
-
rescue TZInfo::InvalidTimezoneIdentifier
|
357
|
-
nil
|
358
|
-
end
|
359
|
-
when Numeric, ActiveSupport::Duration
|
360
|
-
arg *= 3600 if arg.abs <= 13
|
361
|
-
all.find { |z| z.utc_offset == arg.to_i }
|
362
|
-
else
|
363
|
-
raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}"
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
# A convenience method for returning a collection of TimeZone objects
|
368
|
-
# for time zones in the USA.
|
369
|
-
def us_zones
|
370
|
-
@us_zones ||= all.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ }
|
469
|
+
def time_now
|
470
|
+
Time.now
|
371
471
|
end
|
372
|
-
|
373
|
-
protected
|
374
|
-
|
375
|
-
def require_tzinfo
|
376
|
-
require 'tzinfo'
|
377
|
-
rescue LoadError
|
378
|
-
$stderr.puts "You don't have tzinfo installed in your application. Please add it to your Gemfile and run bundle install"
|
379
|
-
raise
|
380
|
-
end
|
381
|
-
|
382
|
-
private
|
383
|
-
|
384
|
-
def lookup(name)
|
385
|
-
(tzinfo = find_tzinfo(name)) && create(tzinfo.name.freeze)
|
386
|
-
end
|
387
|
-
|
388
|
-
def lazy_zones_map
|
389
|
-
require_tzinfo
|
390
|
-
|
391
|
-
@lazy_zones_map ||= Hash.new do |hash, place|
|
392
|
-
hash[place] = create(place) if MAPPING.has_key?(place)
|
393
|
-
end
|
394
|
-
end
|
395
|
-
end
|
396
472
|
end
|
397
473
|
end
|
Binary file
|
@@ -1,10 +1,8 @@
|
|
1
|
-
|
2
|
-
module VERSION #:nodoc:
|
3
|
-
MAJOR = 3
|
4
|
-
MINOR = 1
|
5
|
-
TINY = 0
|
6
|
-
PRE = nil
|
1
|
+
require_relative 'gem_version'
|
7
2
|
|
8
|
-
|
3
|
+
module ActiveSupport
|
4
|
+
# Returns the version of the currently loaded ActiveSupport as a <tt>Gem::Version</tt>
|
5
|
+
def self.version
|
6
|
+
gem_version
|
9
7
|
end
|
10
8
|
end
|
@@ -12,7 +12,6 @@ java_import org.xml.sax.InputSource unless defined? InputSource
|
|
12
12
|
java_import org.xml.sax.Attributes unless defined? Attributes
|
13
13
|
java_import org.w3c.dom.Node unless defined? Node
|
14
14
|
|
15
|
-
# = XmlMini JRuby JDOM implementation
|
16
15
|
module ActiveSupport
|
17
16
|
module XmlMini_JDOM #:nodoc:
|
18
17
|
extend self
|
@@ -38,10 +37,16 @@ module ActiveSupport
|
|
38
37
|
{}
|
39
38
|
else
|
40
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)
|
41
46
|
xml_string_reader = StringReader.new(data)
|
42
47
|
xml_input_source = InputSource.new(xml_string_reader)
|
43
48
|
doc = @dbf.new_document_builder.parse(xml_input_source)
|
44
|
-
merge_element!({CONTENT_KEY => ''}, doc.document_element)
|
49
|
+
merge_element!({CONTENT_KEY => ''}, doc.document_element, XmlMini.depth)
|
45
50
|
end
|
46
51
|
end
|
47
52
|
|
@@ -53,12 +58,13 @@ module ActiveSupport
|
|
53
58
|
# Hash to merge the converted element into.
|
54
59
|
# element::
|
55
60
|
# XML element to merge into hash
|
56
|
-
def merge_element!(hash, element)
|
61
|
+
def merge_element!(hash, element, depth)
|
62
|
+
raise 'Document too deep!' if depth == 0
|
57
63
|
delete_empty(hash)
|
58
|
-
merge!(hash, element.tag_name, collapse(element))
|
64
|
+
merge!(hash, element.tag_name, collapse(element, depth))
|
59
65
|
end
|
60
66
|
|
61
|
-
def delete_empty(hash)
|
67
|
+
def delete_empty(hash)
|
62
68
|
hash.delete(CONTENT_KEY) if hash[CONTENT_KEY] == ''
|
63
69
|
end
|
64
70
|
|
@@ -66,14 +72,14 @@ module ActiveSupport
|
|
66
72
|
#
|
67
73
|
# element::
|
68
74
|
# The document element to be collapsed.
|
69
|
-
def collapse(element)
|
75
|
+
def collapse(element, depth)
|
70
76
|
hash = get_attributes(element)
|
71
77
|
|
72
78
|
child_nodes = element.child_nodes
|
73
79
|
if child_nodes.length > 0
|
74
|
-
|
80
|
+
(0...child_nodes.length).each do |i|
|
75
81
|
child = child_nodes.item(i)
|
76
|
-
merge_element!(hash, child) unless child.node_type == Node.TEXT_NODE
|
82
|
+
merge_element!(hash, child, depth - 1) unless child.node_type == Node.TEXT_NODE
|
77
83
|
end
|
78
84
|
merge_texts!(hash, element) unless empty_content?(element)
|
79
85
|
hash
|
@@ -133,9 +139,9 @@ module ActiveSupport
|
|
133
139
|
def get_attributes(element)
|
134
140
|
attribute_hash = {}
|
135
141
|
attributes = element.attributes
|
136
|
-
|
137
|
-
|
138
|
-
|
142
|
+
(0...attributes.length).each do |i|
|
143
|
+
attribute_hash[CONTENT_KEY] ||= ''
|
144
|
+
attribute_hash[attributes.item(i).name] = attributes.item(i).value
|
139
145
|
end
|
140
146
|
attribute_hash
|
141
147
|
end
|
@@ -147,7 +153,7 @@ module ActiveSupport
|
|
147
153
|
def texts(element)
|
148
154
|
texts = []
|
149
155
|
child_nodes = element.child_nodes
|
150
|
-
|
156
|
+
(0...child_nodes.length).each do |i|
|
151
157
|
item = child_nodes.item(i)
|
152
158
|
if item.node_type == Node.TEXT_NODE
|
153
159
|
texts << item.get_data
|
@@ -163,7 +169,7 @@ module ActiveSupport
|
|
163
169
|
def empty_content?(element)
|
164
170
|
text = ''
|
165
171
|
child_nodes = element.child_nodes
|
166
|
-
|
172
|
+
(0...child_nodes.length).each do |i|
|
167
173
|
item = child_nodes.item(i)
|
168
174
|
if item.node_type == Node.TEXT_NODE
|
169
175
|
text << item.get_data.strip
|
@@ -2,7 +2,6 @@ require 'libxml'
|
|
2
2
|
require 'active_support/core_ext/object/blank'
|
3
3
|
require 'stringio'
|
4
4
|
|
5
|
-
# = XmlMini LibXML implementation
|
6
5
|
module ActiveSupport
|
7
6
|
module XmlMini_LibXML #:nodoc:
|
8
7
|
extend self
|
@@ -38,7 +37,7 @@ module LibXML #:nodoc:
|
|
38
37
|
module Node #:nodoc:
|
39
38
|
CONTENT_ROOT = '__content__'.freeze
|
40
39
|
|
41
|
-
# Convert XML document to hash
|
40
|
+
# Convert XML document to hash.
|
42
41
|
#
|
43
42
|
# hash::
|
44
43
|
# Hash to merge the converted element into.
|
@@ -76,5 +75,5 @@ module LibXML #:nodoc:
|
|
76
75
|
end
|
77
76
|
end
|
78
77
|
|
79
|
-
LibXML::XML::Document.
|
80
|
-
LibXML::XML::Node.
|
78
|
+
LibXML::XML::Document.include(LibXML::Conversions::Document)
|
79
|
+
LibXML::XML::Node.include(LibXML::Conversions::Node)
|