restpack_serializer 0.4.11 → 0.4.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -0
- data/lib/restpack_serializer/options.rb +4 -2
- data/lib/restpack_serializer/serializable/paging.rb +5 -5
- data/lib/restpack_serializer/serializable/resource.rb +2 -2
- data/lib/restpack_serializer/version.rb +1 -1
- data/spec/fixtures/serializers.rb +4 -0
- data/spec/serializable/paging_spec.rb +12 -1
- data/spec/serializable/resource_spec.rb +11 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 477bc16aca491e5318b8665a209f293ac5d4b933
|
4
|
+
data.tar.gz: 9f5e498a6996a42b0e6cada857128a23412e5126
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9c20b94f9c75a1b18c38dd89a7e32686cc94e9c6101f29d52dbe937bb01d034047b8b8933886e89549a7433e77f417ac2a4a6e958f740797301944652ab1323
|
7
|
+
data.tar.gz: ed68635e42185820ff8d39fc4b5ad5e4f76c33f0f89e67fbe326228c2c2c8c0623309f28976ba625d543297dd30beaec86a586d8cd1956218bf5d811da1d3f5a
|
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
module RestPack::Serializer
|
2
2
|
class Options
|
3
|
-
attr_accessor :page, :page_size, :include, :filters, :serializer,
|
3
|
+
attr_accessor :page, :page_size, :include, :filters, :serializer,
|
4
|
+
:model_class, :scope, :context, :include_links
|
4
5
|
|
5
|
-
def initialize(serializer, params = {}, scope = nil)
|
6
|
+
def initialize(serializer, params = {}, scope = nil, context = {})
|
6
7
|
params.symbolize_keys! if params.respond_to?(:symbolize_keys!)
|
7
8
|
|
8
9
|
@page = 1
|
@@ -12,6 +13,7 @@ module RestPack::Serializer
|
|
12
13
|
@serializer = serializer
|
13
14
|
@model_class = serializer.model_class
|
14
15
|
@scope = scope || model_class.send(:all)
|
16
|
+
@context = context
|
15
17
|
@include_links = true
|
16
18
|
|
17
19
|
@page = params[:page].to_i if params[:page]
|
@@ -2,8 +2,8 @@ module RestPack::Serializer::Paging
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
module ClassMethods
|
5
|
-
def page(params = {}, scope = nil)
|
6
|
-
page_with_options RestPack::Serializer::Options.new(self, params, scope)
|
5
|
+
def page(params = {}, scope = nil, context = {})
|
6
|
+
page_with_options RestPack::Serializer::Options.new(self, params, scope, context)
|
7
7
|
end
|
8
8
|
|
9
9
|
def page_with_options(options)
|
@@ -13,7 +13,7 @@ module RestPack::Serializer::Paging
|
|
13
13
|
)
|
14
14
|
|
15
15
|
result = RestPack::Serializer::Result.new
|
16
|
-
result.resources[self.key] = serialize_page(page)
|
16
|
+
result.resources[self.key] = serialize_page(page, options)
|
17
17
|
result.meta[self.key] = serialize_meta(page, options)
|
18
18
|
|
19
19
|
if options.include_links
|
@@ -31,8 +31,8 @@ module RestPack::Serializer::Paging
|
|
31
31
|
|
32
32
|
private
|
33
33
|
|
34
|
-
def serialize_page(page)
|
35
|
-
page.map { |model| self.as_json(model) }
|
34
|
+
def serialize_page(page, options)
|
35
|
+
page.map { |model| self.as_json(model, options.context) }
|
36
36
|
end
|
37
37
|
|
38
38
|
def serialize_meta(page, options)
|
@@ -2,8 +2,8 @@ module RestPack::Serializer::Resource
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
module ClassMethods
|
5
|
-
def resource(params = {}, scope = nil)
|
6
|
-
page_with_options RestPack::Serializer::Options.new(self, params, scope)
|
5
|
+
def resource(params = {}, scope = nil, context = {})
|
6
|
+
page_with_options RestPack::Serializer::Options.new(self, params, scope, context)
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
@@ -7,8 +7,10 @@ describe RestPack::Serializer::Paging do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
context "#page" do
|
10
|
-
let(:page) { MyApp::SongSerializer.page(params) }
|
10
|
+
let(:page) { MyApp::SongSerializer.page(params, scope, context) }
|
11
11
|
let(:params) { { } }
|
12
|
+
let(:scope) { nil }
|
13
|
+
let(:context) { { } }
|
12
14
|
|
13
15
|
context "with defaults" do
|
14
16
|
it "page defaults to 1" do
|
@@ -61,6 +63,15 @@ describe RestPack::Serializer::Paging do
|
|
61
63
|
end
|
62
64
|
end
|
63
65
|
|
66
|
+
context "with context" do
|
67
|
+
let(:context) { { reverse_title?: true } }
|
68
|
+
|
69
|
+
it "returns reversed titles" do
|
70
|
+
first = MyApp::Song.first
|
71
|
+
page[:songs].first[:title].should == first.title.reverse
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
64
75
|
it "serializes results" do
|
65
76
|
first = MyApp::Song.first
|
66
77
|
page[:songs].first.should == {
|
@@ -6,14 +6,24 @@ describe RestPack::Serializer::Resource do
|
|
6
6
|
@song = @album.songs.first
|
7
7
|
end
|
8
8
|
|
9
|
-
let(:resource) { MyApp::SongSerializer.resource(params) }
|
9
|
+
let(:resource) { MyApp::SongSerializer.resource(params, scope, context) }
|
10
10
|
let(:params) { { id: @song.id } }
|
11
|
+
let(:scope) { nil }
|
12
|
+
let(:context) { { } }
|
11
13
|
|
12
14
|
it "returns a resource by id" do
|
13
15
|
resource[:songs].count.should == 1
|
14
16
|
resource[:songs][0][:id].should == @song.id.to_s
|
15
17
|
end
|
16
18
|
|
19
|
+
context "with context" do
|
20
|
+
let(:context) { { reverse_title?: true } }
|
21
|
+
|
22
|
+
it "returns reversed titles" do
|
23
|
+
resource[:songs][0][:title].should == @song.title.reverse
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
17
27
|
describe "side-loading" do
|
18
28
|
let(:params) { { id: @song.id, include: 'albums' } }
|
19
29
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restpack_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gavin Joyce
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01
|
11
|
+
date: 2014-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -269,7 +269,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
269
269
|
version: '0'
|
270
270
|
requirements: []
|
271
271
|
rubyforge_project:
|
272
|
-
rubygems_version: 2.0.
|
272
|
+
rubygems_version: 2.0.14
|
273
273
|
signing_key:
|
274
274
|
specification_version: 4
|
275
275
|
summary: Model serialization, paging, side-loading and filtering
|
@@ -290,3 +290,4 @@ test_files:
|
|
290
290
|
- spec/serializable/side_loading/side_loading_spec.rb
|
291
291
|
- spec/spec_helper.rb
|
292
292
|
- spec/support/factory.rb
|
293
|
+
has_rdoc:
|