sinatra-active-model-serializers 0.0.3 → 0.1.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: dc0d28ae98fef8d4d5d9809110025b3b200ec98c
4
- data.tar.gz: c1fd75ba0b95d1bd8fad30bde41d736406657a70
3
+ metadata.gz: 3eee1dfba4d6f3e8f8f560b5877e31c74f7d21a2
4
+ data.tar.gz: d524672188df09d01cdfe9b0ead8114a6b83ca7a
5
5
  SHA512:
6
- metadata.gz: b81affe5ba647d88f9ab23d7d27df18ac74d364ca1b914a17088572b38f14c3a6f4b1f7c6379e0bce3279333c1a0013f4e052cc661c331884317e859391eba4f
7
- data.tar.gz: 12607c5a5ada55124f103072e164e9a5783da5f3101adeb792120820d581ce65b849b622ac4543fc48bcce0d37e520b096773c000db0c155b614ecf9cb03fba0
6
+ metadata.gz: 0121b5801dee190060a47df5049f06056a61bcb8dc79bf4fa4002ed77811bb42bf47d3d21a9bac3a57e2e07c782528d4ada0e5a4284f2b3e520d2be54a4ef2ac
7
+ data.tar.gz: 845b7be3d130fc90ea7cafb035451c9d2531a9c1890d420e1d4cab88d747307827849158f47842ddf78fc7fe72de7eeb93877822ccc6dc92f86608daa15af3d7
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  Gemfile.lock
2
2
  db/sinatra_active_model_serializers.sqlite3
3
- db/schema.rb
3
+ db/schema.rb
4
+ pkg
data/README.md CHANGED
@@ -44,9 +44,9 @@ class App < Sinatra::Base
44
44
  end
45
45
  ```
46
46
 
47
- ## Options
47
+ ## Sets
48
48
 
49
- **active_model_serializers**
49
+ #### active_model_serializers
50
50
 
51
51
  This attribute is an object, all inserted configuration this object it will be passed on to Active Model Serializers, eg.
52
52
 
@@ -54,7 +54,7 @@ This attribute is an object, all inserted configuration this object it will be p
54
54
  set :active_model_serializers, { root: false }
55
55
  ```
56
56
 
57
- **serializers_path**
57
+ #### serializers_path
58
58
 
59
59
  By default this attribute is set up to look for the serializers from your project in "* app / serializers *". Whether you have a different environment you can set up by inserting the path of the string, eg.
60
60
 
@@ -68,18 +68,20 @@ or not to automatically requires
68
68
  set :serializers_path, false
69
69
  ```
70
70
 
71
- **json**
71
+ ## Response JSON
72
72
 
73
73
  When you return a json, you can send a the second parameter.
74
74
  This the second parameter is an object. This object may contain new configurations to assign to the Active Model Serializers or to rewrite any already set by default, eg.
75
75
 
76
+ #### root
77
+
76
78
  ```
77
79
  get '/' do
78
80
  json Resource.first, { root: false }
79
81
  end
80
82
  ```
81
83
 
82
- or
84
+ #### scope
83
85
 
84
86
  ```
85
87
  get '/' do
@@ -87,6 +89,16 @@ get '/' do
87
89
  end
88
90
  ```
89
91
 
92
+ #### serializer
93
+
94
+ If you wish to use an another serializer than the default, you can explicitly pass it through the renderer
95
+
96
+ 1. For a resource:
97
+
98
+ ```ruby
99
+ json Resource.first, { serializer: ResourcePreviewSerializer }
100
+ ```
101
+
90
102
  ## License
91
103
 
92
104
  [MIT](https://github.com/SauloSilva/sinatra-active-model-serializers/blob/master/LICENSE)
@@ -10,9 +10,7 @@ module Sinatra
10
10
  def json(object, options={})
11
11
  options = options.merge(settings.active_model_serializers)
12
12
 
13
- serializer = ActiveModel::Serializer.serializer_for(object, options)
14
-
15
- if serializer
13
+ if serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(object, options))
16
14
  serializer.new(object, options).to_json
17
15
  else
18
16
  object.to_json(options)
@@ -1,6 +1,6 @@
1
1
  module SinatraActiveModelSerializers
2
- PATCH = 3
3
- MINOR = 0
2
+ PATCH = 0
3
+ MINOR = 1
4
4
  MAJOR = 0
5
5
  VERSION = "#{ MAJOR }.#{ MINOR }.#{ PATCH }".freeze
6
6
  end
@@ -27,6 +27,13 @@ module App
27
27
  get '/without-root/' do
28
28
  json Test.create(foo: 'bar'), { root: false }
29
29
  end
30
+
31
+ get '/with-specific-serializer/' do
32
+ json Test.create(foo: 'bar'), {
33
+ root: false,
34
+ serializer: FooSerializer
35
+ }
36
+ end
30
37
  end
31
38
  end
32
39
 
data/spec/json_spec.rb CHANGED
@@ -10,4 +10,9 @@ describe App::Base do
10
10
  get '/without-root/'
11
11
  match_response_schema(:test_without_root)
12
12
  end
13
+
14
+ it 'should response with specific serializer' do
15
+ get '/with-specific-serializer/'
16
+ match_response_schema(:test_with_specific_serializer)
17
+ end
13
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-active-model-serializers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - saulosantiago
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-08 00:00:00.000000000 Z
11
+ date: 2015-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers