xml-magic 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +9 -1
- data/lib/common_thread/xml/xml_magic.rb +6 -13
- data/test/test_xml_magic.rb +24 -15
- metadata +2 -2
data/README
CHANGED
@@ -19,4 +19,12 @@ puts project_info[:title]
|
|
19
19
|
puts project_info.description
|
20
20
|
for contact in project_info.contact
|
21
21
|
puts "#{contact} the #{contact[:type]}"
|
22
|
-
end
|
22
|
+
end
|
23
|
+
|
24
|
+
== example output
|
25
|
+
|
26
|
+
XML Magic
|
27
|
+
Test description.
|
28
|
+
Anthony the Project Manager
|
29
|
+
Ben the Worker Bee
|
30
|
+
Jason the Designer Bee
|
@@ -1,8 +1,13 @@
|
|
1
1
|
module CommonThread
|
2
2
|
module XML
|
3
|
+
# Credit to Jim Weirich at http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc
|
4
|
+
class BlankSlate
|
5
|
+
instance_methods.each { |m| undef_method m unless m =~ /^__/ }
|
6
|
+
end
|
7
|
+
|
3
8
|
# Class that makes accessing xml objects more like any other ruby object
|
4
9
|
# thanks to the magic of method missing
|
5
|
-
class XmlMagic
|
10
|
+
class XmlMagic < BlankSlate
|
6
11
|
require 'rexml/document'
|
7
12
|
|
8
13
|
def initialize(xml, namespace="")
|
@@ -15,18 +20,10 @@ module CommonThread
|
|
15
20
|
@namespace = namespace
|
16
21
|
end
|
17
22
|
|
18
|
-
def class(selection=nil)
|
19
|
-
evaluate("class", selection)
|
20
|
-
end
|
21
|
-
|
22
23
|
def each
|
23
24
|
@element.each {|e| yield CommonThread::XML::XmlMagic.new(e, @namespace)}
|
24
25
|
end
|
25
26
|
|
26
|
-
def id(selection=nil)
|
27
|
-
evaluate("id", selection)
|
28
|
-
end
|
29
|
-
|
30
27
|
def method_missing(method, selection=nil)
|
31
28
|
evaluate(method.to_s, selection)
|
32
29
|
end
|
@@ -47,10 +44,6 @@ module CommonThread
|
|
47
44
|
end
|
48
45
|
end
|
49
46
|
|
50
|
-
def type(selection=nil)
|
51
|
-
evaluate("type", selection)
|
52
|
-
end
|
53
|
-
|
54
47
|
def [](index, count = nil)
|
55
48
|
if index.is_a?(Fixnum) or index.is_a?(Bignum) or index.is_a?(Integer) or index.is_a?(Range)
|
56
49
|
if @element.is_a?(Array)
|
data/test/test_xml_magic.rb
CHANGED
@@ -6,22 +6,31 @@ class TestXmlMagic < Test::Unit::TestCase
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def test_xml_magic
|
9
|
-
x = CommonThread::XML::XmlMagic.new('<project title="
|
10
|
-
|
11
|
-
|
12
|
-
assert_equal("
|
13
|
-
x.contact.
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
assert_equal(
|
19
|
-
assert_equal("AnthonyBenJason", x.contact.to_s)
|
20
|
-
assert_equal(nil, x.note)
|
21
|
-
assert_equal(nil, x.content)
|
9
|
+
x = CommonThread::XML::XmlMagic.new('<project title="Xml Magic" class="CommonThread::XML::XmlMagic"><media:content>This is the content.</media:content><type>New</type><contact name="Anthony">Anthony</contact><contact name="Ben">Ben</contact><contact name="Jason">Jason</contact><description>Test description.</description></project>')
|
10
|
+
|
11
|
+
# Elements without namespaces
|
12
|
+
assert_equal("Test description.", x.description.to_s, "Element text not retrieved for to_s.")
|
13
|
+
assert_equal("AnthonyBenJason", x.contact.to_s, "Multiple element text not retrieved for to_s.")
|
14
|
+
assert_equal(nil, x.note, "Element selector should return nil when there are no matches.")
|
15
|
+
assert_equal("New", x.type.to_s, "Element names clash with inherited Object methods and properties")
|
16
|
+
|
17
|
+
# Namespaced elements
|
18
|
+
assert_equal(nil, x.content, "Namespaced elements should return nil when namespace is not provided.")
|
22
19
|
x.namespace = "media"
|
23
|
-
assert_equal("This is the content.", x.content.to_s)
|
20
|
+
assert_equal("This is the content.", x.content.to_s, "Namespaced element selection is broken.")
|
24
21
|
x.namespace = ""
|
25
|
-
assert_equal(nil, x.content)
|
22
|
+
assert_equal(nil, x.content, "Namespace was not removed from selection.")
|
23
|
+
|
24
|
+
# Element attributes
|
25
|
+
assert_equal("Xml Magic", x[:title], "Invalid attribute value.")
|
26
|
+
assert_equal("CommonThread::XML::XmlMagic", x[:class], "Attribute names clash with inherited Object methods and properties")
|
27
|
+
|
28
|
+
# Selector matches multiple elements
|
29
|
+
assert_equal("Anthony", x.contact[0][:name], "Multiple elements not quacking like an Array.")
|
30
|
+
x.contact.each do |contact|
|
31
|
+
assert(contact[:name] != nil, "Attribute name should not be nil.")
|
32
|
+
assert(contact[:phone] == nil, "Attribute phone should be nil.")
|
33
|
+
end
|
34
|
+
assert_equal(3, x.contact(:count), "Multiple element count is wrong.")
|
26
35
|
end
|
27
36
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml-magic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anthony Crumley
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2008-03-
|
13
|
+
date: 2008-03-15 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|