restpack_serializer 0.5.7 → 0.5.8

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: 999010d07a5c68b823e2f2c36496dd1a8b2adbcc
4
- data.tar.gz: 16eca8065b150b5694aa041564f8598361004544
3
+ metadata.gz: c62e329e0e5ad1a715c774afa67cf2fa73c9ecbf
4
+ data.tar.gz: a3a127d3888cf16b3b3561ad01990c1728716b88
5
5
  SHA512:
6
- metadata.gz: a0be747ac4aa315149718107badbfc1327c2c70fc772679deae2c533ed24442a6be4199249644f1aa35eaaabbe408f053530dbc477a28fa88a3e694379ba1bbd
7
- data.tar.gz: b0a43b5d5423e078ce5d521aa8853882f50c7d2d5329ffdc70e21a79bf15aeada5f53fa74299cb3cdefb6a511c5a1233a1428a5a8e3286e73497b3e873aebaf5
6
+ metadata.gz: fb603fa7f78a4a8349bf64ecebcc4fb02f2cbd3956bd4aff643dd4450409ec98e99714bc12c307972d50def980edaa4c61e2a408a17ba043d3ad363f7d8f3b55
7
+ data.tar.gz: c9631648c26608e0718b17348402eb46a6b666d165ed4773772ec37b659e9170cae5794c58e675802f2973109e9017cb8393d928fc41a1d43100e8f7493bcc0c
data/README.md CHANGED
@@ -63,6 +63,8 @@ end
63
63
  class AlbumSerializer
64
64
  include RestPack::Serializer
65
65
  attributes :id, :title, :year, :artist_id, :extras
66
+ optional :score
67
+
66
68
  can_include :artists, :songs
67
69
  can_filter_by :year
68
70
 
@@ -78,6 +80,18 @@ end
78
80
  AlbumSerializer.as_json(album, { admin?: true })
79
81
  ```
80
82
 
83
+ All `attributes` are serialized by default. If you'd like to skip an attribute, you can pass an option in the `@context` as follows:
84
+
85
+ ```ruby
86
+ AlbumSerializer.as_json(album, { include_title?: false })
87
+ ```
88
+
89
+ You can also define `optional` attributes which aren't included by default. To include:
90
+
91
+ ```ruby
92
+ AlbumSerializer.as_json(album, { include_score?: true })
93
+ ```
94
+
81
95
  ## Exposing an API
82
96
 
83
97
  The `AlbumSerializer` provides `page` and `resource` methods which provide paged collection and singular resource GET endpoints.
@@ -114,8 +128,7 @@ AlbumSerializer.page(params, Albums.where("year < 1950"), { admin?: true })
114
128
  ```
115
129
 
116
130
  Other features:
