oat 0.4.7 → 0.5.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: 6119b57e5a68a59050cdebb3b16d90f75bc2ce9a
4
- data.tar.gz: a33f4b5e3e01b3554f7f04b0ce0cfadbb7477e79
3
+ metadata.gz: ba603c78357fe7c01817de9999727c2cb8980e9f
4
+ data.tar.gz: 8106d0bdc27f381887c2b614e04b3557e5eb0713
5
5
  SHA512:
6
- metadata.gz: 987245ceead54d06e645bd3af2b965aa0f01aa109aa2529149aeb6115f26074cc1e99759719345e5b9d8fbbcce47d233405b0e2fe96b4d07c14e8def8318e3a9
7
- data.tar.gz: 7c096108dee29fac37563fd090b6867557d2183f7861c215e9dc9cd844472d90996f8d1f548af7d49dfc8abedf0f72e155442300e41bfd87067b7ac4f1fe26bf
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 hang together is that the
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)
@@ -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
- @schema = block if block_given?
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
- instance_eval(&self.class.schema)
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
@@ -1,3 +1,3 @@
1
1
  module Oat
2
- VERSION = "0.4.7"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -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.7
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-02-29 00:00:00.000000000 Z
11
+ date: 2016-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport