oat 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/oat/adapter.rb +1 -1
- data/lib/oat/adapters/hal.rb +1 -0
- data/lib/oat/adapters/json_api.rb +29 -6
- data/lib/oat/adapters/siren.rb +1 -0
- data/lib/oat/serializer.rb +1 -1
- data/lib/oat/version.rb +1 -1
- data/spec/adapters/json_api_spec.rb +139 -70
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fc6d823ab477ce6ea91958585093f23afb9d0d2
|
4
|
+
data.tar.gz: 84bf255fd575dd07ad62d6e942c8948ec44b20f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ac3ea124a6cc06fcd3e21130e0ad99af95a59e92c113f8596298a093cc3bc34c879efb36a815ac0b388a77e231beecc8de8306bb2f12722d64c5bd7d17e7286
|
7
|
+
data.tar.gz: cf27b4fd9ca4dcb4d6b7e6d335d5d1055ee643b4f1a6e94d808724cbfa15d50d0120cba18b34e23cc1332df1bd259957920bdda06290ac1dcb70e3e5fb383c58
|
data/lib/oat/adapter.rb
CHANGED
@@ -28,7 +28,7 @@ module Oat
|
|
28
28
|
serializer_class = Class.new(serializer.class)
|
29
29
|
serializer_class.adapter self.class
|
30
30
|
s = serializer_class.new(obj, serializer.context.merge(context_options), serializer.adapter_class, serializer.top)
|
31
|
-
serializer.
|
31
|
+
serializer.instance_exec(obj, s, &block)
|
32
32
|
s.to_hash
|
33
33
|
else
|
34
34
|
serializer_class.new(obj, serializer.context.merge(context_options), serializer.adapter_class, serializer.top).to_hash
|
data/lib/oat/adapters/hal.rb
CHANGED
@@ -17,7 +17,7 @@ module Oat
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def type(*types)
|
20
|
-
@root_name = types.first.to_s
|
20
|
+
@root_name = types.first.to_s.pluralize.to_sym
|
21
21
|
end
|
22
22
|
|
23
23
|
def link(rel, opts = {})
|
@@ -33,11 +33,11 @@ module Oat
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def entity(name, obj, serializer_class = nil, context_options = {}, &block)
|
36
|
-
@entities[name.to_s.pluralize.to_sym] ||= []
|
37
36
|
ent = serializer_from_block_or_class(obj, serializer_class, context_options, &block)
|
37
|
+
entity_hash[name.to_s.pluralize.to_sym] ||= []
|
38
38
|
if ent
|
39
39
|
link name, :href => ent[:id]
|
40
|
-
|
40
|
+
entity_hash[name.to_s.pluralize.to_sym] << ent
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -46,21 +46,36 @@ module Oat
|
|
46
46
|
data[:links][link_name] = []
|
47
47
|
|
48
48
|
collection.each do |obj|
|
49
|
-
|
49
|
+
entity_hash[link_name] ||= []
|
50
50
|
ent = serializer_from_block_or_class(obj, serializer_class, context_options, &block)
|
51
51
|
if ent
|
52
52
|
data[:links][link_name] << ent[:id]
|
53
|
-
|
53
|
+
entity_hash[link_name] << ent
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
+
def collection(name, collection, serializer_class = nil, context_options = {}, &block)
|
59
|
+
@treat_as_resource_collection = true
|
60
|
+
data[:resource_collection] = [] unless data[:resource_collection].is_a?(Array)
|
61
|
+
|
62
|
+
collection.each do |obj|
|
63
|
+
ent = serializer_from_block_or_class(obj, serializer_class, context_options, &block)
|
64
|
+
data[:resource_collection] << ent if ent
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
58
68
|
def to_hash
|
59
69
|
raise "JSON API entities MUST define a type. Use type 'user' in your serializers" unless root_name
|
60
70
|
if serializer.top != serializer
|
61
71
|
return data
|
62
72
|
else
|
63
|
-
h = {
|
73
|
+
h = {}
|
74
|
+
if @treat_as_resource_collection
|
75
|
+
h[root_name] = data[:resource_collection]
|
76
|
+
else
|
77
|
+
h[root_name] = [data]
|
78
|
+
end
|
64
79
|
h[:linked] = @entities if @entities.keys.any?
|
65
80
|
return h
|
66
81
|
end
|
@@ -70,6 +85,14 @@ module Oat
|
|
70
85
|
|
71
86
|
attr_reader :root_name
|
72
87
|
|
88
|
+
def entity_hash
|
89
|
+
if serializer.top == serializer
|
90
|
+
@entities
|
91
|
+
else
|
92
|
+
serializer.top.adapter.entity_hash
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
73
96
|
end
|
74
97
|
end
|
75
98
|
end
|
data/lib/oat/adapters/siren.rb
CHANGED
data/lib/oat/serializer.rb
CHANGED
data/lib/oat/version.rb
CHANGED
@@ -9,60 +9,11 @@ describe Oat::Adapters::JsonAPI do
|
|
9
9
|
let(:hash) { serializer.to_hash }
|
10
10
|
|
11
11
|
describe '#to_hash' do
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
expect(users.size).to be 1
|
16
|
-
expect(users.first).to include(
|
17
|
-
:id => user.id,
|
18
|
-
:name => user.name,
|
19
|
-
:age => user.age,
|
20
|
-
:controller_name => 'some_controller',
|
21
|
-
:message_from_above => nil
|
22
|
-
)
|
23
|
-
|
24
|
-
expect(users.first.fetch(:links)).to include(
|
25
|
-
:self => "http://foo.bar.com/#{user.id}",
|
26
|
-
# these links are added by embedding entities
|
27
|
-
:manager => manager.id,
|
28
|
-
:friends => [friend.id]
|
29
|
-
)
|
30
|
-
|
31
|
-
# embedded friends
|
32
|
-
linked_friends = hash.fetch(:linked).fetch(:friends)
|
33
|
-
expect(linked_friends.size).to be 1
|
34
|
-
expect(linked_friends.first).to include(
|
35
|
-
:id => friend.id,
|
36
|
-
:name => friend.name,
|
37
|
-
:age => friend.age,
|
38
|
-
:controller_name => 'some_controller',
|
39
|
-
:message_from_above => "Merged into parent's context"
|
40
|
-
)
|
41
|
-
|
42
|
-
expect(linked_friends.first.fetch(:links)).to include(
|
43
|
-
:self => "http://foo.bar.com/#{friend.id}",
|
44
|
-
:empty => nil,
|
45
|
-
:friends => []
|
46
|
-
)
|
47
|
-
|
48
|
-
# embedded manager
|
49
|
-
linked_managers = hash.fetch(:linked).fetch(:managers)
|
50
|
-
expect(linked_managers.size).to be 1
|
51
|
-
expect(linked_managers.first).to include(
|
52
|
-
:id => manager.id,
|
53
|
-
:name => manager.name,
|
54
|
-
:age => manager.age,
|
55
|
-
:links => { :self => "http://foo.bar.com/#{manager.id}" }
|
56
|
-
)
|
57
|
-
end
|
58
|
-
|
59
|
-
context 'with a nil entity relationship' do
|
60
|
-
let(:manager) { nil }
|
12
|
+
context 'top level' do
|
13
|
+
subject(:users){ hash.fetch(:users) }
|
14
|
+
its(:size) { should eq(1) }
|
61
15
|
|
62
|
-
it '
|
63
|
-
# the user being serialized
|
64
|
-
users = hash.fetch(:users)
|
65
|
-
expect(users.size).to be 1
|
16
|
+
it 'contains the correct user properties' do
|
66
17
|
expect(users.first).to include(
|
67
18
|
:id => user.id,
|
68
19
|
:name => user.name,
|
@@ -70,33 +21,151 @@ describe Oat::Adapters::JsonAPI do
|
|
70
21
|
:controller_name => 'some_controller',
|
71
22
|
:message_from_above => nil
|
72
23
|
)
|
24
|
+
end
|
73
25
|
|
74
|
-
|
26
|
+
it 'contains the correct user links' do
|
75
27
|
expect(users.first.fetch(:links)).to include(
|
76
28
|
:self => "http://foo.bar.com/#{user.id}",
|
77
29
|
# these links are added by embedding entities
|
30
|
+
:manager => manager.id,
|
78
31
|
:friends => [friend.id]
|
79
32
|
)
|
80
|
-
|
81
|
-
|
82
|
-
expect(linked_friends.size).to be 1
|
83
|
-
expect(linked_friends.first).to include(
|
84
|
-
:id => friend.id,
|
85
|
-
:name => friend.name,
|
86
|
-
:age => friend.age,
|
87
|
-
:controller_name => 'some_controller',
|
88
|
-
:message_from_above => "Merged into parent's context"
|
89
|
-
)
|
33
|
+
end
|
34
|
+
end
|
90
35
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
)
|
36
|
+
context 'linked' do
|
37
|
+
context 'using #entities' do
|
38
|
+
subject(:linked_friends){ hash.fetch(:linked).fetch(:friends) }
|
39
|
+
|
40
|
+
its(:size) { should eq(1) }
|
41
|
+
|
42
|
+
it 'contains the correct properties' do
|
43
|
+
expect(linked_friends.first).to include(
|
44
|
+
:id => friend.id,
|
45
|
+
:name => friend.name,
|
46
|
+
:age => friend.age,
|
47
|
+
:controller_name => 'some_controller',
|
48
|
+
:message_from_above => "Merged into parent's context"
|
49
|
+
)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'contains the correct links' do
|
53
|
+
expect(linked_friends.first.fetch(:links)).to include(
|
54
|
+
:self => "http://foo.bar.com/#{friend.id}",
|
55
|
+
:empty => nil,
|
56
|
+
:friends => []
|
57
|
+
)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'using #entity' do
|
62
|
+
subject(:linked_managers){ hash.fetch(:linked).fetch(:managers) }
|
63
|
+
its(:size) { should eq(1) }
|
64
|
+
|
65
|
+
it "contains the correct properties and links" do
|
66
|
+
expect(linked_managers.first).to include(
|
67
|
+
:id => manager.id,
|
68
|
+
:name => manager.name,
|
69
|
+
:age => manager.age,
|
70
|
+
:links => { :self => "http://foo.bar.com/#{manager.id}" }
|
71
|
+
)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'with nested entities' do
|
76
|
+
let(:friend) { user_class.new('Joe', 33, 2, [other_friend]) }
|
77
|
+
let(:other_friend) { user_class.new('Jack', 28, 4, []) }
|
78
|
+
|
79
|
+
subject(:linked_friends){ hash.fetch(:linked).fetch(:friends) }
|
80
|
+
its(:size) { should eq(2) }
|
81
|
+
|
82
|
+
it 'has the correct entities' do
|
83
|
+
linked_friends.map{ |friend| friend.fetch(:id) }.should include(2, 4)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'with a nil entity relationship' do
|
89
|
+
let(:manager) { nil }
|
90
|
+
let(:users) { hash.fetch(:users) }
|
96
91
|
|
97
|
-
|
92
|
+
it 'excludes the entity from user links' do
|
93
|
+
expect(users.first.fetch(:links)).not_to include(:manager)
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'excludes the entity from the linked hash' do
|
98
97
|
hash.fetch(:linked).fetch(:managers).should be_empty
|
99
98
|
end
|
100
99
|
end
|
100
|
+
|
101
|
+
context 'with an entity collection' do
|
102
|
+
let(:serializer_collection_class) do
|
103
|
+
USER_SERIALIZER = serializer_class unless defined?(USER_SERIALIZER)
|
104
|
+
Class.new(Oat::Serializer) do
|
105
|
+
schema do
|
106
|
+
type 'users'
|
107
|
+
collection :users, item, USER_SERIALIZER
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
let(:collection_serializer){
|
113
|
+
serializer_collection_class.new(
|
114
|
+
[user,friend],
|
115
|
+
{:name => "some_controller"},
|
116
|
+
Oat::Adapters::JsonAPI
|
117
|
+
)
|
118
|
+
}
|
119
|
+
let(:collection_hash) { collection_serializer.to_hash }
|
120
|
+
|
121
|
+
context 'top level' do
|
122
|
+
subject(:users){ collection_hash.fetch(:users) }
|
123
|
+
its(:size) { should eq(2) }
|
124
|
+
|
125
|
+
it 'contains the correct first user properties' do
|
126
|
+
expect(users[0]).to include(
|
127
|
+
:id => user.id,
|
128
|
+
:name => user.name,
|
129
|
+
:age => user.age,
|
130
|
+
:controller_name => 'some_controller',
|
131
|
+
:message_from_above => nil
|
132
|
+
)
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'contains the correct second user properties' do
|
136
|
+
expect(users[1]).to include(
|
137
|
+
:id => friend.id,
|
138
|
+
:name => friend.name,
|
139
|
+
:age => friend.age,
|
140
|
+
:controller_name => 'some_controller',
|
141
|
+
:message_from_above => nil
|
142
|
+
)
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'contains the correct user links' do
|
146
|
+
expect(users.first.fetch(:links)).to include(
|
147
|
+
:self => "http://foo.bar.com/#{user.id}",
|
148
|
+
# these links are added by embedding entities
|
149
|
+
:manager => manager.id,
|
150
|
+
:friends => [friend.id]
|
151
|
+
)
|
152
|
+
end
|
153
|
+
|
154
|
+
context 'sub entity' do
|
155
|
+
subject(:linked_managers){ collection_hash.fetch(:linked).fetch(:managers) }
|
156
|
+
its(:size) { should eq(1) }
|
157
|
+
|
158
|
+
it "contains the correct properties and links" do
|
159
|
+
expect(linked_managers.first).to include(
|
160
|
+
:id => manager.id,
|
161
|
+
:name => manager.name,
|
162
|
+
:age => manager.age,
|
163
|
+
:links => { :self => "http://foo.bar.com/#{manager.id}" }
|
164
|
+
)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
101
170
|
end
|
102
171
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ismael Celis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|