document_builder 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/document_builder/model.rb +6 -9
- data/lib/document_builder/version.rb +1 -1
- data/spec/document_builder/model_spec.rb +50 -28
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NDQ1MmRhN2FkMzIwNGEyZTcwYjk3OWE4YjRlMDc5NzBiNmM3M2ZkOA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ODk4OWExNDgwN2ZkYWE3ZDFlZTlmZjliNzZlOWNkMmE0Y2YzMTA3NQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YzVkYWE3NzA1MjBlN2FlMDRkOTBmODIwYjdhZjE2MWNhMDNlMjZjYzlhZTU2
|
10
|
+
ZGNkZjcwOWQ4NDYxYTViNTQ0Y2VhZDIyZTEwYjNkOTI4NGRlYTgyMDlhYjBl
|
11
|
+
MjY2MDJhYzE4ZGNlZWZhNjIyOTE3NDljNWMyZjdkZDM5NTJhOWY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
N2M0NDk3M2I0ZmU4MThlNmNkODQ2OTAzNzdhZDBjMWE4NGFlMTg3MTJiOWM3
|
14
|
+
MzAwOWUzZTg4Y2MzYWY1ZTlhMDEyODc5MWQ2ZDlhMmI1ZDVkZGNjYzRlNDdl
|
15
|
+
MTNhNTEyOWJkNjUyZjY3YTRhMmQ1MTc3MjljY2U4ODNiZjA3NjI=
|
@@ -1,9 +1,12 @@
|
|
1
1
|
module DocumentBuilder
|
2
2
|
module Model
|
3
3
|
module ClassMethods
|
4
|
-
def
|
4
|
+
def attributes
|
5
5
|
@attributes ||= {}
|
6
|
-
|
6
|
+
end
|
7
|
+
|
8
|
+
def add_attribute(name, attribute)
|
9
|
+
attributes[name.to_sym] = attribute
|
7
10
|
end
|
8
11
|
|
9
12
|
def property(name, selector: nil, type: nil)
|
@@ -22,12 +25,6 @@ module DocumentBuilder
|
|
22
25
|
@root = selector
|
23
26
|
end
|
24
27
|
|
25
|
-
def inherited(subclass)
|
26
|
-
subclass.instance_variable_set(:@attributes, @attributes)
|
27
|
-
subclass.instance_variable_set(:@root, @root)
|
28
|
-
super
|
29
|
-
end
|
30
|
-
|
31
28
|
def call(document)
|
32
29
|
self.new(document)
|
33
30
|
end
|
@@ -57,7 +54,7 @@ module DocumentBuilder
|
|
57
54
|
end
|
58
55
|
|
59
56
|
def attributes
|
60
|
-
self.class.
|
57
|
+
self.class.attributes
|
61
58
|
end
|
62
59
|
|
63
60
|
def get_attribute(name)
|
@@ -2,19 +2,9 @@ require 'spec_helper'
|
|
2
2
|
require 'nokogiri'
|
3
3
|
|
4
4
|
RSpec.describe DocumentBuilder::Model do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
<inner-list-item>Inner Item 1</inner-list-item>
|
9
|
-
<inner-list-item>Inner Item 2</inner-list-item>
|
10
|
-
</collection>
|
11
|
-
<outer-list-item>Outer Item 1</outer-list-item>
|
12
|
-
<outer-list-item>Outer Item 2</outer-list-item>
|
13
|
-
<property>Some Property</property>
|
14
|
-
</root>
|
15
|
-
XML
|
16
|
-
|
17
|
-
let(:document) { Nokogiri::XML(DOCUMENT) }
|
5
|
+
class EmptyDocument
|
6
|
+
include DocumentBuilder::Model
|
7
|
+
end
|
18
8
|
|
19
9
|
class ListItem
|
20
10
|
include DocumentBuilder::Model
|
@@ -29,25 +19,57 @@ RSpec.describe DocumentBuilder::Model do
|
|
29
19
|
property :property, selector: "//property", type: TextProperty
|
30
20
|
end
|
31
21
|
|
32
|
-
|
22
|
+
context 'for an xml document' do
|
23
|
+
let(:xml) {
|
24
|
+
<<-XML
|
25
|
+
<root>
|
26
|
+
<collection>
|
27
|
+
<inner-list-item>Inner Item 1</inner-list-item>
|
28
|
+
<inner-list-item>Inner Item 2</inner-list-item>
|
29
|
+
</collection>
|
30
|
+
<outer-list-item>Outer Item 1</outer-list-item>
|
31
|
+
<outer-list-item>Outer Item 2</outer-list-item>
|
32
|
+
<property>Some Property</property>
|
33
|
+
</root>
|
34
|
+
XML
|
35
|
+
}
|
33
36
|
|
34
|
-
|
35
|
-
expect(subject.root).to eq "//root"
|
36
|
-
end
|
37
|
+
let(:document) { Nokogiri::XML(xml) }
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
subject { SomeDocument.new(document) }
|
40
|
+
|
41
|
+
it 'returns the root' do
|
42
|
+
expect(subject.root).to eq "//root"
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
it 'returns an innner collection' do
|
46
|
+
expect(subject.collection).to be_a DocumentBuilder::Collection
|
47
|
+
expect(subject.collection.first).to be_a ListItem
|
48
|
+
expect(subject.collection.first.name).to eq "Inner Item 1"
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'returns an outer collection' do
|
52
|
+
expect(subject.outer_collection).to be_a DocumentBuilder::Collection
|
53
|
+
expect(subject.outer_collection.first).to be_a ListItem
|
54
|
+
expect(subject.outer_collection.first.name).to eq "Outer Item 1"
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'returns a property' do
|
58
|
+
expect(subject.property).to eq "Some Property"
|
59
|
+
end
|
48
60
|
end
|
49
61
|
|
50
|
-
|
51
|
-
|
62
|
+
|
63
|
+
context 'for an empty document' do
|
64
|
+
let(:document) { Nokogiri::XML("") }
|
65
|
+
subject { EmptyDocument.new(document) }
|
66
|
+
|
67
|
+
it 'returns an empty document' do
|
68
|
+
expect(subject).to be_a EmptyDocument
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'is serialized' do
|
72
|
+
expect(subject.to_s).to be_a String
|
73
|
+
end
|
52
74
|
end
|
53
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: document_builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Schmitz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|