restpack_serializer 0.6.6 → 0.6.7

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: 33de851fe3b900b57c1cd36c7146dd6d7e799ee5
4
- data.tar.gz: fc2674844a0ad0a839cab52e2182c0661611a763
3
+ metadata.gz: ec99f158a9e7825b3134792a1ea5ba5efb2a961f
4
+ data.tar.gz: a33b39771c550f179826c344af688f5d9e84392b
5
5
  SHA512:
6
- metadata.gz: 94794f6281fbd6c7efe7d36ab39439d462ba0200605c1c488dee9522311e4fc5a917e7b660eab673e41d96339ec35a3d9eb67fac87b48dcfb908d8a9705dff9e
7
- data.tar.gz: 7bec627210c72d263d706a53391e48db1721dc9a6ea754a2e31043fa036e5f8cbdd629896d0b42a72062230a9ce2b8d838c2ff85b1ee89902ba79be3068d9203
6
+ metadata.gz: 6412ccee09177e52299f3b8bfbfcdefc5f541f18bb3e5ab95ef126ecb178fd8a7f8304e3ceda94c838d30904056727b9e9413b3175e7cfc437a8eb33214aacf7
7
+ data.tar.gz: 8c2f759aa5ca1fcf1fa4d927e4fb6c156fdabb0a66a1fbc4bce6f9683d944a861d1b2b79107ae9ca8a23e633a6953dc72f25a67cc7c3dbe35f23f7042262a44b
@@ -37,6 +37,7 @@ module RestPack
37
37
  return model.map { |item| as_json(item, context) }
38
38
  end
39
39
 
40
+ apply_whitelist_and_blacklist(context)
40
41
  @model, @context = model, context
41
42
 
42
43
  data = {}
@@ -72,6 +73,41 @@ module RestPack
72
73
  data.merge!(custom) if custom
73
74
  end
74
75
 
76
+ def apply_whitelist_and_blacklist(context)
77
+ blacklist = context[:attribute_blacklist]
78
+ whitelist = context[:attribute_whitelist]
79
+
80
+ if blacklist.present? && whitelist.present?
81
+ raise ArgumentError.new "the context can't define both an `attribute_whitelist` and an `attribute_blacklist`"
82
+ end
83
+
84
+ if blacklist.present?
85
+ blacklist = csv_to_symbol_array(blacklist)
86
+ self.class.serializable_attributes.each do |key, value|
87
+ if blacklist.include? key
88
+ context[value[:include_method_name]] = false
89
+ end
90
+ end
91
+ end
92
+
93
+ if whitelist.present?
94
+ whitelist = csv_to_symbol_array(whitelist)
95
+ self.class.serializable_attributes.each do |key, value|
96
+ unless whitelist.include? key
97
+ context[value[:include_method_name]] = false
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ def csv_to_symbol_array(maybe_csv)
104
+ if maybe_csv.is_a? String
105
+ maybe_csv.split(',').map {|a| a.strip.to_sym}
106
+ else
107
+ maybe_csv
108
+ end
109
+ end
110
+
75
111
  def add_links(model, data)
76
112
  self.class.associations.each do |association|
77
113
  data[:links] ||= {}
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.6.6'
3
+ VERSION = '0.6.7'
4
4
  end
5
5
  end
@@ -198,6 +198,44 @@ describe RestPack::Serializer do
198
198
  ]
199
199
  }
200
200
  end
201
+
202
+ it "excludes a blacklist of attributes if specified as an array" do
203
+ serializer.as_json(person, { attribute_blacklist: [:name, :description] }).should == {
204
+ id: '123',
205
+ href: '/people/123',
206
+ custom_key: 'custom value for model id 123'
207
+ }
208
+ end
209
+
210
+ it "excludes a blacklist of attributes if specified as a string" do
211
+ serializer.as_json(person, { attribute_blacklist: 'name, description' }).should == {
212
+ id: '123',
213
+ href: '/people/123',
214
+ custom_key: 'custom value for model id 123'
215
+ }
216
+ end
217
+
218
+ it "includes a whitelist of attributes if specified as an array" do
219
+ serializer.as_json(person, { attribute_whitelist: [:name, :description] }).should == {
220
+ name: 'Gavin',
221
+ description: 'This is person #123',
222
+ custom_key: 'custom value for model id 123'
223
+ }
224
+ end
225
+
226
+ it "includes a whitelist of attributes if specified as a string" do
227
+ serializer.as_json(person, { attribute_whitelist: 'name, description' }).should == {
228
+ name: 'Gavin',
229
+ description: 'This is person #123',
230
+ custom_key: 'custom value for model id 123'
231
+ }
232
+ end
233
+
234
+ it "raises an exception if both the whitelist and blacklist are provided" do
235
+ expect do
236
+ serializer.as_json(person, { attribute_whitelist: [:name], attribute_blacklist: [:id] })
237
+ end.to raise_error(ArgumentError, "the context can't define both an `attribute_whitelist` and an `attribute_blacklist`")
238
+ end
201
239
  end
202
240
 
203
241
  context "links" do
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.6.6
4
+ version: 0.6.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Joyce
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-11 00:00:00.000000000 Z
11
+ date: 2016-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport