android-xml 1.2.4 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/android-xml/tag.rb +23 -6
- data/lib/android-xml/version.rb +1 -1
- data/spec/attrs_spec.rb +55 -0
- data/spec/ext_spec.rb +19 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6ed93865b0f2b7c5327619514216e72ce8d5084
|
4
|
+
data.tar.gz: 586579b119a845fc2df419087a4cb3db1ca15e7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc7d414cfa666b698777cccbfac4da9cf518c95cca6e41ab30d9972bb6540c63e1f64047dd37d316117a54912e1a18f789259b6461a500fcb139fb0fa8250c14
|
7
|
+
data.tar.gz: 89d67db87446b6f936daa05a3cb38809c0ba18d505457312d35b387b79f9d81d37a79141ccddda4f2dc9fddd306fd0aba6a29f5f31f5c6b5df3db20352982535
|
data/lib/android-xml/tag.rb
CHANGED
@@ -5,7 +5,7 @@ module AndroidXml
|
|
5
5
|
|
6
6
|
def initialize(tag, attrs={}, &block)
|
7
7
|
@buffer = []
|
8
|
-
@attrs = {}.merge(attrs)
|
8
|
+
@attrs = {}.merge(flatten_attrs(attrs))
|
9
9
|
@raw_tag = tag.to_s
|
10
10
|
|
11
11
|
if rename = Setup.tags[tag.to_s][:rename]
|
@@ -25,7 +25,7 @@ module AndroidXml
|
|
25
25
|
|
26
26
|
def clone(attrs={}, &block)
|
27
27
|
block ||= @generate
|
28
|
-
Tag.new(@tag, @attrs.merge(attrs), &block)
|
28
|
+
Tag.new(@tag, @attrs.merge(flatten_attrs(attrs)), &block)
|
29
29
|
end
|
30
30
|
|
31
31
|
def include(tag, &block)
|
@@ -39,7 +39,21 @@ module AndroidXml
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
def attrs
|
42
|
+
def flatten_attrs(attrs, prefix='')
|
43
|
+
flattened = {}
|
44
|
+
attrs.each do |key, value|
|
45
|
+
key = "#{prefix}#{key}"
|
46
|
+
if value.is_a?(Hash)
|
47
|
+
flattened.merge!(flatten_attrs(value, "#{key}_"))
|
48
|
+
else
|
49
|
+
flattened[key] = value
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
flattened
|
54
|
+
end
|
55
|
+
|
56
|
+
def generate_attrs(whitespace)
|
43
57
|
attrs = {}
|
44
58
|
|
45
59
|
attrs.merge!(Setup.all_tag[:defaults])
|
@@ -53,10 +67,13 @@ module AndroidXml
|
|
53
67
|
format_attrs(@tag, attrs, whitespace)
|
54
68
|
end
|
55
69
|
|
56
|
-
def format_attrs(tag, attrs, whitespace
|
70
|
+
def format_attrs(tag, attrs, whitespace)
|
57
71
|
output = ''
|
72
|
+
is_first = true
|
58
73
|
attrs.each do |key, value|
|
59
|
-
|
74
|
+
if value.nil?
|
75
|
+
next
|
76
|
+
end
|
60
77
|
|
61
78
|
key = key.to_s
|
62
79
|
|
@@ -102,7 +119,7 @@ module AndroidXml
|
|
102
119
|
|
103
120
|
def generate(tab='')
|
104
121
|
whitespace = "#{tab} #{' ' * @tag.length}"
|
105
|
-
output = "#{tab}<#{@tag}#{
|
122
|
+
output = "#{tab}<#{@tag}#{generate_attrs(whitespace)}"
|
106
123
|
if @generate
|
107
124
|
inside = generate_block(tab + Setup.tab)
|
108
125
|
if !inside || inside.strip.empty?
|
data/lib/android-xml/version.rb
CHANGED
data/spec/attrs_spec.rb
CHANGED
@@ -23,4 +23,59 @@ describe 'tag attributes' do
|
|
23
23
|
expect(xml.to_s).to match %r{<any-tag android:tag="value-"'<>&-with-quoted" />}
|
24
24
|
end
|
25
25
|
|
26
|
+
it 'should generate prefixed attributes when using a hash' do
|
27
|
+
xml = AndroidXml.file('tmp/test.xml') do
|
28
|
+
any_tag(layout: {
|
29
|
+
width: 'match_parent',
|
30
|
+
height: 'match_parent',
|
31
|
+
padding: 6.dp,
|
32
|
+
})
|
33
|
+
end
|
34
|
+
|
35
|
+
expect(xml.to_s).to eql <<-XML
|
36
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
37
|
+
<any-tag android:layout_width="match_parent"
|
38
|
+
android:layout_height="match_parent"
|
39
|
+
android:layout_padding="6dp" />
|
40
|
+
XML
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should properly clone a tag with nested attrs' do
|
44
|
+
any_tag = AndroidXml.any_tag(layout: {
|
45
|
+
width: 'match_parent',
|
46
|
+
height: 'match_parent',
|
47
|
+
padding: 6.dp,
|
48
|
+
})
|
49
|
+
|
50
|
+
xml = AndroidXml.file('tmp/test.xml') do
|
51
|
+
resources do
|
52
|
+
include any_tag.clone
|
53
|
+
include any_tag.clone layout_height: 'wrap_content'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
expect(any_tag.to_s).to eql <<-XML
|
58
|
+
<any-tag android:layout_width="match_parent"
|
59
|
+
android:layout_height="match_parent"
|
60
|
+
android:layout_padding="6dp" />
|
61
|
+
XML
|
62
|
+
expect(any_tag.clone(layout_height: 'wrap_content').to_s).to eql <<-XML
|
63
|
+
<any-tag android:layout_width="match_parent"
|
64
|
+
android:layout_height="wrap_content"
|
65
|
+
android:layout_padding="6dp" />
|
66
|
+
XML
|
67
|
+
|
68
|
+
expect(xml.to_s).to eql <<-XML
|
69
|
+
<!-- Do not edit this file. It was generated by AndroidXml. -->
|
70
|
+
<resources>
|
71
|
+
<any-tag android:layout_width="match_parent"
|
72
|
+
android:layout_height="match_parent"
|
73
|
+
android:layout_padding="6dp" />
|
74
|
+
<any-tag android:layout_width="match_parent"
|
75
|
+
android:layout_height="wrap_content"
|
76
|
+
android:layout_padding="6dp" />
|
77
|
+
</resources>
|
78
|
+
XML
|
79
|
+
end
|
80
|
+
|
26
81
|
end
|
data/spec/ext_spec.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: android-xml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Colin T.A. Gray
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- spec/attrs_spec.rb
|
51
51
|
- spec/clone_tag_spec.rb
|
52
52
|
- spec/create_tag_spec.rb
|
53
|
+
- spec/ext_spec.rb
|
53
54
|
- spec/factory_spec.rb
|
54
55
|
- spec/generate_spec.rb
|
55
56
|
- spec/inc_tag_spec.rb
|
@@ -86,6 +87,7 @@ test_files:
|
|
86
87
|
- spec/attrs_spec.rb
|
87
88
|
- spec/clone_tag_spec.rb
|
88
89
|
- spec/create_tag_spec.rb
|
90
|
+
- spec/ext_spec.rb
|
89
91
|
- spec/factory_spec.rb
|
90
92
|
- spec/generate_spec.rb
|
91
93
|
- spec/inc_tag_spec.rb
|