xml_resource 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/xml_resource/base.rb +20 -3
- data/lib/xml_resource/version.rb +1 -1
- data/test/cases/xml_resource_test.rb +20 -1
- data/test/data/inflection.xml +24 -0
- data/test/models/00_camelid.rb +3 -0
- data/test/models/camel.rb +6 -0
- data/test/models/dromedary.rb +3 -0
- data/test/models/elephant.rb +5 -0
- data/test/models/shark.rb +6 -0
- data/test/test_helper.rb +2 -3
- metadata +25 -21
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a9b84a85add7d0dace9d14d734bae2cbbebbeebf
|
4
|
+
data.tar.gz: c5ff1e51e574d32546b621b4272717b90eceb8c7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3850b3775a4eae2ffbfc51a89192d8adac3ee1b74175243c4ca017375b79bdfd572a0c283c20e6d4b6e43263ae19f31e5929a0f674c66f53ba23152310bea28d
|
7
|
+
data.tar.gz: f60b7497c7c28d07421723e1dae9dbc2bee1ca66fdc61b9622d78497489a0b912bbb510712b2b64dde4b6b35b29fabf0e945e3f86b9e82c409e150d67a72b742
|
data/lib/xml_resource/base.rb
CHANGED
@@ -2,10 +2,11 @@ module XmlResource
|
|
2
2
|
class Base
|
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
|
5
|
+
class_attribute :attributes, :collections, :objects, :root_path, :remove_namespaces, :inflection
|
6
6
|
self.attributes = {}
|
7
7
|
self.collections = {}
|
8
8
|
self.objects = {}
|
9
|
+
self.inflection = :underscore
|
9
10
|
|
10
11
|
class XmlResource::ParseError < Exception; end
|
11
12
|
class XmlResource::TypeCastError < Exception; end
|
@@ -47,7 +48,7 @@ module XmlResource
|
|
47
48
|
end
|
48
49
|
|
49
50
|
def root
|
50
|
-
self.root_path || "./#{
|
51
|
+
self.root_path || "./#{inflect(basename)}"
|
51
52
|
end
|
52
53
|
|
53
54
|
def root=(root)
|
@@ -97,7 +98,7 @@ module XmlResource
|
|
97
98
|
[:attribute, :object, :collection].each do |method_name|
|
98
99
|
define_method "#{method_name}_xpath" do |name|
|
99
100
|
options = send(method_name.to_s.pluralize)
|
100
|
-
options[name] && options[name][:xpath] or "./#{name}"
|
101
|
+
options[name] && options[name][:xpath] or "./#{inflect(name)}"
|
101
102
|
end
|
102
103
|
end
|
103
104
|
|
@@ -154,6 +155,22 @@ module XmlResource
|
|
154
155
|
raise XmlResource::ParseError, "cannot parse #{xml_or_string.inspect}"
|
155
156
|
end
|
156
157
|
end
|
158
|
+
|
159
|
+
def inflect(string)
|
160
|
+
string = string.to_s
|
161
|
+
case inflection
|
162
|
+
when :lower_camelcase
|
163
|
+
string.camelcase(:lower)
|
164
|
+
when :upper_camelcase
|
165
|
+
string.camelcase(:upper)
|
166
|
+
when :dasherize
|
167
|
+
string.underscore.dasherize
|
168
|
+
when nil
|
169
|
+
string.underscore
|
170
|
+
else
|
171
|
+
string.public_send(inflection)
|
172
|
+
end
|
173
|
+
end
|
157
174
|
end
|
158
175
|
|
159
176
|
def initialize(attrs = {})
|
data/lib/xml_resource/version.rb
CHANGED
@@ -7,7 +7,7 @@ require 'xml_resource'
|
|
7
7
|
class XmlResourceTest < ActiveSupport::TestCase
|
8
8
|
|
9
9
|
setup do
|
10
|
-
xml =
|
10
|
+
xml = load_xml(:orders)
|
11
11
|
@orders = Order.collection_from_xml(xml)
|
12
12
|
end
|
13
13
|
|
@@ -53,10 +53,29 @@ class XmlResourceTest < ActiveSupport::TestCase
|
|
53
53
|
assert_equal [true, false, true], orders.map { |o| o.finished }
|
54
54
|
end
|
55
55
|
|
56
|
+
test 'Inflections' do
|
57
|
+
xml = load_xml(:inflection)
|
58
|
+
camels = Camel.collection_from_xml(xml)
|
59
|
+
dromedaries = Dromedary.collection_from_xml(xml)
|
60
|
+
assert_equal [2, 2], camels.map(&:humps)
|
61
|
+
assert_equal [1], dromedaries.map(&:humps)
|
62
|
+
|
63
|
+
shark = Shark.collection_from_xml(xml).first
|
64
|
+
assert_equal '2 m', shark.shark_size
|
65
|
+
assert_equal 'white', shark.shark_color
|
66
|
+
|
67
|
+
elephant = Elephant.collection_from_xml(xml).first
|
68
|
+
assert_equal 'grey', elephant.color
|
69
|
+
end
|
70
|
+
|
56
71
|
private
|
57
72
|
|
58
73
|
def orders
|
59
74
|
@orders
|
60
75
|
end
|
61
76
|
|
77
|
+
def load_xml(name)
|
78
|
+
File.read(File.expand_path(File.dirname(__FILE__) + "/../data/#{name}.xml"))
|
79
|
+
end
|
80
|
+
|
62
81
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
<animals>
|
2
|
+
<camel>
|
3
|
+
<camelColor>yellow</camelColor>
|
4
|
+
<camelPrice>1000</camelPrice>
|
5
|
+
<humps>2</humps>
|
6
|
+
</camel>
|
7
|
+
<shark>
|
8
|
+
<shark-color>white</shark-color>
|
9
|
+
<shark-size>2 m</shark-size>
|
10
|
+
</shark>
|
11
|
+
<camel>
|
12
|
+
<camelColor>brown</camelColor>
|
13
|
+
<camelPrice>1500</camelPrice>
|
14
|
+
<humps>2</humps>
|
15
|
+
</camel>
|
16
|
+
<Dromedary>
|
17
|
+
<Humps>1</Humps>
|
18
|
+
</Dromedary>
|
19
|
+
<ELEPHANT>
|
20
|
+
<COLOR>grey</COLOR>
|
21
|
+
</ELEPHANT>
|
22
|
+
</animals>
|
23
|
+
|
24
|
+
|
data/test/test_helper.rb
CHANGED
@@ -24,9 +24,8 @@ require 'rubygems'
|
|
24
24
|
require 'bundler/setup'
|
25
25
|
Bundler.require(:default)
|
26
26
|
require 'test/unit'
|
27
|
+
require 'byebug'
|
27
28
|
|
28
29
|
require 'xml_resource'
|
29
30
|
|
30
|
-
require
|
31
|
-
require File.expand_path('../models/contact', __FILE__)
|
32
|
-
require File.expand_path('../models/order', __FILE__)
|
31
|
+
Pathname.glob(Pathname.new(__FILE__).dirname.join('models').join('*.rb')).each { |model| require model.to_s.sub(/\.rb\z/, '') }
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.3
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Matthias Grosser
|
@@ -14,49 +13,43 @@ dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activesupport
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '3.1'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '3.1'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: nokogiri
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
42
|
+
name: byebug
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - '>='
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - '>='
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
description: Create object trees out of XML documents
|
@@ -73,40 +66,51 @@ files:
|
|
73
66
|
- Rakefile
|
74
67
|
- README.md
|
75
68
|
- test/cases/xml_resource_test.rb
|
69
|
+
- test/data/inflection.xml
|
76
70
|
- test/data/orders.xml
|
71
|
+
- test/models/00_camelid.rb
|
72
|
+
- test/models/camel.rb
|
77
73
|
- test/models/contact.rb
|
74
|
+
- test/models/dromedary.rb
|
75
|
+
- test/models/elephant.rb
|
78
76
|
- test/models/item.rb
|
79
77
|
- test/models/order.rb
|
78
|
+
- test/models/shark.rb
|
80
79
|
- test/test_helper.rb
|
81
80
|
homepage: http://github.com/mtgrosser/xml_resource
|
82
81
|
licenses:
|
83
82
|
- MIT
|
83
|
+
metadata: {}
|
84
84
|
post_install_message:
|
85
85
|
rdoc_options: []
|
86
86
|
require_paths:
|
87
87
|
- lib
|
88
88
|
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
89
|
requirements:
|
91
|
-
- -
|
90
|
+
- - '>='
|
92
91
|
- !ruby/object:Gem::Version
|
93
92
|
version: '0'
|
94
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
-
none: false
|
96
94
|
requirements:
|
97
|
-
- -
|
95
|
+
- - '>='
|
98
96
|
- !ruby/object:Gem::Version
|
99
97
|
version: '0'
|
100
98
|
requirements: []
|
101
99
|
rubyforge_project:
|
102
|
-
rubygems_version:
|
100
|
+
rubygems_version: 2.0.3
|
103
101
|
signing_key:
|
104
|
-
specification_version:
|
102
|
+
specification_version: 4
|
105
103
|
summary: Turn XML into Ruby objects
|
106
104
|
test_files:
|
107
105
|
- test/cases/xml_resource_test.rb
|
106
|
+
- test/data/inflection.xml
|
108
107
|
- test/data/orders.xml
|
108
|
+
- test/models/00_camelid.rb
|
109
|
+
- test/models/camel.rb
|
109
110
|
- test/models/contact.rb
|
111
|
+
- test/models/dromedary.rb
|
112
|
+
- test/models/elephant.rb
|
110
113
|
- test/models/item.rb
|
111
114
|
- test/models/order.rb
|
115
|
+
- test/models/shark.rb
|
112
116
|
- test/test_helper.rb
|