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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4509fa336291a4bd13e096f37d9b23f8b745bd07
4
- data.tar.gz: 6ddf6a6eba7193d428d49c7c6a7dfe2f8f802596
3
+ metadata.gz: b6ed93865b0f2b7c5327619514216e72ce8d5084
4
+ data.tar.gz: 586579b119a845fc2df419087a4cb3db1ca15e7f
5
5
  SHA512:
6
- metadata.gz: 0d50edfc8f5555fe8a8bc8b6c68c80ea3a3c2ad717ddebd90e0d69ccde953d8fff36b44e718facb895d938a8e120de9c64a8e5629ff4c9c448c7f9d4896c4bf2
7
- data.tar.gz: f655177e4243493085c021fe585abd379b706b08ce5f8b6dd9b56d94ebdd7bc09f0867f92fef286d54879208563bd336fed31ed13c97500d20ad78dc6742456c
6
+ metadata.gz: cc7d414cfa666b698777cccbfac4da9cf518c95cca6e41ab30d9972bb6540c63e1f64047dd37d316117a54912e1a18f789259b6461a500fcb139fb0fa8250c14
7
+ data.tar.gz: 89d67db87446b6f936daa05a3cb38809c0ba18d505457312d35b387b79f9d81d37a79141ccddda4f2dc9fddd306fd0aba6a29f5f31f5c6b5df3db20352982535
@@ -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(whitespace)
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, is_first=true)
70
+ def format_attrs(tag, attrs, whitespace)
57
71
  output = ''
72
+ is_first = true
58
73
  attrs.each do |key, value|
59
- next if value.nil?
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}#{attrs(whitespace)}"
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?
@@ -1,3 +1,3 @@
1
1
  module AndroidXml
2
- Version = '1.2.4'
2
+ Version = '1.3.0'
3
3
  end
@@ -23,4 +23,59 @@ describe 'tag attributes' do
23
23
  expect(xml.to_s).to match %r{<any-tag android:tag="value-&quot;&apos;&lt;&gt;&amp;-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
@@ -0,0 +1,19 @@
1
+ describe 'Numeric extensions' do
2
+
3
+ %w{
4
+ pt
5
+ mm
6
+ in
7
+ px
8
+ dp
9
+ dip
10
+ sp
11
+ }.each do |ext|
12
+ it "should support ###.#{ext}" do
13
+ [0, 1, 1.5].each do |num|
14
+ expect(num.send(ext)).to eql("#{num}#{ext}")
15
+ end
16
+ end
17
+ end
18
+
19
+ end
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.2.4
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