restpack_serializer 0.5.5 → 0.5.6

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: 1ab97075ec26119008cee6b11195794a748b17af
4
- data.tar.gz: 155498c6a186c00f7fe37b4a8a2af3a02556a888
3
+ metadata.gz: 5d9ebc87b6344eadafb83a213f41bbf0cff31ba8
4
+ data.tar.gz: c4f02a1c7357fda3564d4914283a661b68993309
5
5
  SHA512:
6
- metadata.gz: 2e1eee7a4d17308778a0a76a3efa839ec9c0add561e7eb1817ae2709653cbfd5c407cc9cb50538abb1e37410099b3dbe8e1e8d6e9794cb6ff4a92dc88614c0e4
7
- data.tar.gz: e959700701297dc69ebc1289a80b3c76519a2ca20950bd08c8c5609300e4a710da6ba1741f28c07fc3c997c50e5070246f7dc6bf7a94cb3dbc32de59d67ed2a7
6
+ metadata.gz: 0ba7ae1a03d00c7bb84653881165c4765f9f1177249c119025d370084b70dba5d589470ceb280c09e82374a1805a68f61c494613f5e1e430adf2651b13e0c53e
7
+ data.tar.gz: bad906f6db7243f1aec6e248917c8120606a841509968eb417e08140f180bb15a36e0e6dd50f9730223d6e63db2dcd1e0ad74f64e07c1471c163f4f394fd86c3
@@ -2,7 +2,7 @@ module RestPack::Serializer::Attributes
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  def default_href
5
- "#{RestPack::Serializer.config.href_prefix}/#{self.class.key}/#{@model.to_param}"
5
+ "#{self.class.href_prefix}/#{self.class.key}/#{@model.to_param}"
6
6
  end
7
7
 
8
8
  module ClassMethods
@@ -14,12 +14,22 @@ module RestPack::Serializer::Attributes
14
14
  attrs.each { |attr| attribute attr }
15
15
  end
16
16
 
17
- def attribute(name, options={})
18
- options[:key] ||= name.to_sym
17
+ def transform(attrs = [], transform_lambda)
18
+ attrs.each { |attr| transform_attribute(attr, transform_lambda) }
19
+ end
19
20
 
20
- @serializable_attributes ||= {}
21
- @serializable_attributes[options[:key]] = name
21
+ def transform_attribute(name, transform_lambda, options = {})
22
+ add_to_serializable(name, options)
22
23
 
24
+ define_method name do
25
+ transform_lambda.call(name, @model)
26
+ end
27
+
28
+ define_include_method name
29
+ end
30
+
31
+ def attribute(name, options={})
32
+ add_to_serializable(name, options)
23
33
  define_attribute_method name
24
34
  define_include_method name
25
35
  end
@@ -44,5 +54,12 @@ module RestPack::Serializer::Attributes
44
54
  end
45
55
  end
46
56
  end
57
+
58
+ def add_to_serializable(name, options = {})
59
+ options[:key] ||= name.to_sym
60
+
61
+ @serializable_attributes ||= {}
62
+ @serializable_attributes[options[:key]] = name
63
+ end
47
64
  end
48
65
  end
@@ -54,7 +54,7 @@ module RestPack::Serializer::Paging
54
54
  def page_href(page, options)
55
55
  return nil unless page
56
56
 
57
- url = "#{RestPack::Serializer.config.href_prefix}/#{self.key}"
57
+ url = "#{self.href_prefix}/#{self.key}"
58
58
 
59
59
  params = []
60
60
  params << "page=#{page}" unless page == 1
@@ -34,8 +34,9 @@ module RestPack::Serializer::SideLoading
34
34
  link_key = "#{self.key}.#{association.plural_name}"
35
35
  href = "/#{association.plural_name}?#{singular_key}_id={#{key}.id}"
36
36
  end
37
+
37
38
  links.merge!(link_key => {
38
- :href => RestPack::Serializer.config.href_prefix + href,
39
+ :href => href_prefix + href,
39
40
  :type => association.plural_name.to_sym
40
41
  }
41
42
  )
@@ -89,7 +89,7 @@ module RestPack
89
89
  end
90
90
 
91
91
  module ClassMethods
