roxml 3.1.5 → 3.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/{spec/spec.opts → .rspec} +0 -0
  2. data/History.txt +12 -1
  3. data/README.rdoc +19 -2
  4. data/Rakefile +11 -13
  5. data/VERSION +1 -1
  6. data/examples/amazon.rb +1 -1
  7. data/examples/current_weather.rb +1 -1
  8. data/examples/dashed_elements.rb +1 -1
  9. data/examples/posts.rb +1 -1
  10. data/examples/rails.rb +1 -1
  11. data/examples/twitter.rb +1 -1
  12. data/lib/roxml.rb +8 -1
  13. data/lib/roxml/definition.rb +6 -4
  14. data/lib/roxml/xml.rb +3 -3
  15. data/lib/roxml/xml/parsers/libxml.rb +46 -52
  16. data/lib/roxml/xml/parsers/nokogiri.rb +46 -41
  17. data/lib/roxml/xml/references.rb +25 -18
  18. data/roxml.gemspec +10 -8
  19. data/spec/definition_spec.rb +8 -7
  20. data/spec/examples/active_record_spec.rb +3 -2
  21. data/spec/examples/amazon_spec.rb +2 -2
  22. data/spec/examples/current_weather_spec.rb +2 -2
  23. data/spec/examples/dashed_elements_spec.rb +2 -2
  24. data/spec/examples/library_spec.rb +5 -5
  25. data/spec/examples/post_spec.rb +2 -2
  26. data/spec/examples/twitter_spec.rb +2 -2
  27. data/spec/roxml_spec.rb +2 -2
  28. data/spec/shared_specs.rb +2 -2
  29. data/spec/spec_helper.rb +3 -4
  30. data/spec/xml/array_spec.rb +36 -0
  31. data/spec/xml/attributes_spec.rb +5 -5
  32. data/spec/xml/encoding_spec.rb +38 -7
  33. data/spec/xml/namespace_spec.rb +4 -4
  34. data/spec/xml/namespaces_spec.rb +1 -1
  35. data/spec/xml/object_spec.rb +5 -5
  36. data/spec/xml/parser_spec.rb +8 -13
  37. data/spec/xml/text_spec.rb +5 -5
  38. data/test/mocks/dictionaries.rb +1 -1
  39. data/test/mocks/mocks.rb +1 -1
  40. data/test/support/fixtures.rb +1 -1
  41. data/test/test_helper.rb +5 -5
  42. data/test/unit/definition_test.rb +5 -5
  43. data/test/unit/deprecations_test.rb +1 -1
  44. data/test/unit/to_xml_test.rb +4 -4
  45. data/test/unit/xml_attribute_test.rb +1 -1
  46. data/test/unit/xml_block_test.rb +1 -1
  47. data/test/unit/xml_bool_test.rb +1 -1
  48. data/test/unit/xml_convention_test.rb +1 -1
  49. data/test/unit/xml_hash_test.rb +1 -1
  50. data/test/unit/xml_initialize_test.rb +1 -1
  51. data/test/unit/xml_name_test.rb +2 -2
  52. data/test/unit/xml_namespace_test.rb +1 -1
  53. data/test/unit/xml_object_test.rb +3 -4
  54. data/test/unit/xml_required_test.rb +1 -1
  55. data/test/unit/xml_text_test.rb +1 -1
  56. metadata +63 -27
