xml_resource 2.0.5 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 39d686a14bc85492ab2c14febba48aa87f4f6886
4
- data.tar.gz: ac9700fdf9aaeebf0c33dcf7a9afb6af044942b0
3
+ metadata.gz: 49c8c1c5f8723060db353908111209349586cabd
4
+ data.tar.gz: fa85add787dd73c08c2119efb09e852c3186c1d7
5
5
  SHA512:
6
- metadata.gz: d58ea3703ec35453f365641d47537d6d2ceba5637453d7f54355d2985fc29a14d7609690ca833937fde57a96c1a7dc335d62d563efbd4166281fd1e1d207081a
7
- data.tar.gz: e154eeeaac55e95cb6ea58c51003fa01144b6e4780af092fa231acfea99cf9d2b4ce98f1620be79c641456169f22b1f8d3748c51aa636c5fa577dba681e67e59
6
+ metadata.gz: 654917f32e7acff9e9e36b59b7790beb3062d504568cb662211cb1528587303d1c92aba0cf502c9deb05fcf738721b633e8d0c9552b0c4dd1aa69c74ad133863
7
+ data.tar.gz: dc1ca45a9f0a90cf6a25d87d0e4f4965fe06eedfb93aac4e7dd4ac09038ca03cb95edbdb4e7d4851b507f286c3d6b85dd5c6e02f1a8e2c2ce749b31f9774b475
data/README.md CHANGED
@@ -9,7 +9,7 @@ Install
9
9
  In your Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'xml_resource', '~> 2.0.0'