92
- attr_accessor :model_class, :key
92
+ attr_accessor :model_class, :href_prefix, :key
93
93
 
94
94
  def array_as_json(models, context = {})
95
95
  new.as_json(models, context)
@@ -111,6 +111,10 @@ module RestPack
111
111
  @model_class || self.name.chomp('Serializer').constantize
112
112
  end
113
113
 
114
+ def href_prefix
115
+ @href_prefix || RestPack::Serializer.config.href_prefix
116
+ end
117
+
114
118
  def key
115
119
  (@key || self.model_class.send(:table_name)).to_sym
116
120
  end
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.5.5'
3
+ VERSION = '0.5.6'
4
4
  end
5
5
  end
@@ -1,27 +1,36 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe RestPack::Serializer::Attributes do
4
- class CustomSerializer
5
- include RestPack::Serializer
6
- attributes :a, :b, :c
7
- attribute :old_attribute, :key => :new_key
8
- end
9
-
10
- before do
11
- @attributes = CustomSerializer.serializable_attributes
12
- end
13
-
14
- it "correctly models specified attributes" do
15
- @attributes.length.should == 4
16
- end
17
-
18
- it "correctly maps normal attributes" do
19
- [:a, :b, :c].each do |attr|
20
- @attributes[attr].should == attr
21
- end
22
- end
23
-
24
- it "correctly maps attribute with :key options" do
25
- @attributes[:new_key].should == :old_attribute
26
- end
4
+ class CustomSerializer
5
+ include RestPack::Serializer
6
+ attributes :a, :b, :c
7
+ attribute :old_attribute, :key => :new_key
8
+ transform [:gonzaga], lambda { |name, model| model.send(name).downcase }
9
+ end
10
+
11
+ subject(:attributes) { CustomSerializer.serializable_attributes }
12
+
13
+ it "correctly models specified attributes" do
14
+ expect(attributes.length).to be(5)
15
+ end
16
+
17
+ it "correctly maps normal attributes" do
18
+ [:a, :b, :c].each do |attr|
19
+ expect(attributes[attr]).to eq(attr)
20
+ end
21
+ end
22
+
23
+ it "correctly maps attribute with :key options" do
24
+ expect(attributes[:new_key]).to eq(:old_attribute)
25
+ end
26
+
27
+ describe '#transform_attributes' do
28
+ let(:model) { OpenStruct.new(gonzaga: 'IS A SCHOOL') }
29
+
30
+ subject(:as_json) { CustomSerializer.as_json(model) }
31
+
32
+ it 'uses the transform method on the model attribute' do
33
+ expect(as_json[:gonzaga]).to eq('is a school')
34
+ end
35
+ end
27
36
  end
@@ -37,6 +37,22 @@ describe RestPack::Serializer::Paging do
37
37
  end
38
38
  end
39
39
 
40
+ context 'when href prefix is set' do
41
+ before do
42
+ @original_prefix = MyApp::SongSerializer.href_prefix
43
+ MyApp::SongSerializer.href_prefix = '/api/v3'
44
+ end
45
+ after do
46
+ MyApp::SongSerializer.href_prefix = @original_prefix
47
+ end
48
+ let(:page) do
49
+ MyApp::SongSerializer.page(params, scope, context)
50
+ end
51
+ it 'should use prefixed links' do
52
+ page[:meta][:songs][:next_href].should == '/api/v3/songs?page=2'
53
+ end
54
+ end
55
+
40
56
  context "with custom page size" do
41
57
  let(:params) { { page_size: '3' } }
42
58
  it "returns custom page sizes" do
@@ -67,6 +67,13 @@ describe RestPack::Serializer::SideLoading do
67
67
  MyApp::AlbumSerializer.links["albums.artist"][:href].should == "/api/v1/artists/{albums.artist}"
68
68
  RestPack::Serializer.config.href_prefix = original
69
69
  end
70
+
71
+ it "applies custom serializer href_prefix" do
72
+ original = RestPack::Serializer.config.href_prefix
73
+ MyApp::AlbumSerializer.href_prefix = '/api/v2'
74
+ MyApp::AlbumSerializer.links["albums.artist"][:href].should == "/api/v2/artists/{albums.artist}"
75
+ MyApp::AlbumSerializer.href_prefix = original
76
+ end
70
77
  end
