demolisher 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -2
- data/VERSION +1 -1
- data/demolisher.gemspec +1 -1
- data/lib/demolisher.rb +31 -9
- data/test/test.xml +1 -0
- metadata +3 -5
data/README.rdoc
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
=
|
1
|
+
= Demolisher
|
2
2
|
|
3
3
|
Works in a similar fashion to Builder but is instead used for extracting information from XML files rather than building them.
|
4
4
|
|
@@ -6,7 +6,7 @@ Works in a similar fashion to Builder but is instead used for extracting informa
|
|
6
6
|
|
7
7
|
Given the simple XML example file below
|
8
8
|
|
9
|
-
<addressbook>
|
9
|
+
<addressbook version="1.0">
|
10
10
|
<person>
|
11
11
|
<firstname>Enoch</firstname>
|
12
12
|
<lastname>Root</lastname>
|
@@ -31,6 +31,7 @@ we can parse it with
|
|
31
31
|
|
32
32
|
xml = Demolisher.demolish('addressbook.xml')
|
33
33
|
xml.addressbook do
|
34
|
+
puts "Version #{xml['version']}"
|
34
35
|
xml.person do
|
35
36
|
puts "#{xml.firstname} #{xml.lastname}: #{xml.contact.email}"
|
36
37
|
puts "this person is active" if xml.active?
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.3
|
data/demolisher.gemspec
CHANGED
data/lib/demolisher.rb
CHANGED
@@ -9,30 +9,47 @@ module Demolisher
|
|
9
9
|
yield node if block_given?
|
10
10
|
node
|
11
11
|
end
|
12
|
+
|
13
|
+
# Handles all the complexity of accessing the XML contents
|
12
14
|
class Node
|
13
|
-
# Creates a new Node object.
|
14
|
-
#
|
15
|
+
# Creates a new Node object.
|
16
|
+
#
|
17
|
+
# If the node is not the root node then the secondargument needs to be false.
|
15
18
|
def initialize(xml, is_root = true)
|
16
19
|
@nodes = [xml]
|
17
20
|
@nodes.unshift(nil) unless is_root
|
18
21
|
end
|
19
|
-
|
20
|
-
#
|
22
|
+
|
23
|
+
# Access an attribute of the current node.
|
24
|
+
#
|
25
|
+
# XML file:
|
21
26
|
# <addressbook>
|
22
27
|
# <person rel="friend">
|
23
28
|
# <firstname>Steve</firstname>
|
24
29
|
# </person>
|
25
30
|
# </addressbook>
|
26
|
-
#
|
31
|
+
#
|
32
|
+
# Example:
|
27
33
|
# xml.addressbook do
|
28
34
|
# xml.person do
|
29
35
|
# puts "#{xml.firstname} is a #{xml['rel']}" #=> "Steve is a friend"
|
30
36
|
# end
|
31
37
|
# end
|
32
|
-
#
|
38
|
+
#
|
33
39
|
def [](attr_name)
|
34
40
|
@nodes.last.attributes[attr_name]
|
35
41
|
end
|
42
|
+
|
43
|
+
# The workhorse, finds the node matching meth.
|
44
|
+
#
|
45
|
+
# Rough flow guide:
|
46
|
+
# If a block is given then yield to it each for each instance
|
47
|
+
# of the element found in the current node.
|
48
|
+
# If no block given then get the first element found
|
49
|
+
# If the node has only one text element check if the
|
50
|
+
# method called has a ? suffix then return true if node content
|
51
|
+
# looks like a boolean. Otherwise return text content
|
52
|
+
# Otherwise return a new Node instance
|
36
53
|
def method_missing(meth, *args, &block) # :nodoc:
|
37
54
|
xpath = @nodes.last.find(element_from_symbol(meth))
|
38
55
|
return nil if xpath.empty?
|
@@ -41,10 +58,10 @@ module Demolisher
|
|
41
58
|
xpath.each_with_index do |node, idx|
|
42
59
|
@nodes.push(node)
|
43
60
|
case block.arity
|
44
|
-
when 0
|
45
|
-
yield
|
46
61
|
when 1
|
47
62
|
yield idx
|
63
|
+
else
|
64
|
+
yield
|
48
65
|
end
|
49
66
|
@nodes.pop
|
50
67
|
end
|
@@ -64,13 +81,18 @@ module Demolisher
|
|
64
81
|
end
|
65
82
|
end
|
66
83
|
end
|
84
|
+
|
85
|
+
# Returns the current nodes contents.
|
67
86
|
def to_s # :nodoc:
|
68
87
|
@nodes.last.content.strip
|
69
88
|
end
|
89
|
+
|
90
|
+
# Transforms a symbol into a XML element path.
|
70
91
|
def element_from_symbol(sym) # :nodoc:
|
71
92
|
"#{is_root_node? ? '/' : nil}#{sym.to_s.gsub(/[^a-z0-9_]/i, '')}"
|
72
93
|
end
|
73
|
-
|
94
|
+
|
95
|
+
# Indicates if the current node is the root of the XML document.
|
74
96
|
def is_root_node?
|
75
97
|
@nodes.size == 1
|
76
98
|
end
|
data/test/test.xml
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: demolisher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Geoff Garside
|
@@ -43,10 +43,8 @@ files:
|
|
43
43
|
- test/demolisher_test.rb
|
44
44
|
- test/test.xml
|
45
45
|
- test/test_helper.rb
|
46
|
-
has_rdoc:
|
46
|
+
has_rdoc: false
|
47
47
|
homepage: http://github.com/geoffgarside/demolisher
|
48
|
-
licenses: []
|
49
|
-
|
50
48
|
post_install_message:
|
51
49
|
rdoc_options:
|
52
50
|
- --charset=UTF-8
|
@@ -67,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
65
|
requirements: []
|
68
66
|
|
69
67
|
rubyforge_project: demolisher
|
70
|
-
rubygems_version: 1.3.
|
68
|
+
rubygems_version: 1.3.1
|
71
69
|
signing_key:
|
72
70
|
specification_version: 3
|
73
71
|
summary: Gem for extracting information from XML files, think Builder but backwards
|