lazy_resource 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,8 +33,8 @@ module LazyResource
33
33
  relation.load(objects)
34
34
  end
35
35
  else
36
- if self.root_node_name && objects.key?(self.root_node_name.to_s)
37
- self.load(objects.delete(self.root_node_name.to_s)).tap do |obj|
36
+ if mapped_name = self.mapped_root_node_name(objects)
37
+ self.load(objects.delete(mapped_name)).tap do |obj|
38
38
  obj.other_attributes = objects
39
39
  end
40
40
  else
@@ -42,6 +42,13 @@ module LazyResource
42
42
  end
43
43
  end
44
44
  end
45
+
46
+ def mapped_root_node_name(objects)
47
+ if self.root_node_name
48
+ root_node_names = self.root_node_name.is_a?(Array) ? self.root_node_name : [self.root_node_name]
49
+ mapped_name = (root_node_names.map(&:to_s) & objects.keys).first
50
+ end
51
+ end
45
52
  end
46
53
 
47
54
  def load(hash, persisted=true)
@@ -51,9 +58,9 @@ module LazyResource
51
58
  resource.persisted = persisted
52
59
  resource.fetched = false
53
60
 
54
- if resource.class.root_node_name && hash.key?(resource.class.root_node_name.to_s)
61
+ if mapped_name = resource.class.mapped_root_node_name(hash)
55
62
  other_attributes = hash
56
- hash = other_attributes.delete(resource.class.root_node_name.to_s)
63
+ hash = other_attributes.delete(mapped_name)
57
64
  self.other_attributes = other_attributes
58
65
  end
59
66
 
@@ -1,3 +1,3 @@
1
1
  module LazyResource
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -162,6 +162,19 @@ describe LazyResource::Mapping do
162
162
  user.load('data' => { :id => 123 }, 'length' => 12)
163
163
  user.other_attributes.should == { 'length' => 12 }
164
164
  end
165
+
166
+ it 'supports multiple root node names with a single resource' do
167
+ Foo.root_node_name = [:data, :datum]
168
+ user = Foo.new
169
+ user.load('data' => { :id => 123 })
170
+ user.id.should == 123
171
+ end
172
+
173
+ it 'supports multiple root node names with a collection' do
174
+ Foo.root_node_name = [:data, :datum]
175
+ users = Foo.load('data' => [{ :id => 123 }, { :id => 124 }])
176
+ users.map(&:id).should == [123, 124]
177
+ end
165
178
  end
166
179
 
167
180
  describe '#load' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-07 00:00:00.000000000 Z
12
+ date: 2012-12-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -137,7 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
137
  version: '0'
138
138
  segments:
139
139
  - 0
140
- hash: 1900980543301721831
140
+ hash: -809900452515726390
141
141
  required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  none: false
143
143
  requirements:
@@ -146,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  version: '0'
147
147
  segments:
148
148
  - 0
149
- hash: 1900980543301721831
149
+ hash: -809900452515726390
150
150
  requirements: []
151
151
  rubyforge_project:
152
152
  rubygems_version: 1.8.24