activesupport 5.0.7.2 → 5.1.7
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 +4 -4
- data/CHANGELOG.md +464 -694
- data/MIT-LICENSE +1 -1
- data/README.rdoc +1 -1
- data/lib/active_support.rb +8 -4
- data/lib/active_support/all.rb +3 -3
- data/lib/active_support/array_inquirer.rb +7 -5
- data/lib/active_support/backtrace_cleaner.rb +4 -4
- data/lib/active_support/benchmarkable.rb +3 -3
- data/lib/active_support/builder.rb +1 -1
- data/lib/active_support/cache.rb +41 -48
- data/lib/active_support/cache/file_store.rb +11 -20
- data/lib/active_support/cache/mem_cache_store.rb +30 -40
- data/lib/active_support/cache/memory_store.rb +13 -13
- data/lib/active_support/cache/null_store.rb +4 -4
- data/lib/active_support/cache/strategy/local_cache.rb +13 -22
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +4 -5
- data/lib/active_support/callbacks.rb +649 -584
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
- data/lib/active_support/concurrency/share_lock.rb +20 -21
- data/lib/active_support/configurable.rb +5 -5
- data/lib/active_support/core_ext.rb +1 -2
- data/lib/active_support/core_ext/array.rb +7 -7
- data/lib/active_support/core_ext/array/access.rb +1 -1
- data/lib/active_support/core_ext/array/conversions.rb +15 -15
- data/lib/active_support/core_ext/array/grouping.rb +1 -1
- data/lib/active_support/core_ext/array/inquiry.rb +1 -1
- data/lib/active_support/core_ext/array/prepend_and_append.rb +1 -1
- data/lib/active_support/core_ext/benchmark.rb +1 -1
- data/lib/active_support/core_ext/big_decimal.rb +1 -1
- data/lib/active_support/core_ext/big_decimal/conversions.rb +4 -6
- data/lib/active_support/core_ext/class.rb +2 -2
- data/lib/active_support/core_ext/class/attribute.rb +5 -5
- data/lib/active_support/core_ext/class/attribute_accessors.rb +1 -1
- data/lib/active_support/core_ext/class/subclasses.rb +18 -4
- data/lib/active_support/core_ext/date.rb +5 -5
- data/lib/active_support/core_ext/date/acts_like.rb +1 -1
- data/lib/active_support/core_ext/date/blank.rb +1 -1
- data/lib/active_support/core_ext/date/calculations.rb +8 -8
- data/lib/active_support/core_ext/date/conversions.rb +12 -12
- data/lib/active_support/core_ext/date/zones.rb +2 -2
- data/lib/active_support/core_ext/date_and_time/calculations.rb +27 -22
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +1 -1
- data/lib/active_support/core_ext/date_and_time/zones.rb +7 -8
- data/lib/active_support/core_ext/date_time.rb +5 -5
- data/lib/active_support/core_ext/date_time/acts_like.rb +2 -2
- data/lib/active_support/core_ext/date_time/blank.rb +1 -1
- data/lib/active_support/core_ext/date_time/calculations.rb +20 -10
- data/lib/active_support/core_ext/date_time/compatibility.rb +2 -2
- data/lib/active_support/core_ext/date_time/conversions.rb +12 -12
- data/lib/active_support/core_ext/digest/uuid.rb +4 -4
- data/lib/active_support/core_ext/enumerable.rb +23 -12
- data/lib/active_support/core_ext/file.rb +1 -1
- data/lib/active_support/core_ext/file/atomic.rb +4 -4
- data/lib/active_support/core_ext/hash.rb +9 -9
- data/lib/active_support/core_ext/hash/compact.rb +12 -9
- data/lib/active_support/core_ext/hash/conversions.rb +36 -37
- data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -2
- data/lib/active_support/core_ext/hash/keys.rb +6 -6
- data/lib/active_support/core_ext/hash/reverse_merge.rb +1 -1
- data/lib/active_support/core_ext/hash/slice.rb +4 -4
- data/lib/active_support/core_ext/hash/transform_values.rb +1 -0
- data/lib/active_support/core_ext/integer.rb +3 -3
- data/lib/active_support/core_ext/integer/inflections.rb +1 -1
- data/lib/active_support/core_ext/integer/time.rb +2 -2
- data/lib/active_support/core_ext/kernel.rb +4 -4
- data/lib/active_support/core_ext/kernel/concern.rb +1 -1
- data/lib/active_support/core_ext/kernel/reporting.rb +1 -1
- data/lib/active_support/core_ext/load_error.rb +1 -18
- data/lib/active_support/core_ext/module.rb +11 -12
- data/lib/active_support/core_ext/module/aliasing.rb +3 -48
- data/lib/active_support/core_ext/module/attr_internal.rb +4 -4
- data/lib/active_support/core_ext/module/attribute_accessors.rb +11 -5
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +20 -13
- data/lib/active_support/core_ext/module/concerning.rb +1 -1
- data/lib/active_support/core_ext/module/delegation.rb +85 -16
- data/lib/active_support/core_ext/module/introspection.rb +3 -11
- data/lib/active_support/core_ext/module/reachable.rb +2 -2
- data/lib/active_support/core_ext/numeric.rb +4 -4
- data/lib/active_support/core_ext/numeric/conversions.rb +3 -9
- data/lib/active_support/core_ext/numeric/inquiry.rb +21 -21
- data/lib/active_support/core_ext/numeric/time.rb +5 -5
- data/lib/active_support/core_ext/object.rb +12 -12
- data/lib/active_support/core_ext/object/blank.rb +3 -1
- data/lib/active_support/core_ext/object/conversions.rb +4 -4
- data/lib/active_support/core_ext/object/deep_dup.rb +1 -1
- data/lib/active_support/core_ext/object/duplicable.rb +34 -4
- data/lib/active_support/core_ext/object/inclusion.rb +1 -1
- data/lib/active_support/core_ext/object/json.rb +26 -12
- data/lib/active_support/core_ext/object/to_param.rb +1 -1
- data/lib/active_support/core_ext/object/to_query.rb +8 -5
- data/lib/active_support/core_ext/object/try.rb +1 -1
- data/lib/active_support/core_ext/object/with_options.rb +12 -1
- data/lib/active_support/core_ext/range.rb +4 -4
- data/lib/active_support/core_ext/range/conversions.rb +1 -1
- data/lib/active_support/core_ext/regexp.rb +4 -0
- data/lib/active_support/core_ext/securerandom.rb +3 -3
- data/lib/active_support/core_ext/string.rb +13 -13
- data/lib/active_support/core_ext/string/access.rb +6 -6
- data/lib/active_support/core_ext/string/conversions.rb +2 -2
- data/lib/active_support/core_ext/string/filters.rb +3 -3
- data/lib/active_support/core_ext/string/indent.rb +4 -4
- data/lib/active_support/core_ext/string/inflections.rb +10 -14
- data/lib/active_support/core_ext/string/inquiry.rb +1 -1
- data/lib/active_support/core_ext/string/multibyte.rb +1 -1
- data/lib/active_support/core_ext/string/output_safety.rb +19 -20
- data/lib/active_support/core_ext/string/strip.rb +1 -1
- data/lib/active_support/core_ext/string/zones.rb +2 -2
- data/lib/active_support/core_ext/time.rb +5 -5
- data/lib/active_support/core_ext/time/acts_like.rb +1 -1
- data/lib/active_support/core_ext/time/calculations.rb +46 -29
- data/lib/active_support/core_ext/time/conversions.rb +15 -12
- data/lib/active_support/core_ext/time/zones.rb +3 -3
- data/lib/active_support/core_ext/uri.rb +2 -2
- data/lib/active_support/dependencies.rb +45 -46
- data/lib/active_support/dependencies/interlock.rb +1 -1
- data/lib/active_support/deprecation.rb +9 -8
- data/lib/active_support/deprecation/behaviors.rb +3 -3
- data/lib/active_support/deprecation/constant_accessor.rb +50 -0
- data/lib/active_support/deprecation/instance_delegator.rb +2 -2
- data/lib/active_support/deprecation/method_wrappers.rb +10 -3
- data/lib/active_support/deprecation/proxy_wrappers.rb +6 -4
- data/lib/active_support/deprecation/reporting.rb +7 -7
- data/lib/active_support/duration.rb +221 -28
- data/lib/active_support/duration/iso8601_parser.rb +66 -65
- data/lib/active_support/duration/iso8601_serializer.rb +11 -9
- data/lib/active_support/evented_file_update_checker.rb +59 -55
- data/lib/active_support/execution_wrapper.rb +3 -3
- data/lib/active_support/executor.rb +1 -1
- data/lib/active_support/file_update_checker.rb +54 -50
- data/lib/active_support/gem_version.rb +2 -2
- data/lib/active_support/gzip.rb +4 -4
- data/lib/active_support/hash_with_indifferent_access.rb +40 -28
- data/lib/active_support/i18n.rb +5 -5
- data/lib/active_support/i18n_railtie.rb +14 -9
- data/lib/active_support/inflections.rb +11 -11
- data/lib/active_support/inflector.rb +5 -5
- data/lib/active_support/inflector/inflections.rb +11 -9
- data/lib/active_support/inflector/methods.rb +52 -51
- data/lib/active_support/inflector/transliterate.rb +8 -11
- data/lib/active_support/json.rb +2 -2
- data/lib/active_support/json/decoding.rb +3 -3
- data/lib/active_support/json/encoding.rb +8 -7
- data/lib/active_support/key_generator.rb +17 -17
- data/lib/active_support/lazy_load_hooks.rb +2 -2
- data/lib/active_support/log_subscriber.rb +9 -7
- data/lib/active_support/log_subscriber/test_helper.rb +9 -9
- data/lib/active_support/logger.rb +3 -3
- data/lib/active_support/logger_silence.rb +3 -3
- data/lib/active_support/logger_thread_safe_level.rb +1 -1
- data/lib/active_support/message_encryptor.rb +77 -35
- data/lib/active_support/message_verifier.rb +7 -7
- data/lib/active_support/multibyte.rb +2 -2
- data/lib/active_support/multibyte/chars.rb +23 -21
- data/lib/active_support/multibyte/unicode.rb +68 -89
- data/lib/active_support/notifications.rb +7 -5
- data/lib/active_support/notifications/fanout.rb +3 -3
- data/lib/active_support/notifications/instrumenter.rb +5 -5
- data/lib/active_support/number_helper.rb +5 -4
- data/lib/active_support/number_helper/number_converter.rb +11 -11
- data/lib/active_support/number_helper/number_to_currency_converter.rb +3 -3
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +1 -2
- data/lib/active_support/number_helper/number_to_human_converter.rb +8 -10
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +6 -11
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +1 -1
- data/lib/active_support/number_helper/number_to_phone_converter.rb +1 -3
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +12 -32
- data/lib/active_support/number_helper/rounding_helper.rb +64 -0
- data/lib/active_support/option_merger.rb +1 -1
- data/lib/active_support/ordered_hash.rb +3 -3
- data/lib/active_support/ordered_options.rb +6 -4
- data/lib/active_support/per_thread_registry.rb +5 -5
- data/lib/active_support/rails.rb +12 -6
- data/lib/active_support/railtie.rb +3 -3
- data/lib/active_support/reloader.rb +1 -1
- data/lib/active_support/rescuable.rb +6 -6
- data/lib/active_support/security_utils.rb +1 -1
- data/lib/active_support/string_inquirer.rb +8 -2
- data/lib/active_support/subscriber.rb +9 -5
- data/lib/active_support/tagged_logging.rb +4 -4
- data/lib/active_support/test_case.rb +12 -29
- data/lib/active_support/testing/assertions.rb +100 -2
- data/lib/active_support/testing/autorun.rb +2 -2
- data/lib/active_support/testing/constant_lookup.rb +0 -1
- data/lib/active_support/testing/declarative.rb +1 -1
- data/lib/active_support/testing/deprecation.rb +3 -2
- data/lib/active_support/testing/isolation.rb +15 -22
- data/lib/active_support/testing/method_call_assertions.rb +1 -1
- data/lib/active_support/testing/setup_and_teardown.rb +2 -2
- data/lib/active_support/testing/stream.rb +28 -28
- data/lib/active_support/testing/tagged_logging.rb +1 -1
- data/lib/active_support/testing/time_helpers.rb +45 -11
- data/lib/active_support/time.rb +12 -12
- data/lib/active_support/time_with_zone.rb +16 -14
- data/lib/active_support/values/time_zone.rb +100 -31
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +1 -1
- data/lib/active_support/xml_mini.rb +34 -36
- data/lib/active_support/xml_mini/jdom.rb +112 -112
- data/lib/active_support/xml_mini/libxml.rb +12 -11
- data/lib/active_support/xml_mini/libxmlsax.rb +13 -14
- data/lib/active_support/xml_mini/nokogiri.rb +10 -10
- data/lib/active_support/xml_mini/nokogirisax.rb +12 -13
- data/lib/active_support/xml_mini/rexml.rb +9 -9
- metadata +8 -9
- data/lib/active_support/concurrency/latch.rb +0 -26
- data/lib/active_support/core_ext/kernel/debugger.rb +0 -3
- data/lib/active_support/core_ext/module/method_transplanting.rb +0 -3
- data/lib/active_support/core_ext/module/qualified_const.rb +0 -70
- data/lib/active_support/core_ext/struct.rb +0 -3
- data/lib/active_support/core_ext/time/marshal.rb +0 -3
Binary file
|
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
1
|
+
require "time"
|
2
|
+
require "base64"
|
3
|
+
require "bigdecimal"
|
4
|
+
require "active_support/core_ext/module/delegation"
|
5
|
+
require "active_support/core_ext/string/inflections"
|
6
|
+
require "active_support/core_ext/date_time/calculations"
|
7
7
|
|
8
8
|
module ActiveSupport
|
9
9
|
# = XmlMini
|
@@ -20,11 +20,11 @@ module ActiveSupport
|
|
20
20
|
attr_writer :original_filename, :content_type
|
21
21
|
|
22
22
|
def original_filename
|
23
|
-
@original_filename ||
|
23
|
+
@original_filename || "untitled"
|
24
24
|
end
|
25
25
|
|
26
26
|
def content_type
|
27
|
-
@content_type ||
|
27
|
+
@content_type || "application/octet-stream"
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
@@ -73,7 +73,7 @@ module ActiveSupport
|
|
73
73
|
begin
|
74
74
|
BigDecimal(number)
|
75
75
|
rescue ArgumentError
|
76
|
-
BigDecimal(
|
76
|
+
BigDecimal("0")
|
77
77
|
end
|
78
78
|
else
|
79
79
|
BigDecimal(number)
|
@@ -96,7 +96,7 @@ module ActiveSupport
|
|
96
96
|
attr_accessor :depth
|
97
97
|
self.depth = 100
|
98
98
|
|
99
|
-
delegate :parse, :
|
99
|
+
delegate :parse, to: :backend
|
100
100
|
|
101
101
|
def backend
|
102
102
|
current_thread_backend || @backend
|
@@ -118,7 +118,7 @@ module ActiveSupport
|
|
118
118
|
|
119
119
|
def to_tag(key, value, options)
|
120
120
|
type_name = options.delete(:type)
|
121
|
-
merged_options = options.merge(:
|
121
|
+
merged_options = options.merge(root: key, skip_instruct: true)
|
122
122
|
|
123
123
|
if value.is_a?(::Method) || value.is_a?(::Proc)
|
124
124
|
if value.arity == 1
|
@@ -136,7 +136,7 @@ module ActiveSupport
|
|
136
136
|
|
137
137
|
key = rename_key(key.to_s, options)
|
138
138
|
|
139
|
-
attributes = options[:skip_types] || type_name.nil? ? {
|
139
|
+
attributes = options[:skip_types] || type_name.nil? ? {} : { type: type_name }
|
140
140
|
attributes[:nil] = true if value.nil?
|
141
141
|
|
142
142
|
encoding = options[:encoding] || DEFAULT_ENCODINGS[type_name]
|
@@ -159,33 +159,31 @@ module ActiveSupport
|
|
159
159
|
key
|
160
160
|
end
|
161
161
|
|
162
|
-
|
163
|
-
|
164
|
-
def _dasherize(key)
|
165
|
-
# $2 must be a non-greedy regex for this to work
|
166
|
-
left, middle, right = /\A(_*)(.*?)(_*)\Z/.match(key.strip)[1,3]
|
167
|
-
"#{left}#{middle.tr('_ ', '--')}#{right}"
|
168
|
-
end
|
162
|
+
private
|
169
163
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
::Base64.decode64(bin)
|
175
|
-
else
|
176
|
-
bin
|
164
|
+
def _dasherize(key)
|
165
|
+
# $2 must be a non-greedy regex for this to work
|
166
|
+
left, middle, right = /\A(_*)(.*?)(_*)\Z/.match(key.strip)[1, 3]
|
167
|
+
"#{left}#{middle.tr('_ ', '--')}#{right}"
|
177
168
|
end
|
178
|
-
end
|
179
169
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
170
|
+
# TODO: Add support for other encodings
|
171
|
+
def _parse_binary(bin, entity)
|
172
|
+
case entity["encoding"]
|
173
|
+
when "base64"
|
174
|
+
::Base64.decode64(bin)
|
175
|
+
else
|
176
|
+
bin
|
177
|
+
end
|
178
|
+
end
|
187
179
|
|
188
|
-
|
180
|
+
def _parse_file(file, entity)
|
181
|
+
f = StringIO.new(::Base64.decode64(file))
|
182
|
+
f.extend(FileLike)
|
183
|
+
f.original_filename = entity["name"]
|
184
|
+
f.content_type = entity["content_type"]
|
185
|
+
f
|
186
|
+
end
|
189
187
|
|
190
188
|
def current_thread_backend
|
191
189
|
Thread.current[:xml_mini_backend]
|
@@ -205,5 +203,5 @@ module ActiveSupport
|
|
205
203
|
end
|
206
204
|
end
|
207
205
|
|
208
|
-
XmlMini.backend =
|
206
|
+
XmlMini.backend = "REXML"
|
209
207
|
end
|
@@ -1,9 +1,9 @@
|
|
1
|
-
raise "JRuby is required to use the JDOM backend for XmlMini" unless RUBY_PLATFORM
|
1
|
+
raise "JRuby is required to use the JDOM backend for XmlMini" unless RUBY_PLATFORM.include?("java")
|
2
2
|
|
3
|
-
require
|
3
|
+
require "jruby"
|
4
4
|
include Java
|
5
5
|
|
6
|
-
require
|
6
|
+
require "active_support/core_ext/object/blank"
|
7
7
|
|
8
8
|
java_import javax.xml.parsers.DocumentBuilder unless defined? DocumentBuilder
|
9
9
|
java_import javax.xml.parsers.DocumentBuilderFactory unless defined? DocumentBuilderFactory
|
@@ -16,7 +16,7 @@ module ActiveSupport
|
|
16
16
|
module XmlMini_JDOM #:nodoc:
|
17
17
|
extend self
|
18
18
|
|
19
|
-
CONTENT_KEY =
|
19
|
+
CONTENT_KEY = "__content__".freeze
|
20
20
|
|
21
21
|
NODE_TYPE_NAMES = %w{ATTRIBUTE_NODE CDATA_SECTION_NODE COMMENT_NODE DOCUMENT_FRAGMENT_NODE
|
22
22
|
DOCUMENT_NODE DOCUMENT_TYPE_NODE ELEMENT_NODE ENTITY_NODE ENTITY_REFERENCE_NODE NOTATION_NODE
|
@@ -46,136 +46,136 @@ module ActiveSupport
|
|
46
46
|
xml_string_reader = StringReader.new(data)
|
47
47
|
xml_input_source = InputSource.new(xml_string_reader)
|
48
48
|
doc = @dbf.new_document_builder.parse(xml_input_source)
|
49
|
-
merge_element!({CONTENT_KEY =>
|
49
|
+
merge_element!({ CONTENT_KEY => "" }, doc.document_element, XmlMini.depth)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
private
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
def delete_empty(hash)
|
68
|
-
hash.delete(CONTENT_KEY) if hash[CONTENT_KEY] == ''
|
69
|
-
end
|
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
|
70
66
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
# The document element to be collapsed.
|
75
|
-
def collapse(element, depth)
|
76
|
-
hash = get_attributes(element)
|
67
|
+
def delete_empty(hash)
|
68
|
+
hash.delete(CONTENT_KEY) if hash[CONTENT_KEY] == ""
|
69
|
+
end
|
77
70
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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)
|
83
88
|
end
|
84
|
-
merge_texts!(hash, element) unless empty_content?(element)
|
85
|
-
hash
|
86
|
-
else
|
87
|
-
merge_texts!(hash, element)
|
88
89
|
end
|
89
|
-
end
|
90
90
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
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
|
105
106
|
end
|
106
|
-
end
|
107
107
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
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]
|
123
128
|
else
|
124
|
-
hash[key] =
|
129
|
+
hash[key] = value
|
125
130
|
end
|
126
|
-
|
127
|
-
hash[key] = [value]
|
128
|
-
else
|
129
|
-
hash[key] = value
|
131
|
+
hash
|
130
132
|
end
|
131
|
-
hash
|
132
|
-
end
|
133
133
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
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
|
145
147
|
end
|
146
|
-
attribute_hash
|
147
|
-
end
|
148
148
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
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
|
160
161
|
end
|
162
|
+
texts
|
161
163
|
end
|
162
|
-
texts
|
163
|
-
end
|
164
164
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
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
|
176
177
|
end
|
178
|
+
text.strip.length == 0
|
177
179
|
end
|
178
|
-
text.strip.length == 0
|
179
|
-
end
|
180
180
|
end
|
181
181
|
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "libxml"
|
2
|
+
require "active_support/core_ext/object/blank"
|
3
|
+
require "stringio"
|
4
4
|
|
5
5
|
module ActiveSupport
|
6
6
|
module XmlMini_LibXML #:nodoc:
|
@@ -11,7 +11,7 @@ module ActiveSupport
|
|
11
11
|
# XML Document string or IO to parse
|
12
12
|
def parse(data)
|
13
13
|
if !data.respond_to?(:read)
|
14
|
-
data = StringIO.new(data ||
|
14
|
+
data = StringIO.new(data || "")
|
15
15
|
end
|
16
16
|
|
17
17
|
if data.eof?
|
@@ -20,7 +20,6 @@ module ActiveSupport
|
|
20
20
|
LibXML::XML::Parser.io(data).parse.to_hash
|
21
21
|
end
|
22
22
|
end
|
23
|
-
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
@@ -33,20 +32,20 @@ module LibXML #:nodoc:
|
|
33
32
|
end
|
34
33
|
|
35
34
|
module Node #:nodoc:
|
36
|
-
CONTENT_ROOT =
|
35
|
+
CONTENT_ROOT = "__content__".freeze
|
37
36
|
|
38
37
|
# Convert XML document to hash.
|
39
38
|
#
|
40
39
|
# hash::
|
41
40
|
# Hash to merge the converted element into.
|
42
|
-
def to_hash(hash={})
|
41
|
+
def to_hash(hash = {})
|
43
42
|
node_hash = {}
|
44
43
|
|
45
44
|
# Insert node hash into parent hash correctly.
|
46
45
|
case hash[name]
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
when Array then hash[name] << node_hash
|
47
|
+
when Hash then hash[name] = [hash[name], node_hash]
|
48
|
+
when nil then hash[name] = node_hash
|
50
49
|
end
|
51
50
|
|
52
51
|
# Handle child elements
|
@@ -54,7 +53,7 @@ module LibXML #:nodoc:
|
|
54
53
|
if c.element?
|
55
54
|
c.to_hash(node_hash)
|
56
55
|
elsif c.text? || c.cdata?
|
57
|
-
node_hash[CONTENT_ROOT] ||=
|
56
|
+
node_hash[CONTENT_ROOT] ||= ""
|
58
57
|
node_hash[CONTENT_ROOT] << c.content
|
59
58
|
end
|
60
59
|
end
|
@@ -73,5 +72,7 @@ module LibXML #:nodoc:
|
|
73
72
|
end
|
74
73
|
end
|
75
74
|
|
75
|
+
# :enddoc:
|
76
|
+
|
76
77
|
LibXML::XML::Document.include(LibXML::Conversions::Document)
|
77
78
|
LibXML::XML::Node.include(LibXML::Conversions::Node)
|