oat 0.4.7 → 0.5.0
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.
- checksums.yaml +4 -4
- data/README.md +12 -1
- data/lib/oat/adapter.rb +1 -0
- data/lib/oat/serializer.rb +8 -4
- data/lib/oat/version.rb +1 -1
- data/spec/serializer_spec.rb +34 -1
- 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: ba603c78357fe7c01817de9999727c2cb8980e9f
|
4
|
+
data.tar.gz: 8106d0bdc27f381887c2b614e04b3557e5eb0713
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2b34f0607bb589e0e47f8bb0edec1ffce5057e02dd439a565025845f429add68307ddf7fdde84a75880be72b07301effdea3cd5d9c4a9ade2803272d5978a04
|
7
|
+
data.tar.gz: ca0062c54c4800a7076e86683376fce749293eb7bfb094036da9a3eca35d571438fd9a1b46113fd3105723f1d0aed074b425cd2acb5447fc5f9bff6445e151aa
|
data/README.md
CHANGED
@@ -346,6 +346,17 @@ class SocialSerializer < Oat::Serializer
|
|
346
346
|
end
|
347
347
|
```
|
348
348
|
|
349
|
+
You can specify multiple schema blocks, including across class hierarchies. This allows us to append schema attributes or override previously defined attributes:
|
350
|
+
|
351
|
+
```ruby
|
352
|
+
class ExtendedUserSerializer < UserSerializer
|
353
|
+
schema do
|
354
|
+
name item.full_name # name property will now by the user's full name
|
355
|
+
property :dob, item.dob # additional date of birth attribute
|
356
|
+
end
|
357
|
+
end
|
358
|
+
```
|
359
|
+
|
349
360
|
## URLs
|
350
361
|
|
351
362
|
Hypermedia is all about the URLs linking your resources together. Oat adapters can have methods to declare links in your entity schema but it's up to your code/framework how to create those links.
|
@@ -642,7 +653,7 @@ Now http requests that specify the Siren mime type will work as
|
|
642
653
|
expected.
|
643
654
|
|
644
655
|
**NOTE**
|
645
|
-
The key thing that makes this all
|
656
|
+
The key thing that makes this all work together is that the
|
646
657
|
object passed to `respond_with` implements a `to_FORMAT` method, where
|
647
658
|
`FORMAT` is the symbol used to register the mime type and responder
|
648
659
|
(`:siren`). Without it, Rails will not invoke your responder block.
|
data/lib/oat/adapter.rb
CHANGED
@@ -26,6 +26,7 @@ module Oat
|
|
26
26
|
|
27
27
|
if block_given?
|
28
28
|
serializer_class = Class.new(serializer.class)
|
29
|
+
serializer_class.schemas = []
|
29
30
|
serializer_class.adapter self.class
|
30
31
|
s = serializer_class.new(obj, serializer.context.merge(context_options), serializer.adapter_class, serializer.top)
|
31
32
|
serializer.instance_exec(obj, s, &block)
|
data/lib/oat/serializer.rb
CHANGED
@@ -2,11 +2,12 @@ require 'support/class_attribute'
|
|
2
2
|
module Oat
|
3
3
|
class Serializer
|
4
4
|
|
5
|
-
class_attribute :_adapter, :logger
|
5
|
+
class_attribute :_adapter, :logger, :schemas
|
6
|
+
|
7
|
+
self.schemas = []
|
6
8
|
|
7
9
|
def self.schema(&block)
|
8
|
-
|
9
|
-
@schema || Proc.new{}
|
10
|
+
self.schemas += [block] if block_given?
|
10
11
|
end
|
11
12
|
|
12
13
|
def self.adapter(adapter_class = nil)
|
@@ -51,7 +52,10 @@ module Oat
|
|
51
52
|
|
52
53
|
def to_hash
|
53
54
|
@to_hash ||= (
|
54
|
-
|
55
|
+
self.class.schemas.each do |schema|
|
56
|
+
instance_eval(&schema)
|
57
|
+
end
|
58
|
+
|
55
59
|
adapter.to_hash
|
56
60
|
)
|
57
61
|
end
|
data/lib/oat/version.rb
CHANGED
data/spec/serializer_spec.rb
CHANGED
@@ -78,6 +78,39 @@ describe Oat::Serializer do
|
|
78
78
|
:self => "http://foo.bar.com/#{user1.id}"
|
79
79
|
)
|
80
80
|
end
|
81
|
-
end
|
82
81
|
|
82
|
+
context "when multiple schema blocks are specified across a class hierarchy" do
|
83
|
+
let(:child_serializer) {
|
84
|
+
Class.new(@sc) do
|
85
|
+
schema do
|
86
|
+
attribute :id_plus_x, "#{item.id}_x"
|
87
|
+
|
88
|
+
attributes do |attrs|
|
89
|
+
attrs.inherited "true"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
}
|
94
|
+
|
95
|
+
it "produces the result of both schema blocks in order" do
|
96
|
+
serializer = child_serializer.new(user1, :name => "child_controller")
|
97
|
+
|
98
|
+
expect(serializer.to_hash.fetch(:attributes)).to include(
|
99
|
+
:special => 'Hello',
|
100
|
+
:id => user1.id,
|
101
|
+
:id_plus_x => "#{user1.id}_x",
|
102
|
+
:inherited => "true"
|
103
|
+
)
|
104
|
+
end
|
105
|
+
|
106
|
+
it "does not affect the parent serializer" do
|
107
|
+
serializer = @sc.new(user1, :name => 'some_controller')
|
108
|
+
|
109
|
+
attributes = serializer.to_hash.fetch(:attributes)
|
110
|
+
|
111
|
+
expect(attributes).to_not have_key(:id_plus_x)
|
112
|
+
expect(attributes).to_not have_key(:inherited)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
83
116
|
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.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ismael Celis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|