xml-mixup 0.1.9 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/lib/xml/mixup.rb +21 -12
- data/lib/xml/mixup/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74f33a7307a517d3447e6cc3e41b79a964d7002dcd41118f20f77854ad20c7c9
|
4
|
+
data.tar.gz: c0fe7162ce9a6b6d0b27880b6127e783bbe2f340358be29e2cc9386345ef6cff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c50bb0074170d8b936bf83084d5d471ab3f6f70703ec87e4c7e1b3f8d02bff4bb557986367f648bfec14f62cdb0a6b7b0ddaa7b88cdc3308e6922eb377b8619d
|
7
|
+
data.tar.gz: 86e2f6b2ba2f89808215e1176031bc2079ff254f425d5c3a8e2d6f9a8f81506e9bc921b9605d531b3708fca92cf3dc18cb3f8ceca6dd8615b76b2eeed1f7deee
|
data/README.md
CHANGED
@@ -148,6 +148,21 @@ flattened like this:
|
|
148
148
|
# => <foo array="a b" hash="c: d e: f"/>
|
149
149
|
```
|
150
150
|
|
151
|
+
Note that attribute values can also be a `Proc`, which are fed
|
152
|
+
arbitrary arguments from the `markup` method. The `Proc` is expected
|
153
|
+
to return something which can subsequently flattened. If an attribute
|
154
|
+
value is `nil` or ultimately resolves to `nil`, or an empty `Array` or
|
155
|
+
`Hash`, that attribute will be omitted. `nil` values in arrays or
|
156
|
+
hashes will also be skipped, as will empty-string values in
|
157
|
+
arrays. This is different behaviour from versions prior to 0.1.10,
|
158
|
+
where `nil` (or, e.g., `[]`) would produce an attribute containing the
|
159
|
+
empty string.
|
160
|
+
|
161
|
+
This change was made to eliminate a lot of clunky logic in application
|
162
|
+
code to determine whether or not to include a given attribute. If you
|
163
|
+
need to render attributes explicitly with empty strings, then
|
164
|
+
explicitly pass in the empty string.
|
165
|
+
|
151
166
|
#### Processing instructions
|
152
167
|
|
153
168
|
```ruby
|
data/lib/xml/mixup.rb
CHANGED
@@ -216,7 +216,7 @@ module XML::Mixup
|
|
216
216
|
# now we dispatch based on the name
|
217
217
|
if name == '#comment'
|
218
218
|
# first up, comments
|
219
|
-
node = doc.create_comment flatten(children, args)
|
219
|
+
node = doc.create_comment flatten(children, args).to_s
|
220
220
|
|
221
221
|
# attach it
|
222
222
|
ADJACENT[adj].call node, nodes[adj]
|
@@ -230,11 +230,12 @@ module XML::Mixup
|
|
230
230
|
content = ''
|
231
231
|
if (c = children[1..children.length]) and c.length > 0
|
232
232
|
#warn c.inspect
|
233
|
-
content = flatten(c, args)
|
233
|
+
content = flatten(c, args).to_s
|
234
234
|
else
|
235
235
|
content = attr.sort.map { |pair|
|
236
|
-
|
237
|
-
|
236
|
+
v = flatten(pair[1], args) or next
|
237
|
+
"#{pair[0].to_s}=\"#{v}\""
|
238
|
+
}.compact.join(' ')
|
238
239
|
end
|
239
240
|
|
240
241
|
node = Nokogiri::XML::ProcessingInstruction.new(doc, target, content)
|
@@ -292,11 +293,12 @@ module XML::Mixup
|
|
292
293
|
ns = {}
|
293
294
|
at = {}
|
294
295
|
attr.each do |k, v|
|
295
|
-
|
296
|
-
|
296
|
+
k = k.to_s
|
297
|
+
v = flatten(v, args) or next
|
298
|
+
if (md = /^xmlns(?::(.*))?$/i.match(k))
|
297
299
|
ns[md[1]] = v
|
298
300
|
else
|
299
|
-
at[k
|
301
|
+
at[k] = v
|
300
302
|
end
|
301
303
|
end
|
302
304
|
|
@@ -545,26 +547,33 @@ module XML::Mixup
|
|
545
547
|
elem.add_namespace((p.nil? ? p : p.to_s), ns[p].to_s)
|
546
548
|
end
|
547
549
|
attr.sort.each do |k, v|
|
548
|
-
|
550
|
+
v = flatten(v, args) or next
|
551
|
+
elem[k.to_s] = v
|
549
552
|
end
|
550
553
|
|
551
554
|
elem
|
552
555
|
end
|
553
556
|
|
554
|
-
ATOMS = [String, Symbol, Numeric,
|
557
|
+
ATOMS = [String, Symbol, Numeric, FalseClass, TrueClass].freeze
|
555
558
|
|
556
559
|
# yo dawg
|
557
560
|
|
558
561
|
def flatten obj, args
|
562
|
+
return if obj.nil?
|
559
563
|
# early bailout for most likely condition
|
560
564
|
if ATOMS.any? { |x| obj.is_a? x }
|
561
565
|
obj.to_s
|
562
566
|
elsif obj.is_a? Hash
|
563
|
-
obj.sort.map
|
567
|
+
tmp = obj.sort.map do |kv|
|
568
|
+
v = flatten(kv[1], args)
|
569
|
+
v.nil? ? nil : "#{kv[0].to_s}: #{v}"
|
570
|
+
end.compact
|
571
|
+
tmp.empty? ? nil : tmp.join(' ')
|
564
572
|
elsif obj.respond_to? :call
|
565
|
-
obj.call(*args)
|
573
|
+
flatten(obj.call(*args), args)
|
566
574
|
elsif obj.respond_to? :map
|
567
|
-
obj.map { |x| flatten(x, args) }.
|
575
|
+
tmp = obj.map { |x| flatten(x, args) }.reject { |x| x.nil? || x == '' }
|
576
|
+
tmp.empty? ? nil : tmp.join(' ')
|
568
577
|
else
|
569
578
|
obj.to_s
|
570
579
|
end
|
data/lib/xml/mixup/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml-mixup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dorian Taylor
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06
|
11
|
+
date: 2019-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
105
|
version: '0'
|
106
106
|
requirements: []
|
107
107
|
rubyforge_project:
|
108
|
-
rubygems_version: 2.7.6
|
108
|
+
rubygems_version: 2.7.6
|
109
109
|
signing_key:
|
110
110
|
specification_version: 4
|
111
111
|
summary: A mixin for (XML) markup
|