117
- * [Dynamically Include/Exclude Attributes](https://github.com/RestPack/restpack_serializer/blob/master/spec/serializable/serializer_spec.rb#L42)
118
- * [Custom Attributes Hash](https://github.com/RestPack/restpack_serializer/blob/master/spec/serializable/serializer_spec.rb#L46)
131
+ * [Custom Attributes Hash](https://github.com/RestPack/restpack_serializer/blob/master/spec/serializable/serializer_spec.rb#L55)
119
132
 
120
133
  ## Paging
121
134
 
@@ -14,6 +14,10 @@ module RestPack::Serializer::Attributes
14
14
  attrs.each { |attr| attribute attr }
15
15
  end
16
16
 
17
+ def optional(*attrs)
18
+ attrs.each { |attr| optional_attribute attr }
19
+ end
20
+
17
21
  def transform(attrs = [], transform_lambda)
18
22
  attrs.each { |attr| transform_attribute(attr, transform_lambda) }
19
23
  end
@@ -34,23 +38,43 @@ module RestPack::Serializer::Attributes
34
38
  define_include_method name
35
39
  end
36
40
 
41
+ def optional_attribute(name, options={})
42
+ add_to_serializable(name, options)
43
+ define_attribute_method name
44
+ define_optional_include_method name
45
+ end
46
+
37
47
  def define_attribute_method(name)
38
48
  unless method_defined?(name)
39
49
  define_method name do
40
50
  value = self.default_href if name == :href
41
- value ||= @model.send(name)
51
+ if @model.is_a?(Hash)
52
+ value ||= @model[name] || @model[name.to_s]
53
+ else
54
+ value ||= @model.send(name)
55
+ end
42
56
  value = value.to_s if name == :id
43
57
  value
44
58
  end
45
59
  end
46
60
  end
47
61
 
48
- def define_include_method(name)
62
+ def define_optional_include_method(name)
63
+ define_include_method(name, false)
64
+ end
65
+
66
+ def define_include_method(name, include_by_default=true)
49
67
  method = "include_#{name}?".to_sym
50
68
 
51
69
  unless method_defined?(method)
52
- define_method method do
53
- @context[method].nil? || @context[method]
70
+ if include_by_default
71
+ define_method method do
72
+ @context[method].nil? || @context[method]
73
+ end
74
+ else
75
+ define_method method do
76
+ @context[method].present?
77
+ end
54
78
  end
55
79
  end
56
80
  end
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.5.7'
3
+ VERSION = '0.5.8'
4
4
  end
5
5
  end
@@ -4,6 +4,8 @@ describe RestPack::Serializer::Attributes do
4
4
  class CustomSerializer
5
5
  include RestPack::Serializer
6
6
  attributes :a, :b, :c
7
+ attributes :d, :e
8
+ optional :sometimes, :maybe
7
9
  attribute :old_attribute, :key => :new_key
8
10
  transform [:gonzaga], lambda { |name, model| model.send(name).downcase }
9
11
  end
@@ -11,11 +13,11 @@ describe RestPack::Serializer::Attributes do
11
13
  subject(:attributes) { CustomSerializer.serializable_attributes }
12
14
 
13
15
  it "correctly models specified attributes" do
14
- expect(attributes.length).to be(5)
16
+ expect(attributes.length).to be(9)
15
17
  end
16
18
 
17
19
  it "correctly maps normal attributes" do
18
- [:a, :b, :c].each do |attr|
20
+ [:a, :b, :c, :d, :e].each do |attr|
19
21
  expect(attributes[attr]).to eq(attr)
20
22
  end
21
23
  end
@@ -24,6 +26,26 @@ describe RestPack::Serializer::Attributes do
24
26
  expect(attributes[:new_key]).to eq(:old_attribute)
25
27
  end
26
28
 
29
+ describe "optional attributes" do
30
+ let(:model) { OpenStruct.new(a: 'A', sometimes: 'SOMETIMES', gonzaga: 'GONZAGA') }
31
+ let(:context) { {} }
32
+ subject(:as_json) { CustomSerializer.as_json(model, context) }
33
+
34
+ context 'with no includes context' do
35
+ it "excludes by default" do
36
+ expect(as_json[:sometimes]).to eq(nil)
37
+ end
38
+ end
39
+
40
+ context 'with an includes context' do
41
+ let(:context) { { include_sometimes?: true } }
42
+
43
+ it "allows then to be included" do
44
+ expect(as_json[:sometimes]).to eq('SOMETIMES')
45
+ end
46
+ end
47
+ end
48
+
27
49
  describe '#transform_attributes' do
28
50
  let(:model) { OpenStruct.new(gonzaga: 'IS A SCHOOL') }
29
51
 
@@ -33,4 +55,15 @@ describe RestPack::Serializer::Attributes do
33
55
  expect(as_json[:gonzaga]).to eq('is a school')
34
56
  end
35
57
  end
58
+
59
+ describe "model as a hash" do
60
+ let(:model) { { a: 'A', 'b' => 'B' } }
61
+
62
+ subject(:as_json) { CustomSerializer.as_json(model, include_gonzaga?: false) }
63
+
64
+ it 'uses the transform method on the model attribute' do
65
+ expect(as_json[:a]).to eq('A')
66
+ expect(as_json[:b]).to eq('B')
67
+ end
68
+ end
36
69
  end
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.5.7
4
+ version: 0.5.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Joyce
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-01 00:00:00.000000000 Z
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport