demolisher 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -11,7 +11,8 @@ begin
11
11
  gem.authors = ["Geoff Garside"]
12
12
  gem.rubyforge_project = 'demolisher'
13
13
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
- gem.add_dependency('libxml-ruby', '>= 1.1.3')
14
+ gem.add_dependency('libxml-ruby', '>=1.1.3')
15
+ gem.add_dependency('nokogiri', '>=1.4.2')
15
16
  end
16
17
 
17
18
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.2
1
+ 0.6.0
@@ -1,56 +1,56 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{demolisher}
8
- s.version = "0.5.2"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Geoff Garside"]
12
- s.date = %q{2010-06-07}
12
+ s.date = %q{2011-03-23}
13
13
  s.email = %q{geoff@geoffgarside.co.uk}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
16
- "README.rdoc"
16
+ "README.rdoc"
17
17
  ]
18
18
  s.files = [
19
19
  ".document",
20
- ".gitignore",
21
- "LICENSE",
22
- "README.rdoc",
23
- "Rakefile",
24
- "VERSION",
25
- "demolisher.gemspec",
26
- "lib/demolisher.rb",
27
- "test/demolisher_test.rb",
28
- "test/ns.xml",
29
- "test/test.xml",
30
- "test/test_helper.rb"
20
+ "LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "demolisher.gemspec",
25
+ "lib/demolisher.rb",
26
+ "test/demolisher_test.rb",
27
+ "test/ns.xml",
28
+ "test/test.xml",
29
+ "test/test_helper.rb"
31
30
  ]
