xml_resource 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,2 +1,64 @@
1
1
  xml_resource
2
- ============
2
+ ============
3
+
4
+ Turn XML into Ruby objects
5
+
6
+ Install
7
+ -------
8
+
9
+ In your Gemfile:
10
+
11
+ ```ruby
12
+ gem 'xml_resource', '~> 2.0.0'
13
+ ```
14
+
15
+ Use
16
+ ---
17
+
18
+ ```ruby
19
+ xml = %q{
20
+ <library>
21
+ <books>
22
+ <book>
23
+ <name>The Tempest</name>
24
+ <author>William Shakespeare</author>
25
+ <firstpublished>Nov 1, 1611</firstpublished>
26
+ </book>
27
+ <book>
28
+ <name>Moby-Dick</name>
29
+ <author>Herman Melville</author>
30
+ <firstpublished>Oct 18, 1851</firstpublished>
31
+ </book>
32
+ </books>
33
+ <hours>
34
+ <from>08:00 AM</from>
35
+ <till>06:00 PM</till>
36
+ </hours>
37
+ </library>
38
+ }
39
+
40
+ class Library < XmlResource::Base
41
+ has_collection :books
42
+
43
+ has_attribute :open_from, :xpath => 'hours/from'
44
+ has_attribute :open_till, :xpath => 'hours/till'
45
+ end
46
+
47
+ class Book < XmlResource::Base
48
+ has_attribute :name
49
+ has_attribute :author
50
+ has_attribute :published, :type => :date, :xpath => 'firstpublished'
51
+ end
52
+
53
+ library = Library.from_xml(xml)
54
+ => #<Library:0x00000100dcf7e8 @attributes={"open_from"=>"08:00 AM", "open_till"=>"06:00 PM"}, @books=[#<Book:0x00000100dd1778 @attributes={"name"=>"The Tempest", "author"=>"William Shakespeare", "published"=>Tue, 01 Nov 1611}>, #<Book:0x00000100dcfbd0 @attributes={"name"=>"Moby-Dick", "author"=>"Herman Melville", "published"=>Sat, 18 Oct 1851}>]>
55
+
56
+ library.open_from
57
+ => "08:00 AM"
58
+
59
+ book = library.books.first
60
+ => #<Book:0x00000100dd1778 @attributes={"name"=>"The Tempest", "author"=>"William Shakespeare", "published"=>Tue, 01 Nov 1611}>
61
+
62
+ book.published
63
+ => Tue, 01 Nov 1611
64
+ ```
@@ -1,5 +1,7 @@
1
1
  module XmlResource
2
2
  class Base
3
+ TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set
4
+
3
5
  class_attribute :attributes, :collections, :objects, :root_path
4
6
  self.attributes = {}
5
7
  self.collections = {}
@@ -45,7 +47,7 @@ module XmlResource
45
47
  end
46
48
 
47
49
  def root
48
- self.root_path || "/#{self.basename.underscore}"
50
+ self.root_path || "./#{self.basename.underscore}"
49
51
  end
50
52
 
51
53
  def root=(root)
@@ -95,7 +97,7 @@ module XmlResource
95
97
  [:attribute, :object, :collection].each do |method_name|
96
98
  define_method "#{method_name}_xpath" do |name|
97
99
  options = send(method_name.to_s.pluralize)
98
- options[name] && options[name][:xpath] or "/#{name}"
100
+ options[name] && options[name][:xpath] or "./#{name}"
99
101
  end
100
102
  end
101
103
 
@@ -124,7 +126,7 @@ module XmlResource
124
126
  when :string then value
125
127
  when :integer then value.to_i
126
128
  when :float then value.to_f
127
- when :boolean then !!value
129
+ when :boolean then cast_to_boolean(value)
128
130
  when :decimal then BigDecimal.new(value)
129
131
  when :date then Date.parse(value)
130
132
  when :datetime then DateTime.parse(value)
@@ -133,10 +135,18 @@ module XmlResource
133
135
  end
134
136
  end
135
137
 
138
+ def cast_to_boolean(value)
139
+ if value.is_a?(String) && value.blank?
140
+ nil
141
+ else
142
+ TRUE_VALUES.include?(value)
143
+ end
144
+ end
145
+
136
146
  def parse(xml_or_string)