File without changes
data/History.txt CHANGED
@@ -1,3 +1,14 @@
1
+ == 3.1.6 (September 9, 2010)
2
+
3
+ * bug fix
4
+
5
+ * load active_support in a way that is compatible with 2.x and 3.x
6
+ (fixes gh issue #27)
7
+ * change the way we monkey patch Nokogiri to leave the original
8
+ behaviour of the search() method unchanged (fixes gh issue #16)
9
+ * Unofficial release by James Healy, empact has stepped back from maintaining
10
+ roxml
11
+
1
12
  == 3.1.5 (December 18, 2009)
2
13
 
3
14
  * bug fix
@@ -340,4 +351,4 @@
340
351
 
341
352
  == 1.1 (September 24, 2006)
342
353
 
343
- * Initial design & development [Zak Mandhro & Anders Engstrom]
354
+ * Initial design & development [Zak Mandhro & Anders Engstrom]
data/README.rdoc CHANGED
@@ -2,7 +2,7 @@ ROXML Ruby Object to XML mapping library.
2
2
 
3
3
  For more information visit:
4
4
 
5
- http://roxml.rubyforge.org/rdoc/
5
+ http://rdoc.info/projects/Empact/roxml
6
6
  http://empact.github.com/roxml/
7
7
  http://rubyforge.org/projects/roxml/
8
8
 
@@ -10,6 +10,15 @@ Please submit bugs here:
10
10
 
11
11
  http://github.com/Empact/roxml/issues
12
12
 
13
+
14
+ =ROXML is of, by, and for all of us
15
+
16
+ I've taken a lead role on ROXML these past months, but unfortunately I, Ben Woosley (Empact), am unable to continue to maintain ROXML at this time, as it no longer aligns with my other interests & efforts. While I would very much like to fix the bugs listed on the github issues page, I'm simply not the person to do it, given my other concerns.
17
+
18
+ But don't be mistaken, the library covers most cases very well, and is on the better-tested side of the spectrum (some 353 examples, all green as of this writing).
19
+
20
+ If you run into a problem with ROXML, *please* take advantage of those tests, and github, by rolling up your sleeves and making a fix! Your fellow programmers, myself included, will appreciate you for it.
21
+
13
22
  =Quick Start Guide
14
23
 
15
24
  This is a short usage example. See ROXML::ClassMethods::Declarations and packaged test cases for more information.
@@ -50,7 +59,15 @@ To create a library and put a number of books in it we could run the following c
50
59
 
51
60
  To save this information to an XML file:
52
61
 
53
- doc = ROXML::XML::Document.new
62
+ doc = Nokogiri::XML::Document.new
63
+ doc.root = lib.to_xml
64
+ open("library.xml", 'w') do |file|
65
+ file << doc.serialize
66
+ end
67
+
68
+ or
69
+
70
+ doc = LibXML::XML::Document.new
54
71
  doc.root = lib.to_xml
55
72
  doc.save("library.xml")
56
73
 
data/Rakefile CHANGED
@@ -25,7 +25,7 @@ EOF
25
25
  gem.add_dependency 'activesupport', '>= 2.3.0'
26
26
  gem.add_dependency 'nokogiri', '>= 1.3.3'
27
27
 
28
- gem.add_development_dependency "rspec"
28
+ gem.add_development_dependency "rspec", '>= 2.0.0'
29
29
  gem.add_development_dependency "sqlite3-ruby", '>= 1.2.4'
30
30
  gem.add_development_dependency "activerecord", '>= 2.2.2'
31
31
  end
@@ -56,30 +56,28 @@ Rake::RDocTask.new do |rdoc|
56
56
  rdoc.rdoc_files.include('lib/**/*.rb')
57
57
  end
58
58
 
59
- require 'spec/rake/spectask'
59
+ require 'rspec/core/rake_task'
60
60
  desc "Run specs"
61
- Spec::Rake::SpecTask.new(:spec) do |spec|
62
- spec.libs << 'lib' << 'spec' << 'examples'
63
- spec.spec_opts = ['--options', "spec/spec.opts"]
64
- spec.spec_files = FileList['spec/**/*_spec.rb']
61
+ RSpec::Core::RakeTask.new(:spec) do |spec|
62
+ spec.ruby_opts = '-Ilib -Ispec -Iexamples'
63
+ # spec.spec_files = FileList['spec/**/*_spec.rb']
65
64
  end
66
65
 
67
66
  namespace :spec do
68
67
  [:libxml, :nokogiri].each do |parser|
69
68
  desc "Spec ROXML under the #{parser} parser"
70
- Spec::Rake::SpecTask.new(parser) do |spec|
71
- spec.libs << 'lib' << 'spec' << 'examples'
72
- spec.spec_opts = ['--options=spec/spec.opts']
73
- spec.spec_files = ["spec/support/#{parser}.rb"] + FileList['spec/**/*_spec.rb']
69
+ RSpec::Core::RakeTask.new(parser) do |spec|
70
+ spec.ruby_opts = '-Ilib -Ispec -Iexamples'
71
+ # spec.spec_files = ["spec/support/#{parser}.rb"] + FileList['spec/**/*_spec.rb']
74
72
  end
75
73
  end
76
74
  end
77
75
 
78
76
  desc "Run specs with rcov"
79
- Spec::Rake::SpecTask.new(:rcov) do |spec|
80
- spec.libs << 'lib' << 'spec'
81
- spec.pattern = 'spec/**/*_spec.rb'
77
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
82
78
  spec.rcov = true
79
+ spec.ruby_opts = '-Ilib -Ispec -Iexamples'
80
+ # spec.spec_files = FileList['spec/**/*_spec.rb']
83
81
  end
84
82
 
85
83
  require 'rake/testtask'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.5
1
+ 3.1.6
data/examples/amazon.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- require 'spec/spec_helper'
2
+ require_relative './../spec/spec_helper'
3
3
 
4
4
  # The document `pita.xml` contains both a default namespace and the 'georss'
5
5
  # namespace (for the 'point' xml_reader).
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- require File.expand_path(File.dirname(__FILE__) + '/../spec/spec_helper')
2
+ require_relative './../spec/spec_helper'
3
3
 
4
4
  class Base
5
5
  include ROXML
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- require File.expand_path(File.dirname(__FILE__) + '/../spec/spec_helper')
2
+ require_relative './../spec/spec_helper'
3
3
 
4
4
  module GitHub
5
5
  class Commit
data/examples/posts.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- require File.expand_path(File.dirname(__FILE__) + '/../spec/spec_helper')
2
+ require_relative './../spec/spec_helper'
3
3
 
4
4
  class Post
5
5
  include ROXML
data/examples/rails.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- require 'spec/spec_helper'
2
+ require_relative './../spec/spec_helper'
3
3
  require 'sqlite3'
4
4
  require 'active_record'
5
5
 
data/examples/twitter.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- require File.expand_path(File.dirname(__FILE__) + '/../spec/spec_helper')
2
+ require_relative './../spec/spec_helper'
3
3
  require 'time'
4
4
 
5
5
  class User
data/lib/roxml.rb CHANGED
@@ -1,5 +1,12 @@
1
1
  require 'uri'
2
+
2
3
  require 'active_support'
4
+ require 'active_support/inflector/inflections'
5
+ require 'active_support/core_ext/object/duplicable'
6
+ require 'active_support/core_ext/module/delegation'
7
+ require 'active_support/core_ext/array/extract_options'
8
+ require 'active_support/core_ext/hash'
9
+ require 'active_support/core_ext/string/starts_ends_with'
3
10
 
4
11
  require 'roxml/definition'
5
12
  require 'roxml/xml'
@@ -23,7 +30,7 @@ module ROXML # :nodoc:
23
30
  def to_xml(params = {})
24
31
  params.reverse_merge!(:name => self.class.tag_name, :namespace => self.class.roxml_namespace)
25
32
  params[:namespace] = nil if ['*', 'xmlns'].include?(params[:namespace])
26
- XML::Node.create([params[:namespace], params[:name]].compact.join(':')).tap do |root|
33
+ XML.new_node([params[:namespace], params[:name]].compact.join(':')).tap do |root|
27
34
  refs = (self.roxml_references.present? \
28
35
  ? self.roxml_references \
29
36
  : self.class.roxml_attrs.map {|attr| attr.to_ref(self) })
@@ -55,16 +55,17 @@ module ROXML
55
55
  elsif opts[:from] == :attr
56
56
  @sought_type = :attr
57
57
  opts[:from] = nil
58
- elsif opts[:from] == :name
58
+ elsif opts[:from] == :namespace
59
59
  opts[:from] = '*'
60
+ @sought_type = :namespace
60
61
  elsif opts[:from].to_s.starts_with?('@')
61
62
  @sought_type = :attr
62
63
  opts[:from].sub!('@', '')
63
64
  end
64
65
 
65
- @attr_name = accessor.to_s.chomp('?')
66
- @name = (opts[:from] || @attr_name).to_s
66
+ @name = @attr_name = accessor.to_s.chomp('?')
67
67
  @name = @name.singularize if hash? || array?
68
+ @name = (opts[:from] || @name).to_s
68
69
  if hash? && (hash.key.name? || hash.value.name?)
69
70
  @name = '*'
70
71
  end
@@ -112,6 +113,7 @@ module ROXML
112
113
  case sought_type
113
114
  when :attr then XMLAttributeRef
114
115
  when :text then XMLTextRef
116
+ when :namespace then XMLNameSpaceRef
115
117
  when HashDefinition then XMLHashRef
116
118
  when Symbol then raise ArgumentError, "Invalid type argument #{sought_type}"
117
119
  else XMLObjectRef
@@ -212,7 +214,7 @@ module ROXML
212
214
  as = (block ? :bool_combined : :bool_standalone)
213
215
  end
214
216
  as = self.class.block_shorthands.fetch(as) do
215
- unless as.respond_to?(:from_xml) || (as.respond_to?(:first) && as.first.respond_to?(:from_xml)) || (as.is_a?(Hash) && !(as.keys & [:key, :value]).empty?)
217
+ unless (as == :text) || as.respond_to?(:from_xml) || (as.respond_to?(:first) && as.first.respond_to?(:from_xml)) || (as.is_a?(Hash) && !(as.keys & [:key, :value]).empty?)
216
218
  raise ArgumentError, "Invalid :as argument #{as}" unless as.nil?
217
219
  end
218
220
  nil
data/lib/roxml/xml.rb CHANGED
@@ -23,13 +23,13 @@ module ROXML
23
23
  when XML::Document
24
24
  data.root
25
25
  when File, IO
26
- Parser.parse_io(data).root
26
+ XML.parse_io(data).root
27
27
  else
28
28
  if (defined?(URI) && data.is_a?(URI::Generic)) ||
29
29
  (defined?(Pathname) && data.is_a?(Pathname))
30
- Parser.parse_file(data.to_s).root
30
+ XML.parse_file(data.to_s).root
31
31
  else
32
- Parser.parse(data).root
32
+ XML.parse_string(data).root
33
33
  end
34
34
  end
35
35
  end
@@ -1,15 +1,56 @@
1
1
  require 'libxml'
2
- require 'cgi'
3
2
 
4
3
  module ROXML
5
4
  module XML # :nodoc:all
5
+
6
+ class << self
7
+ def set_attribute(node, name, value)
8
+ node.attributes[name] = value
9
+ end
10
+
11
+ def set_content(node, content)
12
+ node.content = content.gsub('&', '&amp;')
13
+ end
14
+
15
+ def new_node(name)
16
+ LibXML::XML::Node.new(name)
17
+ end
18
+
19
+ def add_node(parent, name)
20
+ add_child(parent, new_node(name))
21
+ end
22
+
23
+ def add_cdata(parent, content)
24
+ add_child(parent, LibXML::XML::Node.new_cdata(content))
25
+ end
26
+
27
+ def add_child(parent, child)
28
+ parent << child
29
+ child
30
+ end
31
+
32
+ def parse_string(str_data)
33
+ LibXML::XML::Parser.string(str_data).parse
34
+ end
35
+
36
+ def parse_file(path)
37
+ LibXML::XML::Parser.file(path).parse
38
+ end
39
+
40
+ def parse_io(stream)
41
+ LibXML::XML::Parser.io(stream).parse
42
+ end
43
+
44
+ def save_doc(doc, path)
45
+ doc.save(path)
46
+ end
47
+ end
48
+
6
49
  Document = LibXML::XML::Document
7
50
  Node = LibXML::XML::Node
8
- Parser = LibXML::XML::Parser
9
- Error = LibXML::XML::Error
10
51
 
11
52
  module NamespacedSearch
12
- def search(xpath, roxml_namespaces = {})
53
+ def roxml_search(xpath, roxml_namespaces = {})
13
54
  if namespaces.default
14
55
  roxml_namespaces = {:xmlns => namespaces.default.href}.merge(roxml_namespaces)
15
56
  end
@@ -19,17 +60,6 @@ module ROXML
19
60
  find(xpath)
20
61
  end
21
62
  end
22
-
23
- private
24
- def namespaced(xpath)
25
- xpath.split('/').map do |component|
26
- if component =~ /\w+/ && !component.include?(':') && !component.starts_with?('@')
27
- "xmlns:#{component}"
28
- else
29
- component
30
- end
31
- end.join('/')
32
- end
33
63
  end
34
64
 
35
65
  class Document
@@ -47,45 +77,9 @@ module ROXML
47
77
  class Node
48
78
  include NamespacedSearch
49
79
 
50
- class << self
51
- def new_with_entity_escaping(name, content = nil, namespace = nil)
52
- new_without_entity_escaping(name, content && CGI.escapeHTML(content), namespace)
53
- end
54
- alias_method_chain :new, :entity_escaping
55
-
56
- alias :create :new
57
- end
58
-
59
80
  def default_namespace
60
81
  doc.default_namespace
61
82
  end
62
-
63
- def add_child(child)
64
- # libxml 1.1.3 changed child_add from returning child to returning self
65
- self << child
66
- child
67
- end
68
-
69
- alias_method :set_libxml_content, :content=
70
- def content=(string)
71
- set_libxml_content(string.gsub('&', '&amp;'))
72
- end
73
- end
74
-
75
- class Parser
76
- class << self
77
- def parse(str_data)
78
- string(str_data).parse
79
- end
80
-
81
- def parse_file(path)
82
- file(path).parse
83
- end
84
-
85
- def parse_io(stream)
86
- io(stream).parse
87
- end
88
- end
89
83
  end
90
84
  end
91
- end
85
+ end
@@ -2,35 +2,58 @@ require 'nokogiri'
2
2
 
3
3
  module ROXML
4
4
  module XML # :nodoc:all
5
- Document = Nokogiri::XML::Document
6
- Element = Nokogiri::XML::Element
7
- Node = Nokogiri::XML::Node
8
5
 
9
- module Error; end
6
+ class << self
7
+ def set_attribute(node, name, value)
8
+ node[name] = value
9
+ end
10
10
 
11
- class Parser
12
- class << self
13
- def parse(string)
14
- Nokogiri::XML(string)
15
- end
11
+ def set_content(node, content)
12
+ node.content = content
13
+ end
16
14
 
17
- def parse_file(path) #:nodoc:
18
- path = path.sub('file:', '') if path.starts_with?('file:')
19
- parse(open(path))
20
- end
15
+ def new_node(name)
16
+ Nokogiri::XML::Node.new(name, Document.new)
17
+ end
21
18
 
22
- def parse_io(stream) #:nodoc:
23
- parse(stream)
24
- end
19
+ def add_node(parent, name)
20
+ add_child(parent, Nokogiri::XML::Node.new(name, parent.document))
25
21
  end
26
- end
27
22
 
28
- class Document
29
- def save(path)
23
+ def add_cdata(parent, content)
24
+ parent.add_child(Nokogiri::XML::CDATA.new(parent.document, content))
25
+ end
26
+
27
+ def add_child(parent, child)
28
+ parent.add_child(child)
29
+ end
30
+
31
+ def parse_string(string)
32
+ Nokogiri::XML(string)
33
+ end
34
+
35
+ def parse_file(path)
36
+ path = path.sub('file:', '') if path.starts_with?('file:')
37
+ parse_io(open(path))
38
+ end
39
+
40
+ def parse_io(stream)
41
+ Nokogiri::XML(stream)
42
+ end
43
+
44
+ def save_doc(doc, path)
30
45
  open(path, 'w') do |file|
31
- file << serialize
46
+ file << doc.serialize
32
47
  end
33
48
  end
49
+ end
50
+
51
+ Document = Nokogiri::XML::Document
52
+ Element = Nokogiri::XML::Element
53
+ Node = Nokogiri::XML::Node
54
+
55
+ class Document
56
+ alias :roxml_search :search
34
57
 
35
58
  def default_namespace
36
59
  'xmlns' if root.namespaces['xmlns']
@@ -38,13 +61,9 @@ module ROXML
38
61
  end
39
62
 
40
63
  module NodeExtensions
41
- def search(xpath, roxml_namespaces = {})
64
+ def roxml_search(xpath, roxml_namespaces = {})
42
65
  xpath = "./#{xpath}"
43
- (roxml_namespaces.present? ? super(xpath, roxml_namespaces) : super(xpath)).map {|i| i }
44
- end
45
-
46
- def attributes
47
- self
66
+ (roxml_namespaces.present? ? search(xpath, roxml_namespaces) : search(xpath))
48
67
  end
49
68
 
50
69
  def default_namespace
@@ -54,24 +73,10 @@ module ROXML
54
73
 
55
74
  class Element
56
75
  include NodeExtensions
57
-
58
- def empty?
59
- children.empty?
60
- end
61
76
  end
62
77
 
63
78
  class Node
64
- class << self
65
- def new_cdata(content)
66
- Nokogiri::XML::CDATA.new(Document.new, content)
67
- end
68
-
69
- def create(name)
70
- new(name, Document.new)
71
- end
72
- end
73
79
  include NodeExtensions
74
- alias :remove! :remove
75
80
  end
76
81
  end
77
- end
82
+ end