32
31
  s.homepage = %q{http://github.com/geoffgarside/demolisher}
33
- s.rdoc_options = ["--charset=UTF-8"]
34
32
  s.require_paths = ["lib"]
35
33
  s.rubyforge_project = %q{demolisher}
36
- s.rubygems_version = %q{1.3.7}
34
+ s.rubygems_version = %q{1.6.2}
37
35
  s.summary = %q{Gem for extracting information from XML files, think Builder but backwards}
38
36
  s.test_files = [
39
37
  "test/demolisher_test.rb",
40
- "test/test_helper.rb"
38
+ "test/test_helper.rb"
41
39
  ]
42
40
 
43
41
  if s.respond_to? :specification_version then
44
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
42
  s.specification_version = 3
46
43
 
47
44
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
45
  s.add_runtime_dependency(%q<libxml-ruby>, [">= 1.1.3"])
46
+ s.add_runtime_dependency(%q<nokogiri>, [">= 1.4.2"])
49
47
  else
50
48
  s.add_dependency(%q<libxml-ruby>, [">= 1.1.3"])
49
+ s.add_dependency(%q<nokogiri>, [">= 1.4.2"])
51
50
  end
52
51
  else
53
52
  s.add_dependency(%q<libxml-ruby>, [">= 1.1.3"])
53
+ s.add_dependency(%q<nokogiri>, [">= 1.4.2"])
54
54
  end
55
55
  end
56
56
 
@@ -1,24 +1,29 @@
1
- require 'xml'
1
+ require 'libxml'
2
+ require 'nokogiri'
3
+ require 'nokogiri/xml'
2
4
 
3
5
  module Demolisher
4
6
  # Demolish an XML file or XML::Parser object.
5
- def self.demolish(file_or_xml_parser, namespace_list = nil)
6
- file_or_xml_parser = new_parser(file_or_xml_parser) if file_or_xml_parser.kind_of?(String)
7
- file_or_xml_parser = file_or_xml_parser.parse if file_or_xml_parser.respond_to?(:parse)
8
- node = Node.new(file_or_xml_parser, namespace_list, true)
7
+ # @param [String,IO,#parse] thing
8
+ def self.demolish(thing, namespaces = nil)
9
+ thing = _parse(thing) unless thing.kind_of?(Nokogiri::XML::Document)
10
+ node = Node.new(thing, namespaces, true)
9
11
 
10
12
  yield node if block_given?
11
13
  node
12
14
  end
13
15
 
14
- # ---
15
- # Creates an XML::Parser from the string if its a string or the file if its a file path
16
- # +++
17
- def self.new_parser(string_or_filepath)
18
- if File.exist?(string_or_filepath)
19
- XML::Parser.file(string_or_filepath)
16
+ def self._exchange_libxml(thing)
17
+ thing = thing.parse if thing.respond_to?(:parse)
18
+ thing.to_s
19
+ end
20
+ def self._parse(thing)
21
+ if thing.kind_of?(LibXML::XML::Parser) || thing.kind_of?(LibXML::XML::Document)
22
+ Nokogiri::XML::Document.parse(_exchange_libxml(thing))
20
23
  else
21
- XML::Parser.string(string_or_filepath)
24
+ # could be string data or a file name
25
+ thing = File.open(thing) if File.exists?(thing)
26
+ Nokogiri::XML::Document.parse(thing)
22
27
  end
23
28
  end
24
29
 
@@ -30,7 +35,8 @@ module Demolisher
30
35
  def initialize(xml, namespaces = nil, is_root = true)
31
36
  @nodes = [xml]
32
37
  @nodes.unshift(nil) unless is_root
33
- @namespaces = namespaces
38
+ @namespaces = namespaces || {}
39
+ @namespaces.merge!(xml.collect_namespaces) if xml.respond_to?(:collect_namespaces)
34
40
  end
35
41
 
36
42
  # Access an attribute of the current node.
@@ -50,7 +56,7 @@ module Demolisher
50
56
  # end
51
57
  #
52
58
  def [](attr_name)
53
- _current_node.attributes[attr_name]
59
+ _current_node.attributes[attr_name].to_s
54
60
  end
55
61
 
56
62
  # The workhorse, finds the node matching meth.
@@ -73,16 +79,19 @@ module Demolisher
73
79
  case block.arity
74
80
  when 1
75
81
  yield idx
82
+ when 2
83
+ yield self.class.new(node, @namespaces, false), idx
76
84
  else
77
85
  yield
78
86
  end
79
87
  @nodes.pop
80
88
  end
89
+ self
81
90
  else
82
91
  node = xpath.first
83
92
 
84
- if node.find('text()').length == 1
85
- content = node.find('text()').first.content
93
+ if node.xpath('text()').length == 1
94
+ content = node.xpath('text()').first.content
86
95
  case meth.to_s
87
96
  when /\?$/
88
97
  !! Regexp.new(/(t(rue)?|y(es)?|1)/i).match(content)
@@ -99,13 +108,16 @@ module Demolisher
99
108
  def to_s # :nodoc:
100
109
  _current_node.content.strip
101
110
  end
111
+ def inspect # :nodoc:
112
+ "#<#{self.class} node=#{_current_node.name}>"
113
+ end
102
114
 
103
115
  def _current_node
104
- _is_root_node? ? @nodes.last.root : @nodes.last
116
+ @nodes.last
105
117
  end
106
118
 
107
119
  def _xpath_for_element(el_or_ns, el_for_ns = nil)
108
- _current_node.find(_element_from_symbol(el_or_ns, el_for_ns), @namespaces)
120
+ _current_node.xpath(_element_from_symbol(el_or_ns, el_for_ns), @namespaces)
109
121
  end
110
122
 
111
123
  # Transforms a symbol into a XML element path.
@@ -47,6 +47,57 @@ class DemolisherTest < Test::Unit::TestCase
47
47
  assert_equal 'randy@example.com', @person[:email]
48
48
  end
49
49
  end
50
+ context "shortened form" do
51
+ setup do
52
+ @people = Array.new
53
+ Demolisher.demolish(File.dirname(__FILE__) +'/test.xml') do |xml|
54
+ xml.addressbook.person do |person, idx|
55
+ @people << {:firstname => person.firstname, :lastname => person.lastname,
56
+ :active => person.active?, :email => person.contact.email, :index => idx}
57
+ end
58
+ end
59
+ end
60
+ context "first extracted person" do
61
+ setup do
62
+ @person = @people[0]
63
+ end
64
+ should "have extracted firstname" do
65
+ assert_equal 'Enoch', @person[:firstname]
66
+ end
67
+ should "have extracted lastname" do
68
+ assert_equal 'Root', @person[:lastname]
69
+ end
70
+ should "have extracted true active status" do
71
+ assert @person[:active]
72
+ end
73
+ should "have extracted email" do
74
+ assert_equal 'enoch@example.com', @person[:email]
75
+ end
76
+ should "have index 0" do
77
+ assert_equal 0, @person[:index]
78
+ end
79
+ end
80
+ context "second extracted person" do
81
+ setup do
82
+ @person = @people[1]
83
+ end
84
+ should "have extracted firstname" do
85
+ assert_equal 'Randy', @person[:firstname]
86
+ end
87
+ should "have extracted lastname" do
88
+ assert_equal 'Waterhouse', @person[:lastname]
89
+ end
90
+ should "have extracted false active status" do
91
+ assert !@person[:active]
92
+ end
93
+ should "have extracted email" do
94
+ assert_equal 'randy@example.com', @person[:email]
95
+ end
96
+ should "have index 1" do
97
+ assert_equal 1, @person[:index]
98
+ end
99
+ end
100
+ end
50
101
  end
51
102
  context "Demolished XML String" do
52
103
  setup do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: demolisher
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
5
- prerelease: false
4
+ hash: 7
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 2
10
- version: 0.5.2
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Geoff Garside
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-07 00:00:00 +01:00
18
+ date: 2011-03-23 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -34,6 +34,22 @@ dependencies:
34
34
  version: 1.1.3
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: nokogiri
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 1
48
+ - 4
49
+ - 2
50
+ version: 1.4.2
51
+ type: :runtime
52
+ version_requirements: *id002
37
53
  description:
38
54
  email: geoff@geoffgarside.co.uk
39
55
  executables: []
@@ -45,7 +61,6 @@ extra_rdoc_files:
45
61
  - README.rdoc
46
62
  files:
47
63
  - .document
48
- - .gitignore
49
64
  - LICENSE
50
65
  - README.rdoc
51
66
  - Rakefile
@@ -61,8 +76,8 @@ homepage: http://github.com/geoffgarside/demolisher
61
76
  licenses: []
62
77
 
63
78
  post_install_message:
64
- rdoc_options:
65
- - --charset=UTF-8
79
+ rdoc_options: []
80
+
66
81
  require_paths:
67
82
  - lib
68
83
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -86,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
101
  requirements: []
87
102
 
88
103
  rubyforge_project: demolisher
89
- rubygems_version: 1.3.7
104
+ rubygems_version: 1.6.2
90
105
  signing_key:
91
106
  specification_version: 3
92
107
  summary: Gem for extracting information from XML files, think Builder but backwards
data/.gitignore DELETED
@@ -1,5 +0,0 @@
1
- *.sw?
2
- .DS_Store
3
- coverage
4
- rdoc
5
- pkg