137
147
  case xml_or_string
138
- when Hpricot::Elem, Hpricot::Elements then xml_or_string
139
- when String then Hpricot.XML(xml_or_string).root
148
+ when Nokogiri::XML::Node, Nokogiri::XML::NodeSet then xml_or_string
149
+ when String then Nokogiri.XML(xml_or_string).root
140
150
  else
141
151
  raise XmlResource::ParseError, "cannot parse #{xml_or_string.inspect}"
142
152
  end
@@ -1,3 +1,3 @@
1
1
  module XmlResource
2
- VERSION = '1.0.0'
2
+ VERSION = '2.0.0'
3
3
  end
data/lib/xml_resource.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  require 'active_support/core_ext'
2
- require 'hpricot'
2
+ require 'nokogiri'
3
3
  require 'xml_resource/version'
4
4
  require 'xml_resource/base'
@@ -49,6 +49,10 @@ class XmlResourceTest < ActiveSupport::TestCase
49
49
  assert_raise(XmlResource::ParseError) { Order.collection_from_xml({}) }
50
50
  end
51
51
 
52
+ test 'Boolean attributes' do
53
+ assert_equal [true, false, true], orders.map { |o| o.finished }
54
+ end
55
+
52
56
  private
53
57
 
54
58
  def orders
data/test/data/orders.xml CHANGED
@@ -1,6 +1,7 @@
1
1
  <someroot>
2
2
  <order>
3
3
  <id>1</id>
4
+ <finished>true</finished>
4
5
  <info>
5
6
  <foo type="bar">A</foo>
6
7
  <date>2012-08-27</date>
@@ -25,6 +26,7 @@
25
26
 
26
27
  <order>
27
28
  <id>2</id>
29
+ <finished>false</finished>
28
30
  <info>
29
31
  <foo type="non">blubb</foo>
30
32
  <foo type="bar">B</foo>
@@ -53,6 +55,7 @@
53
55
 
54
56
  <order>
55
57
  <id>3</id>
58
+ <finished>true</finished>
56
59
  <info>
57
60
  <foo type="non">wah</foo>
58
61
  <foo type="bar">C</foo>
data/test/models/order.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  class Order < XmlResource::Base
2
2
  has_attribute :id, :type => :integer
3
- has_attribute :date, :type => :date, :xpath => '/info/date'
4
- has_attribute :foobar, :xpath => '/info/foo[@type="bar"]'
3
+ has_attribute :date, :type => :date, :xpath => 'info/date'
4
+ has_attribute :foobar, :xpath => 'info/foo[@type="bar"]'
5
+ has_attribute :finished, :type => :boolean
5
6
  has_attribute :shipping_cost, :type => :decimal
6
-
7
+
7
8
  has_object :customer, :class_name => "Contact"
8
9
  has_object :contact
9
10
 
10
- has_collection :items, :class_name => "Item", :xpath => '/items'
11
+ has_collection :items, :class_name => "Item", :xpath => 'items'
11
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,27 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-27 00:00:00.000000000 Z
12
+ date: 2013-11-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &2151918380 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
21
  version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2151918380
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
25
30
  - !ruby/object:Gem::Dependency
26
- name: hpricot
27
- requirement: &2151917700 !ruby/object:Gem::Requirement
31
+ name: nokogiri
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *2151917700
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: debugger
38
- requirement: &2151917100 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,8 +53,13 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *2151917100
47
- description: ''
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ! 'xml_resource is '
48
63
  email:
49
64
  - mtgrosser@gmx.net
50
65
  executables: []
@@ -63,7 +78,7 @@ files:
63
78
  - test/models/item.rb
64
79
  - test/models/order.rb
65
80
  - test/test_helper.rb
66
- homepage: http://rubygems.org/gems/xml_resource
81
+ homepage: http://github.com/mtgrosser/xml_resource
67
82
  licenses: []
68
83
  post_install_message:
69
84
  rdoc_options: []
@@ -83,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
98
  version: '0'
84
99
  requirements: []
85
100
  rubyforge_project:
86
- rubygems_version: 1.8.11
101
+ rubygems_version: 1.8.23
87
102
  signing_key:
88
103
  specification_version: 3
89
104
  summary: Turn XML into Ruby objects