xml_mapper 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +39 -16
- data/VERSION +1 -1
- data/lib/xml_mapper.rb +1 -0
- data/spec/xml_mapper_spec.rb +1 -1
- data/xml_mapper.gemspec +1 -1
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -15,17 +15,27 @@ Declarative mapping of XML files to ruby attributes
|
|
15
15
|
<name>Mos Def</name>
|
16
16
|
<id>1212</id>
|
17
17
|
</artist>
|
18
|
+
<contributions>
|
19
|
+
<artist>Mos Def</artist>
|
20
|
+
<producer>DJ Premier</producer>
|
21
|
+
</contributions>
|
18
22
|
<tracks>
|
19
|
-
<track>
|
23
|
+
<track code="1234">
|
20
24
|
<title>Fear Not of Man</title>
|
21
25
|
<number>1</number>
|
22
26
|
<disk>1</number>
|
23
27
|
<explicit_lyrics />
|
28
|
+
<not_streamable_in>
|
29
|
+
<country>de</country>
|
30
|
+
</not_streamable_in>
|
24
31
|
</track>
|
25
|
-
<track>
|
32
|
+
<track code="2345">
|
26
33
|
<title>Hip Hop</title>
|
27
34
|
<number>2</number>
|
28
35
|
<disk>1</number>
|
36
|
+
<not_streamable_in>
|
37
|
+
<country>at</country>
|
38
|
+
</not_streamable_in>
|
29
39
|
</track>
|
30
40
|
</tracks>
|
31
41
|
</album>
|
@@ -41,44 +51,57 @@ Declarative mapping of XML files to ruby attributes
|
|
41
51
|
text :country, :after_map => :upcase # calls after_map method on extracted value if value responds to the method
|
42
52
|
text :released_on, :after_map => :parse_date # calls after_map method defined in Mapper class when value does not respond
|
43
53
|
boolean :allows_streaming # maps Y, y, yes, true => true, N, n, no, false => false
|
44
|
-
|
54
|
+
|
45
55
|
within :artist do
|
46
|
-
text :name => :artist_name # adds mapping for xpath "artist/name"
|
56
|
+
text :name => :artist_name # adds mapping for xpath "artist/name"
|
47
57
|
integer :id => :artist_id
|
48
58
|
end
|
49
|
-
|
59
|
+
|
60
|
+
many "contributions/*" => :contributions do # use the name of xml nodes for mappings
|
61
|
+
node_name :role # map name of xml node to :role
|
62
|
+
inner_text :name # map inner_text of xml node to :name
|
63
|
+
end
|
64
|
+
|
50
65
|
many "tracks/track" => :tracks do # maps xpath "tracks/track" to array with key :tracks
|
66
|
+
attribute :code => :isrc # map xml attribute "code" to :isrc
|
51
67
|
text :title => :track_title
|
52
68
|
integer :number => :track_number
|
53
69
|
integer :disk => :disk_number
|
54
|
-
exists :explicit_lyrics
|
70
|
+
exists :explicit_lyrics # checks if a node with the xpath exists
|
71
|
+
not_exists "not_streamable_in/country[text()='de']" => :allows_streaming
|
55
72
|
end
|
56
|
-
|
73
|
+
|
57
74
|
after_map do # is called after attributes are extracted, self references the extracted attributes
|
58
75
|
self[:tracks_count] = self[:tracks].length
|
59
76
|
end
|
60
|
-
|
77
|
+
|
61
78
|
# to be used for after_map callbacks
|
62
79
|
def parse_date(date_string)
|
63
80
|
Date.parse(date_string)
|
64
81
|
end
|
65
82
|
end
|
66
83
|
|
67
|
-
|
68
84
|
== How to call
|
69
85
|
|
70
86
|
* MyMapper.attributes_from_xml(xml)
|
71
87
|
* MyMapper.attributes_from_xml_path(xml) (also sets :xml_path)
|
72
88
|
|
73
89
|
== Output
|
74
|
-
|
75
|
-
|
76
|
-
:
|
77
|
-
:
|
78
|
-
{:
|
79
|
-
{:disk_number=>1, :explicit_lyrics=>true, :track_title=>"Hip Hop", :track_number=>2}
|
90
|
+
{ :title=>"Black on Both Sides", :allows_streaming=>true, :version_title=>"Extended Edition",
|
91
|
+
:xml_path=>"/Users/tobias/Projects/xml_mapper/spec/fixtures/base.xml", :tracks_count=>2, :released_in=>1999,
|
92
|
+
:released_on=>#<Date: 4902927/2,0,2299161>, :artist_name=>"Mos Def",
|
93
|
+
:contributions=>[
|
94
|
+
{:name=>"Mos Def", :role=>"artist"}, {:name=>"DJ Premier", :role=>"producer"}
|
80
95
|
],
|
81
|
-
:
|
96
|
+
:artist_id=>1212, :country=>"DE",
|
97
|
+
:tracks=>[
|
98
|
+
{ :track_title=>"Fear Not of Man", :allows_streaming=>false, :track_number=>1, :disk_number=>1,
|
99
|
+
:explicit_lyrics=>true, :isrc=>"1234"
|
100
|
+
},
|
101
|
+
{ :track_title=>"Hip Hop", :allows_streaming=>true, :track_number=>2, :disk_number=>1,
|
102
|
+
:explicit_lyrics=>false, :isrc=>"2345"
|
103
|
+
}
|
104
|
+
]
|
82
105
|
}
|
83
106
|
|
84
107
|
== Contributing to xml_mapper
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
data/lib/xml_mapper.rb
CHANGED
data/spec/xml_mapper_spec.rb
CHANGED
@@ -314,7 +314,7 @@ describe "XmlMapper" do
|
|
314
314
|
describe "#string_to_boolean" do
|
315
315
|
{
|
316
316
|
"true" => true, "false" => false, "y" => true, "TRUE" => true, "" => nil, "YES" => true, "yes" => true,
|
317
|
-
"n" => false, "1" => true, "0" => false
|
317
|
+
"n" => false, "1" => true, "0" => false, "No" => false, "NO" => false
|
318
318
|
}.each do |value, result|
|
319
319
|
it "converts #{value.inspect} to #{result}" do
|
320
320
|
@mapper.string_to_boolean(value).should == result
|
data/xml_mapper.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 1
|
10
|
+
version: 0.4.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tobias Schwab
|