12
+ gem 'xml_resource', '~> 3.0.0'
13
13
  ```
14
14
 
15
15
  Use
@@ -37,14 +37,18 @@ xml = %q{
37
37
  </library>
38
38
  }
39
39
 
40
- class Library < XmlResource::Base
40
+ class Library
41
+ include XmlResource::Model
42
+
41
43
  has_collection :books
42
44
 
43
45
  has_attribute :open_from, :xpath => 'hours/from'
44
46
  has_attribute :open_till, :xpath => 'hours/till'
45
47
  end
46
48
 
47
- class Book < XmlResource::Base
49
+ class Book
50
+ include XmlResource::Model
51
+
48
52
  has_attribute :name
49
53
  has_attribute :author
50
54
  has_attribute :published, :type => :date, :xpath => 'firstpublished'
@@ -0,0 +1,2 @@
1
+ class XmlResource::ParseError < StandardError; end
2
+ class XmlResource::TypeCastError < StandardError; end
@@ -1,18 +1,20 @@
1
1
  module XmlResource
2
- class Base
2
+ module Model
3
3
  TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set
4
4
 
5
- class_attribute :attributes, :collections, :objects, :root_path, :remove_namespaces, :inflection
6
- self.attributes = {}
7
- self.collections = {}
8
- self.objects = {}
9
- self.inflection = :underscore
10
-
11
- class XmlResource::ParseError < StandardError; end
12
- class XmlResource::TypeCastError < StandardError; end
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ base.class_eval do
8
+ class_attribute :attributes, :collections, :objects, :root_path, :remove_namespaces, :inflection
9
+ self.attributes = {}
10
+ self.collections = {}
11
+ self.objects = {}
12
+ self.inflection = :underscore
13
+ end
14
+ end
13
15
 
14
- class << self
15
-
16
+ module ClassMethods
17
+
16
18
  def from_xml(xml_or_string, default_attrs = {})
17
19
  xml_or_string and xml = parse(xml_or_string) or return
18
20
  attrs = {}
@@ -26,17 +28,18 @@ module XmlResource
26
28
  attrs[name] = value
27
29
  end
28
30
  end
31
+ instance = new(attrs.reject { |k, v| v.nil? }.reverse_merge(default_attrs))
29
32
  self.objects.each do |name, options|
30
33
  if xpath = object_xpath(name)
31
- attrs[name] = object_class(name).from_xml(xml.at(xpath))
34
+ instance.public_send("#{name}=", object_class(name).from_xml(xml.at(xpath)))
32
35
  end
33
36
  end
34
37
  self.collections.each do |name, options|
35
38
  if xpath = collection_xpath(name)
36
- attrs[name] = collection_class(name).collection_from_xml(xml.at(xpath))
39
+ instance.public_send("#{name}=", collection_class(name).collection_from_xml(xml.at(xpath)))
37
40
  end
38
41
  end
39
- new attrs.reject { |k, v| v.nil? }.reverse_merge(default_attrs)
42
+ instance
40
43
  end
41
44
 
42
45
  def collection_from_xml(xml_or_string, default_attrs = {})
@@ -64,7 +67,7 @@ module XmlResource
64
67
  protected
65
68
 
66
69
  def has_attribute(name, options = {})
67
- self.attributes = attributes.merge(name.to_sym => options.symbolize_keys!)
70
+ self.attributes = attributes.merge(name.to_sym => options.symbolize_keys)
68
71
  attribute_accessor_method name
69
72
  end
70
73
 
@@ -74,12 +77,12 @@ module XmlResource
74
77
  end
75
78
 
76
79
  def has_object(name, options = {})
77
- self.objects = objects.merge(name.to_sym => options.symbolize_keys!)
80
+ self.objects = objects.merge(name.to_sym => options.symbolize_keys)
78
81
  attr_accessor name
79
82
  end
80
83
 
81
84
  def has_collection(name, options = {})
82
- self.collections = collections.merge(name.to_sym => options.symbolize_keys!)
85
+ self.collections = collections.merge(name.to_sym => options.symbolize_keys)
83
86
  define_method "#{name}" do
84
87
  instance_variable_get("@#{name}") or instance_variable_set("@#{name}", [])
85
88
  end
@@ -174,7 +177,8 @@ module XmlResource
174
177
  end
175
178
 
176
179
  def initialize(attrs = {})
177
- self.attributes = attrs
180
+ self.attributes = attrs if attrs
181
+ super()
178
182
  end
179
183
 
180
184
  def valid?
@@ -182,7 +186,9 @@ module XmlResource
182
186
  end
183
187
 
184
188
  def attributes=(attrs)
185
- attrs.each { |name, value| send "#{name}=", value }
189
+ attrs.each do |attr, value|
190
+ self.public_send("#{attr}=", value)
191
+ end
186
192
  end
187
193
 
188
194
  def attributes
@@ -1,3 +1,3 @@
1
1
  module XmlResource
2
- VERSION = '2.0.5'
2
+ VERSION = '3.0.0'
3
3
  end
data/lib/xml_resource.rb CHANGED
@@ -1,4 +1,7 @@
1
+ require 'active_support'
1
2
  require 'active_support/core_ext'
2
3
  require 'nokogiri'
4
+
3
5
  require 'xml_resource/version'
4
- require 'xml_resource/base'
6
+ require 'xml_resource/errors'
7
+ require 'xml_resource/model'
@@ -0,0 +1,5 @@
1
+ class Camelid
2
+ include XmlResource::Model
3
+
4
+ has_attribute :humps, type: :integer
5
+ end
@@ -1,4 +1,6 @@
1
- class Contact < XmlResource::Base
1
+ class Contact
2
+ include XmlResource::Model
3
+
2
4
  has_attributes :first_name, :last_name, :type => :string
3
5
 
4
6
  def name
@@ -1,4 +1,6 @@
1
- class Elephant < XmlResource::Base
1
+ class Elephant
2
+ include XmlResource::Model
3
+
2
4
  self.inflection = :upcase
3
5
 
4
6
  has_attribute :color
@@ -0,0 +1,13 @@
1
+ class Gadget
2
+ include XmlResource::Model
3
+
4
+ attr_reader :name
5
+
6
+ has_attribute :magic
7
+
8
+ def initialize(name, attrs = {})
9
+ raise ArgumentError, 'Name is required' if name.blank?
10
+ @name = name
11
+ super(attrs)
12
+ end
13
+ end
data/test/models/item.rb CHANGED
@@ -1,4 +1,6 @@
1
- class Item < XmlResource::Base
1
+ class Item
2
+ include XmlResource::Model
3
+
2
4
  self.root = 'i'
3
5
 
4
6
  has_attribute :quantity, :type => :float
data/test/models/order.rb CHANGED
@@ -1,4 +1,6 @@
1
- class Order < XmlResource::Base
1
+ class Order
2
+ include XmlResource::Model
3
+
2
4
  has_attribute :id, :type => :integer
3
5
  has_attribute :date, :type => :date, :xpath => 'info/date'
4
6
  has_attribute :foobar, :xpath => 'info/foo[@type="bar"]'
data/test/models/shark.rb CHANGED
@@ -1,4 +1,6 @@
1
- class Shark < XmlResource::Base
1
+ class Shark
2
+ include XmlResource::Model
3
+
2
4
  self.inflection = :dasherize
3
5
 
4
6
  has_attribute :shark_color
data/test/test_helper.rb CHANGED
@@ -23,9 +23,21 @@ end
23
23
  require 'rubygems'
24
24
  require 'bundler/setup'
25
25
  Bundler.require(:default)
26
- require 'test/unit'
27
26
  require 'byebug'
28
27
 
28
+ require 'active_support/testing/autorun'
29
+ require 'active_support/test_case'
30
+
31
+ ActiveSupport::TestCase.test_order = :random if ActiveSupport::TestCase.respond_to?(:test_order=)
32
+
29
33
  require 'xml_resource'
30
34
 
31
- Pathname.glob(Pathname.new(__FILE__).dirname.join('models').join('*.rb')).each { |model| require model.to_s.sub(/\.rb\z/, '') }
35
+ require_relative 'models/camelid'
36
+ require_relative 'models/camel'
37
+ require_relative 'models/contact'
38
+ require_relative 'models/dromedary'
39
+ require_relative 'models/elephant'
40
+ require_relative 'models/item'
41
+ require_relative 'models/order'
42
+ require_relative 'models/shark'
43
+ require_relative 'models/gadget'
@@ -1,8 +1,4 @@
1
- # encoding: utf-8
2
-
3
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
4
-
5
- require 'xml_resource'
1
+ require_relative 'test_helper'
6
2
 
7
3
  class XmlResourceTest < ActiveSupport::TestCase
8
4
 
@@ -12,24 +8,24 @@ class XmlResourceTest < ActiveSupport::TestCase
12
8
  end
13
9
 
14
10
  test 'Instantiation of attributes' do
15
- assert_equal 3, orders.size
16
- assert_equal [1, 2, 3], orders.map(&:id)
17
- assert_equal %w(A B C), orders.map(&:foobar)
18
- assert_equal %w(2012-08-27 2012-08-26 2012-08-25).map(&:to_date), orders.map(&:date)
19
- assert_equal [12.99, 10, -12.99], orders.map(&:shipping_cost)
20
- assert_equal [BigDecimal], orders.map(&:shipping_cost).map(&:class).uniq
11
+ assert_equal 3, @orders.size
12
+ assert_equal [1, 2, 3], @orders.map(&:id)
13
+ assert_equal %w(A B C), @orders.map(&:foobar)
14
+ assert_equal %w(2012-08-27 2012-08-26 2012-08-25).map(&:to_date), @orders.map(&:date)
15
+ assert_equal [12.99, 10, -12.99], @orders.map(&:shipping_cost)
16
+ assert_equal [BigDecimal], @orders.map(&:shipping_cost).map(&:class).uniq
21
17
  assert_equal [:quantity, :name], Item.attribute_names
22
18
  end
23
19
 
24
20
  test 'Instantiation of objects' do
25
- assert_equal "Mister Spock", orders.first.customer.name
26
- assert_equal "Käptn Kirk", orders.second.customer.name
27
- assert_equal nil, orders.third.customer
28
- assert_equal ['E F', 'A B', 'C D'], orders.map(&:contact).map(&:name)
21
+ assert_equal "Mister Spock", @orders.first.customer.name
22
+ assert_equal "Käptn Kirk", @orders.second.customer.name
23
+ assert_equal nil, @orders.third.customer
24
+ assert_equal ['E F', 'A B', 'C D'], @orders.map(&:contact).map(&:name)
29
25
  end
30
26
 
31
27
  test 'Instantiation of collections' do
32
- items = orders.map(&:items)
28
+ items = @orders.map(&:items)
33
29
  assert_equal [1, 2, 3], items.map(&:size)
34
30
  assert_equal [[3], [3, 2], [3, 2, 1]], items.map { |ary| ary.map(&:quantity) }
35
31
  assert_equal [%w(1Three), %w(2Three 2Two), %w(3Three 3Two 3One)], items.map { |ary| ary.map(&:name) }
@@ -42,7 +38,7 @@ class XmlResourceTest < ActiveSupport::TestCase
42
38
  end
43
39
 
44
40
  test 'Validation method' do
45
- assert_equal true, orders.first.valid?
41
+ assert_equal true, @orders.first.valid?
46
42
  end
47
43
 
48
44
  test 'Invalid data raises error' do
@@ -50,7 +46,7 @@ class XmlResourceTest < ActiveSupport::TestCase
50
46
  end
51
47
 
52
48
  test 'Boolean attributes' do
53
- assert_equal [true, false, true], orders.map { |o| o.finished }
49
+ assert_equal [true, false, true], @orders.map { |o| o.finished }
54
50
  end
55
51
 
56
52
  test 'Inflections' do
@@ -74,14 +70,21 @@ class XmlResourceTest < ActiveSupport::TestCase
74
70
  assert_equal [1, 3], dromedaries.map(&:humps)
75
71
  end
76
72
 
77
- private
78
-
79
- def orders
80
- @orders
73
+ test 'Custom constructor' do
74
+ assert_nothing_raised do
75
+ gadget = Gadget.new('foo', magic: 5)
76
+ assert_equal 5, gadget.magic
77
+ assert_equal 'foo', gadget.name
78
+ end
79
+ assert_raise ArgumentError do
80
+ gadget = Gadget.new(nil, magic: 6)
81
+ end
81
82
  end
82
83
 
84
+ private
85
+
83
86
  def load_xml(name)
84
- File.read(File.expand_path(File.dirname(__FILE__) + "/../data/#{name}.xml"))
87
+ File.read(File.expand_path(File.join(File.dirname(__FILE__), 'data', "#{name}.xml")))
85
88
  end
86
89
 
87
90
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthias Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-20 00:00:00.000000000 Z
11
+ date: 2015-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: nokogiri
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: byebug
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: Create object trees out of XML documents
@@ -59,24 +59,26 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - lib/xml_resource/base.rb
63
- - lib/xml_resource/version.rb
64
- - lib/xml_resource.rb
65
62
  - MIT-LICENSE
66
- - Rakefile
67
63
  - README.md
68
- - test/cases/xml_resource_test.rb
64
+ - Rakefile
65
+ - lib/xml_resource.rb
66
+ - lib/xml_resource/errors.rb
67
+ - lib/xml_resource/model.rb
68
+ - lib/xml_resource/version.rb
69
69
  - test/data/inflection.xml
70
70
  - test/data/orders.xml
71
- - test/models/00_camelid.rb
72
71
  - test/models/camel.rb
72
+ - test/models/camelid.rb
73
73
  - test/models/contact.rb
74
74
  - test/models/dromedary.rb
75
75
  - test/models/elephant.rb
76
+ - test/models/gadget.rb
76
77
  - test/models/item.rb
77
78
  - test/models/order.rb
78
79
  - test/models/shark.rb
79
80
  - test/test_helper.rb
81
+ - test/xml_resource_test.rb
80
82
  homepage: http://github.com/mtgrosser/xml_resource
81
83
  licenses:
82
84
  - MIT
@@ -87,30 +89,31 @@ require_paths:
87
89
  - lib
88
90
  required_ruby_version: !ruby/object:Gem::Requirement
89
91
  requirements:
90
- - - '>='
92
+ - - ">="
91
93
  - !ruby/object:Gem::Version
92
94
  version: '0'
93
95
  required_rubygems_version: !ruby/object:Gem::Requirement
94
96
  requirements:
95
- - - '>='
97
+ - - ">="
96
98
  - !ruby/object:Gem::Version
97
99
  version: '0'
98
100
  requirements: []
99
101
  rubyforge_project:
100
- rubygems_version: 2.0.3
102
+ rubygems_version: 2.4.5
101
103
  signing_key:
102
104
  specification_version: 4
103
105
  summary: Turn XML into Ruby objects
104
106
  test_files:
105
- - test/cases/xml_resource_test.rb
106
107
  - test/data/inflection.xml
107
108
  - test/data/orders.xml
108
- - test/models/00_camelid.rb
109
109
  - test/models/camel.rb
110
+ - test/models/camelid.rb
110
111
  - test/models/contact.rb
111
112
  - test/models/dromedary.rb
112
113
  - test/models/elephant.rb
114
+ - test/models/gadget.rb
113
115
  - test/models/item.rb
114
116
  - test/models/order.rb
115
117
  - test/models/shark.rb
116
118
  - test/test_helper.rb
119
+ - test/xml_resource_test.rb
@@ -1,3 +0,0 @@
1
- class Camelid < XmlResource::Base
2
- has_attribute :humps, type: :integer
3
- end