xml-magic 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/LICENSE +22 -0
- data/README +22 -0
- data/demo.rb +18 -0
- data/lib/common_thread/xml/xml_magic.rb +99 -0
- data/lib/xml_magic.rb +1 -0
- data/test/test_helper.rb +2 -0
- data/test/test_xml_magic.rb +27 -0
- metadata +61 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2008 CommonThread, LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
xml-magic makes accessing xml objects more like any other ruby object
|
2
|
+
|
3
|
+
== example
|
4
|
+
|
5
|
+
require 'xml_magic'
|
6
|
+
|
7
|
+
xml = <<XML
|
8
|
+
<project title="XML Magic">
|
9
|
+
<description>Test description.</description>
|
10
|
+
<contact type="Project Manager">Anthony</contact>
|
11
|
+
<contact type="Worker Bee">Ben</contact>
|
12
|
+
<contact type="Designer Bee">Jason</contact>
|
13
|
+
</project>
|
14
|
+
XML
|
15
|
+
|
16
|
+
project_info = CommonThread::XML::XmlMagic.new(xml)
|
17
|
+
|
18
|
+
puts project_info[:title]
|
19
|
+
puts project_info.description
|
20
|
+
for contact in project_info.contact
|
21
|
+
puts "#{contact} the #{contact[:type]}"
|
22
|
+
end
|
data/demo.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'lib/common_thread/xml/xml_magic'
|
2
|
+
|
3
|
+
xml = <<XML
|
4
|
+
<project title="XML Magic">
|
5
|
+
<description>Test description.</description>
|
6
|
+
<contact type="Project Manager">Anthony</contact>
|
7
|
+
<contact type="Worker Bee">Ben</contact>
|
8
|
+
<contact type="Designer Bee">Jason</contact>
|
9
|
+
</project>
|
10
|
+
XML
|
11
|
+
|
12
|
+
project_info = CommonThread::XML::XmlMagic.new(xml)
|
13
|
+
|
14
|
+
puts project_info[:title]
|
15
|
+
puts project_info.description
|
16
|
+
for contact in project_info.contact
|
17
|
+
puts "#{contact} the #{contact[:type]}"
|
18
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module CommonThread
|
2
|
+
module XML
|
3
|
+
# Class that makes accessing xml objects more like any other ruby object
|
4
|
+
# thanks to the magic of method missing
|
5
|
+
class XmlMagic
|
6
|
+
require 'rexml/document'
|
7
|
+
|
8
|
+
def initialize(xml, namespace="")
|
9
|
+
if xml.class == REXML::Element or xml.class == Array
|
10
|
+
@element = xml
|
11
|
+
else
|
12
|
+
@xml = REXML::Document.new(xml)
|
13
|
+
@element = @xml.root
|
14
|
+
end
|
15
|
+
@namespace = namespace
|
16
|
+
end
|
17
|
+
|
18
|
+
def class(selection=nil)
|
19
|
+
evaluate("class", selection)
|
20
|
+
end
|
21
|
+
|
22
|
+
def each
|
23
|
+
@element.each {|e| yield CommonThread::XML::XmlMagic.new(e, @namespace)}
|
24
|
+
end
|
25
|
+
|
26
|
+
def id(selection=nil)
|
27
|
+
evaluate("id", selection)
|
28
|
+
end
|
29
|
+
|
30
|
+
def method_missing(method, selection=nil)
|
31
|
+
evaluate(method.to_s, selection)
|
32
|
+
end
|
33
|
+
|
34
|
+
def namespace=(namespace)
|
35
|
+
if namespace and namespace.length > 0
|
36
|
+
@namespace = namespace + ":"
|
37
|
+
else
|
38
|
+
@namespace = ""
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
if @element.class == Array
|
44
|
+
@element.collect{|e| e.text}.join
|
45
|
+
else
|
46
|
+
@element.text
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def type(selection=nil)
|
51
|
+
evaluate("type", selection)
|
52
|
+
end
|
53
|
+
|
54
|
+
def [](index, count = nil)
|
55
|
+
if index.is_a?(Fixnum) or index.is_a?(Bignum) or index.is_a?(Integer) or index.is_a?(Range)
|
56
|
+
if @element.is_a?(Array)
|
57
|
+
if count
|
58
|
+
CommonThread::XML::XmlMagic.new(@element[index, count], @namespace)
|
59
|
+
else
|
60
|
+
CommonThread::XML::XmlMagic.new(@element[index], @namespace)
|
61
|
+
end
|
62
|
+
else
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
elsif index.is_a?(Symbol)
|
66
|
+
if @element.is_a?(Array)
|
67
|
+
if @element.empty?
|
68
|
+
nil
|
69
|
+
else
|
70
|
+
@element[0].attributes[index.to_s]
|
71
|
+
end
|
72
|
+
else
|
73
|
+
@element.attributes[index.to_s]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
def evaluate(name, selection)
|
80
|
+
|
81
|
+
if @element.is_a?(Array)
|
82
|
+
elements = @element[0].get_elements(@namespace + name)
|
83
|
+
else
|
84
|
+
elements = @element.get_elements(@namespace + name)
|
85
|
+
end
|
86
|
+
|
87
|
+
if elements.empty?
|
88
|
+
nil
|
89
|
+
else
|
90
|
+
if selection == :count
|
91
|
+
elements.length
|
92
|
+
else
|
93
|
+
CommonThread::XML::XmlMagic.new(elements, @namespace)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
data/lib/xml_magic.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/common_thread/xml/xml_magic'
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestXmlMagic < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_xml_magic
|
9
|
+
x = CommonThread::XML::XmlMagic.new('<project title="Sewer work"><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
|
+
assert_equal("Sewer work", x[:title])
|
11
|
+
assert_equal("New", x.type.to_s)
|
12
|
+
assert_equal("Anthony", x.contact[0][:name])
|
13
|
+
x.contact.each do |contact|
|
14
|
+
assert(contact[:name] != nil, "Attribute name should not be nil")
|
15
|
+
assert(contact[:phone] == nil, "Attribute phone should be nil")
|
16
|
+
end
|
17
|
+
assert_equal(3, x.contact(:count))
|
18
|
+
assert_equal("Test description.", x.description.to_s)
|
19
|
+
assert_equal("AnthonyBenJason", x.contact.to_s)
|
20
|
+
assert_equal(nil, x.note)
|
21
|
+
assert_equal(nil, x.content)
|
22
|
+
x.namespace = "media"
|
23
|
+
assert_equal("This is the content.", x.content.to_s)
|
24
|
+
x.namespace = ""
|
25
|
+
assert_equal(nil, x.content)
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xml-magic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anthony Crumley
|
8
|
+
- Ben Wyrosdick
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2008-03-05 00:00:00 -06:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: xml-magic makes accessing xml objects more like any other ruby object
|
18
|
+
email: anthony@commonthread.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- README
|
27
|
+
- CHANGELOG
|
28
|
+
- LICENSE
|
29
|
+
- demo.rb
|
30
|
+
- lib/xml_magic.rb
|
31
|
+
- lib/common_thread/xml/xml_magic.rb
|
32
|
+
- test/test_helper.rb
|
33
|
+
- test/test_xml_magic.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://rubyforge.org/projects/xml-magic/
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project: xml-magic
|
56
|
+
rubygems_version: 1.0.1
|
57
|
+
signing_key:
|
58
|
+
specification_version: 2
|
59
|
+
summary: xml-magic makes accessing xml objects more like any other ruby object
|
60
|
+
test_files: []
|
61
|
+
|