71
78
 
72
79
  describe "#filterable_by" do
metadata CHANGED
@@ -1,207 +1,207 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restpack_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.6
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-10-13 00:00:00.000000000 Z
11
+ date: 2014-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: 4.0.3
20
- - - "<"
20
+ - - <
21
21
  - !ruby/object:Gem::Version
22
22
  version: '5.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ">="
27
+ - - '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: 4.0.3
30
- - - "<"
30
+ - - <
31
31
  - !ruby/object:Gem::Version
32
32
  version: '5.0'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activerecord
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">="
37
+ - - '>='
38
38
  - !ruby/object:Gem::Version
39
39
  version: 4.0.3
40
- - - "<"
40
+ - - <
41
41
  - !ruby/object:Gem::Version
42
42
  version: '5.0'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - ">="
47
+ - - '>='
48
48
  - !ruby/object:Gem::Version
49
49
  version: 4.0.3
50
- - - "<"
50
+ - - <
51
51
  - !ruby/object:Gem::Version
52
52
  version: '5.0'
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: kaminari
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
- - - "~>"
57
+ - - ~>
58
58
  - !ruby/object:Gem::Version
59
59
  version: 0.16.1
60
60
  type: :runtime
61
61
  prerelease: false
62
62
  version_requirements: !ruby/object:Gem::Requirement
63
63
  requirements:
64
- - - "~>"
64
+ - - ~>
65
65
  - !ruby/object:Gem::Version
66
66
  version: 0.16.1
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: restpack_gem
69
69
  requirement: !ruby/object:Gem::Requirement
70
70
  requirements:
71
- - - "~>"
71
+ - - ~>
72
72
  - !ruby/object:Gem::Version
73
73
  version: 0.0.9
74
74
  type: :development
75
75
  prerelease: false
76
76
  version_requirements: !ruby/object:Gem::Requirement
77
77
  requirements:
78
- - - "~>"
78
+ - - ~>
79
79
  - !ruby/object:Gem::Version
80
80
  version: 0.0.9
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rake
83
83
  requirement: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - "~>"
85
+ - - ~>
86
86
  - !ruby/object:Gem::Version
87
87
  version: 10.0.3
88
88
  type: :development
89
89
  prerelease: false
90
90
  version_requirements: !ruby/object:Gem::Requirement
91
91
  requirements:
92
- - - "~>"
92
+ - - ~>
93
93
  - !ruby/object:Gem::Version
94
94
  version: 10.0.3
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: guard-rspec
97
97
  requirement: !ruby/object:Gem::Requirement
98
98
  requirements:
99
- - - "~>"
99
+ - - ~>
100
100
  - !ruby/object:Gem::Version
101
101
  version: 2.5.4
102
102
  type: :development
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  requirements:
106
- - - "~>"
106
+ - - ~>
107
107
  - !ruby/object:Gem::Version
108
108
  version: 2.5.4
109
109
  - !ruby/object:Gem::Dependency
110
110
  name: growl
111
111
  requirement: !ruby/object:Gem::Requirement
112
112
  requirements:
113
- - - "~>"
113
+ - - ~>
114
114
  - !ruby/object:Gem::Version
115
115
  version: 1.0.3
116
116
  type: :development
117
117
  prerelease: false
118
118
  version_requirements: !ruby/object:Gem::Requirement
119
119
  requirements:
120
- - - "~>"
120
+ - - ~>
121
121
  - !ruby/object:Gem::Version
122
122
  version: 1.0.3
123
123
  - !ruby/object:Gem::Dependency
124
124
  name: factory_girl
125
125
  requirement: !ruby/object:Gem::Requirement
126
126
  requirements:
127
- - - "~>"
127
+ - - ~>
128
128
  - !ruby/object:Gem::Version
129
129
  version: 4.2.0
130
130
  type: :development
131
131
  prerelease: false
132
132
  version_requirements: !ruby/object:Gem::Requirement
133
133
  requirements:
134
- - - "~>"
134
+ - - ~>
135
135
  - !ruby/object:Gem::Version
136
136
  version: 4.2.0
137
137
  - !ruby/object:Gem::Dependency
