geoffgarside-demolisher 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +48 -0
- data/Rakefile +57 -0
- data/VERSION +1 -0
- data/demolisher.gemspec +49 -0
- data/lib/demolisher.rb +63 -0
- data/test/demolisher_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +73 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Geoff Garside
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= demolisher
|
2
|
+
|
3
|
+
Works in a similar fashion to Builder but is instead used for extracting information from XML files rather than building them.
|
4
|
+
|
5
|
+
== Example
|
6
|
+
|
7
|
+
Given the simple XML example file below
|
8
|
+
|
9
|
+
<addressbook>
|
10
|
+
<person>
|
11
|
+
<firstname>Enoch</firstname>
|
12
|
+
<lastname>Root</lastname>
|
13
|
+
<contact>
|
14
|
+
<phone>01234 567 8900</phone>
|
15
|
+
<email>enoch@example.com</email>
|
16
|
+
</contact>
|
17
|
+
<active>YES</active>
|
18
|
+
</person>
|
19
|
+
<person>
|
20
|
+
<firstname>Randy</firstname>
|
21
|
+
<lastname>Waterhouse</lastname>
|
22
|
+
<contact>
|
23
|
+
<phone>01234 567 8901</phone>
|
24
|
+
<email>randy@example.com</email>
|
25
|
+
</contact>
|
26
|
+
<active>NO</active>
|
27
|
+
</person>
|
28
|
+
</addressbook>
|
29
|
+
|
30
|
+
we can parse it with
|
31
|
+
|
32
|
+
xml = Demolisher.demolish('addressbook.xml')
|
33
|
+
xml.addressbook do
|
34
|
+
xml.person do
|
35
|
+
puts "#{xml.firstname} #{xml.lastname}: #{xml.contact.email}"
|
36
|
+
puts "this person is active" if xml.active?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
and we should get the result of
|
41
|
+
|
42
|
+
Enoch Root: enoch@example.com
|
43
|
+
this person is active
|
44
|
+
Randy Waterhouse: randy@example.com
|
45
|
+
|
46
|
+
== Copyright
|
47
|
+
|
48
|
+
Copyright (c) 2009 Geoff Garside. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "demolisher"
|
8
|
+
gem.summary = %Q{Gem for extracting information from XML files, think Builder but backwards}
|
9
|
+
gem.email = "geoff@geoffgarside.co.uk"
|
10
|
+
gem.homepage = "http://github.com/geoffgarside/demolisher"
|
11
|
+
gem.authors = ["Geoff Garside"]
|
12
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
13
|
+
gem.add_dependency('libxml-ruby', '>= 1.1.3')
|
14
|
+
end
|
15
|
+
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/*_test.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
if File.exist?('VERSION.yml')
|
46
|
+
config = YAML.load(File.read('VERSION.yml'))
|
47
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
48
|
+
else
|
49
|
+
version = ""
|
50
|
+
end
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "demolisher #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
+
end
|
57
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/demolisher.gemspec
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{demolisher}
|
5
|
+
s.version = "0.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Geoff Garside"]
|
9
|
+
s.date = %q{2009-06-05}
|
10
|
+
s.email = %q{geoff@geoffgarside.co.uk}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION",
|
22
|
+
"demolisher.gemspec",
|
23
|
+
"lib/demolisher.rb",
|
24
|
+
"test/demolisher_test.rb",
|
25
|
+
"test/test_helper.rb"
|
26
|
+
]
|
27
|
+
s.homepage = %q{http://github.com/geoffgarside/demolisher}
|
28
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
s.rubygems_version = %q{1.3.3}
|
31
|
+
s.summary = %q{Gem for extracting information from XML files, think Builder but backwards}
|
32
|
+
s.test_files = [
|
33
|
+
"test/demolisher_test.rb",
|
34
|
+
"test/test_helper.rb"
|
35
|
+
]
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
42
|
+
s.add_runtime_dependency(%q<libxml-ruby>, [">= 1.1.3"])
|
43
|
+
else
|
44
|
+
s.add_dependency(%q<libxml-ruby>, [">= 1.1.3"])
|
45
|
+
end
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<libxml-ruby>, [">= 1.1.3"])
|
48
|
+
end
|
49
|
+
end
|
data/lib/demolisher.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'xml'
|
2
|
+
|
3
|
+
module Demolisher
|
4
|
+
# Demolish an XML file or XML::Parser object.
|
5
|
+
def self.demolish(file_or_xml_parser)
|
6
|
+
file_or_xml_parser = XML::Parser.file(file_or_xml_parser) if file_or_xml_parser.kind_of?(String)
|
7
|
+
Node.new(file_or_xml_parser.parse, true)
|
8
|
+
end
|
9
|
+
class Node
|
10
|
+
# Creates a new Node object. If the node is not the root node then
|
11
|
+
# the second argument needs to be false.
|
12
|
+
def initialize(xml, is_root = true)
|
13
|
+
@nodes = [xml]
|
14
|
+
@nodes.unshift(nil) unless is_root
|
15
|
+
end
|
16
|
+
# Access an attribute of the current node
|
17
|
+
# Example:
|
18
|
+
# <addressbook>
|
19
|
+
# <person rel="friend">
|
20
|
+
# <firstname>Steve</firstname>
|
21
|
+
# </person>
|
22
|
+
# </addressbook>
|
23
|
+
#
|
24
|
+
# xml.addressbook do
|
25
|
+
# xml.person do
|
26
|
+
# puts "#{xml.firstname} is a #{xml['rel']}" #=> "Steve is a friend"
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
def [](attr_name)
|
31
|
+
@nodes.last.attributes[attr_name]
|
32
|
+
end
|
33
|
+
def method_missing(meth, *args, &block) # :nodoc:
|
34
|
+
xpath = @nodes.last.find(element_from_symbol(meth))
|
35
|
+
return nil if xpath.empty?
|
36
|
+
|
37
|
+
if block_given?
|
38
|
+
xpath.each do |node|
|
39
|
+
@nodes.push(node)
|
40
|
+
yield
|
41
|
+
@nodes.pop
|
42
|
+
end
|
43
|
+
else
|
44
|
+
case meth.to_s
|
45
|
+
when /\?$/
|
46
|
+
!! Regexp.new(/(t(rue)?|y(es)?|1)/i).match(xpath.first.content.strip)
|
47
|
+
else
|
48
|
+
self.class.new(xpath.first, false)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
def to_s # :nodoc:
|
53
|
+
@nodes.last.content.strip
|
54
|
+
end
|
55
|
+
def element_from_symbol(sym) # :nodoc:
|
56
|
+
"#{is_root_node? ? '/' : nil}#{sym.to_s.gsub(/[^a-z0-9_]/i, '')}"
|
57
|
+
end
|
58
|
+
# Indicates if the current node is the root of the XML document
|
59
|
+
def is_root_node?
|
60
|
+
@nodes.size == 1
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geoffgarside-demolisher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Geoff Garside
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-05 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: libxml-ruby
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.1.3
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: geoff@geoffgarside.co.uk
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- demolisher.gemspec
|
42
|
+
- lib/demolisher.rb
|
43
|
+
- test/demolisher_test.rb
|
44
|
+
- test/test_helper.rb
|
45
|
+
has_rdoc: false
|
46
|
+
homepage: http://github.com/geoffgarside/demolisher
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --charset=UTF-8
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.2.0
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Gem for extracting information from XML files, think Builder but backwards
|
71
|
+
test_files:
|
72
|
+
- test/demolisher_test.rb
|
73
|
+
- test/test_helper.rb
|