138
138
  name: sqlite3
139
139
  requirement: !ruby/object:Gem::Requirement
140
140
  requirements:
141
- - - "~>"
141
+ - - ~>
142
142
  - !ruby/object:Gem::Version
143
143
  version: 1.3.7
144
144
  type: :development
145
145
  prerelease: false
146
146
  version_requirements: !ruby/object:Gem::Requirement
147
147
  requirements:
148
- - - "~>"
148
+ - - ~>
149
149
  - !ruby/object:Gem::Version
150
150
  version: 1.3.7
151
151
  - !ruby/object:Gem::Dependency
152
152
  name: database_cleaner
153
153
  requirement: !ruby/object:Gem::Requirement
154
154
  requirements:
155
- - - "~>"
155
+ - - ~>
156
156
  - !ruby/object:Gem::Version
157
157
  version: 1.0.1
158
158
  type: :development
159
159
  prerelease: false
160
160
  version_requirements: !ruby/object:Gem::Requirement
161
161
  requirements:
162
- - - "~>"
162
+ - - ~>
163
163
  - !ruby/object:Gem::Version
164
164
  version: 1.0.1
165
165
  - !ruby/object:Gem::Dependency
166
166
  name: rspec
167
167
  requirement: !ruby/object:Gem::Requirement
168
168
  requirements:
169
- - - ">="
169
+ - - '>='
170
170
  - !ruby/object:Gem::Version
171
171
  version: '0'
172
172
  type: :development
173
173
  prerelease: false
174
174
  version_requirements: !ruby/object:Gem::Requirement
175
175
  requirements:
176
- - - ">="
176
+ - - '>='
177
177
  - !ruby/object:Gem::Version
178
178
  version: '0'
179
179
  - !ruby/object:Gem::Dependency
180
180
  name: bump
181
181
  requirement: !ruby/object:Gem::Requirement
182
182
  requirements:
183
- - - ">="
183
+ - - '>='
184
184
  - !ruby/object:Gem::Version
185
185
  version: '0'
186
186
  type: :development
187
187
  prerelease: false
188
188
  version_requirements: !ruby/object:Gem::Requirement
189
189
  requirements:
190
- - - ">="
190
+ - - '>='
191
191
  - !ruby/object:Gem::Version
192
192
  version: '0'
193
193
  - !ruby/object:Gem::Dependency
194
194
  name: protected_attributes
195
195
  requirement: !ruby/object:Gem::Requirement
196
196
  requirements:
197
- - - "~>"
197
+ - - ~>
198
198
  - !ruby/object:Gem::Version
199
199
  version: 1.0.5
200
200
  type: :development
201
201
  prerelease: false
202
202
  version_requirements: !ruby/object:Gem::Requirement
203
203
  requirements:
204
- - - "~>"
204
+ - - ~>
205
205
  - !ruby/object:Gem::Version
206
206
  version: 1.0.5
207
207
  description: Model serialization, paging, side-loading and filtering
@@ -211,10 +211,10 @@ executables: []
211
211
  extensions: []
212
212
  extra_rdoc_files: []
213
213
  files:
214
- - ".gitignore"
215
- - ".rspec"
216
- - ".ruby-version"
217
- - ".travis.yml"
214
+ - .gitignore
215
+ - .rspec
216
+ - .ruby-version
217
+ - .travis.yml
218
218
  - Gemfile
219
219
  - Guardfile
220
220
  - LICENSE
@@ -265,12 +265,12 @@ require_paths:
265
265
  - lib
266
266
  required_ruby_version: !ruby/object:Gem::Requirement
267
267
  requirements:
268
- - - ">="
268
+ - - '>='
269
269
  - !ruby/object:Gem::Version
270
270
  version: '0'
271
271
  required_rubygems_version: !ruby/object:Gem::Requirement
272
272
  requirements:
273
- - - ">="
273
+ - - '>='
274
274
  - !ruby/object:Gem::Version
275
275
  version: '0'
276
276
  requirements: []
@@ -299,4 +299,3 @@ test_files:
299
299
  - spec/serializable/sortable_spec.rb
300
300
  - spec/spec_helper.rb
301
301
  - spec/support/factory.rb
302
- has_rdoc: