adequate_serializer 0.1.0 → 0.1.1

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: 51d4f06cfbdbc34070c3a165637576031e8e0e14
4
- data.tar.gz: ed2dca6154272060b027e144888d479551ac4739
3
+ metadata.gz: fa402c6b2f3c574115120ec0fecd0bf881f3ba8b
4
+ data.tar.gz: 4751584ac3ac456908d745fe16ab76c2a8d662e7
5
5
  SHA512:
6
- metadata.gz: 3ce860ec614186a9052f89ad4fe9f5099c6410b008b12d543cec420d4c581832309b2df7fba055c621209b340e3ee13c5815e5425d5eff19b9a21d0fa78520a7
7
- data.tar.gz: 6d36d252fc2dc5756e93e32aca94d5632ae1351269a1c959a483e64b1c1935322abc19b890159f80dde3c8808f70b99e8039a084a7b5e0a019eff94707a650b2
6
+ metadata.gz: 4547364833383fb4a1f88e18c4753e7fb9acdf9ebf56e7d9a75936b940f6538e728101a374dc7ca0dca1160a88e6e0bf09209ec81153e4819779591ee0a10536
7
+ data.tar.gz: f2651a12bafe90ef26f70feb711497c4e95738ae21c6af9119dd444345ea3db20789a1ac39f2146d0eb8874c7798570bfb7f768b7cc737899a7d7cc0bdc5b7be
data/.travis.yml CHANGED
@@ -2,3 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 2.2.2
4
4
  before_install: gem install bundler -v 1.10.5
5
+ notifications:
6
+ email: false
7
+ slack:
8
+ secure: pms4y+KAAznnss/WF3cl7Nsm9gTNe4gTGQYjqcCC7Ygd9Ecuh1W76g2ttwdk4dXQMJE0+2M7TiEPSQijA/zdvjiaFsjm7ghLmyAJze1Yon9RL/JScF+2Ryqun/5LMMAN7vlEJ2FtYkBwCb9BpJNhmwSz5QH110nFvOo3cwgl+1cfFJyxtIT1ByEedrdRg7hfaETiHal7OG0MQcvJcQAh/xxjCEYq9ljRbWCDOYDWPnrmq4abqvZfZaXKg6xj5ZZIXQVWSbguCoao1zvWrxPAF5r2jJWlbL3AcAPRl2CdjmKWzubrf2xV0mwEGx+LbRwZB0ePw3jCg0+u+3QHtd2Dxs/25llk6zKB4KnfKDtlPSuBixFiQLXs2HowJN+0n0gLfbnSqCxsBeToDu7+tVkeMNa5CK6PMes8B2n2C1b7w4Q4bugf9BFFUMPZ2QXx738Bs6zXtI0nB42HV8fCIqtBfVtkdI7OeJRGj3JCvNajocz2gj5XdyW40alkcdPq7rqSqJecE6PfUIJIzDYlOmdcPbZlQjLhq1EWHPviycyIq4/Ddjj3oO747C97HsvoH7MwbCj+/A3hkP0sTJBcDUyCtMLIi54q1fZVKuWKU3jA92kWb06X0FPHBUH8PUF+0gw85sANcBRFTlLEMI7VDq/L8ahWMYCdx/J7+HWkClx6JJw=
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # AdequateSerializer
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/adequate_serializer`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Gem Version][vb]][vl] [![Build Status][tb]][tl] [![Code Climate][cb]][cl]
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ A very opinionated lightweight serializer.
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,20 +22,138 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ ### Controller
26
+
27
+ Just use AdequateSerializer's `serialize` method to serialize your data.
28
+ It doesn't matter whether it is just one object or a collection.
29
+
30
+ ```ruby
31
+ class UsersController < ApplicationController
32
+ def index
33
+ @users = User.all
34
+ render json: serialize(@users)
35
+ end
36
+
37
+ def show
38
+ @user = User.find(params[:id])
39
+ render json: serialize(@user)
40
+ end
41
+ end
42
+ ```
43
+
44
+ The serialize method will infer the serializer's name from the class of the
45
+ object it serializes. In this case it will look for a `UserSerializer`.
46
+
47
+ If you want another serializer, you can just pass it to the `serialize`
48
+ method.
49
+
50
+ ```ruby
51
+ render json: serialize(@user, serializer: AdminSerializer)
52
+ ```
53
+
54
+ ### Root Key
55
+
56
+ By default the `serialize` method will infer the root key from the objects it
57
+ is serializing. For example, when you give it a user the root will be `user`
58
+ or rather `users` if you provide a collection of users.
59
+
60
+ You can also define the root key yourself:
61
+
62
+ ```ruby
63
+ serialize(@user, root: :admin)
64
+ ```
65
+
66
+ or use no root at all:
67
+
68
+ ```ruby
69
+ serialize(@user, root: false)
70
+ ```
71
+
72
+ ### Attributes
73
+
74
+ You can specify which attributes of your objects will be serialized in the
75
+ serializer.
76
+
77
+ ```ruby
78
+ class UserSerializer::Base
79
+ attributes :id, :full_name, :created_at, :updated_at
80
+
81
+ def full_name
82
+ "#{object.first_name} #{object.last_name}"
83
+ end
84
+ end
85
+ ```
86
+
87
+ These can be attributes or methods of your model. You can also define methods
88
+ in the serializer itself and use them.
89
+
90
+ Within a serializer's methods, you can access the object being serialized as
91
+ `object`.
92
+
93
+ ### Associations
94
+
95
+ If you want to include associations, just specify them in the controller.
96
+
97
+ ```ruby
98
+ serialize(@user, includes: :posts)
99
+ ```
100
+
101
+ In this case the PostSerializer will be used to nest the user's posts under the
102
+ `posts` key in the user JSON object.
103
+
104
+ You can also include multiple associations by using an array of association
105
+ keys.
106
+
107
+ #### Overriding association methods
108
+
109
+ If you want to override any association, you can use:
110
+
111
+ ```ruby
112
+ class UserSerializer::Base
113
+ attributes :id, :created_at, :updated_at
114
+
115
+ def posts
116
+ object.posts.published
117
+ end
118
+ end
119
+ ```
26
120
 
27
121
  ## Development
28
122
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
123
+ After checking out the repo, run `bin/setup` to install dependencies.
124
+ Then, run `rake test` to run the tests. You can also run `bin/console` for an
125
+ interactive prompt that will allow you to experiment.
30
126
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
127
+ To install this gem onto your local machine, run `bundle exec rake install`.
128
+ To release a new version, update the version number in `version.rb`,
129
+ and then run `bundle exec rake release`, which will create a git tag for the
130
+ version, push git commits and tags, and push the `.gem` file to
131
+ [rubygems.org][rg].
32
132
 
33
133
  ## Contributing
34
134
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/adequate_serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
135
+ Bug reports and pull requests are welcome on [GitHub][gh]. This project is
136
+ intended to be a safe, welcoming space for collaboration, and contributors
137
+ are expected to adhere to the [Contributor Covenant][cc] code of conduct.
138
+
139
+ ## Credits
140
+
141
+ ![netflower][nl]
36
142
 
143
+ AdequateSerializer is maintained by [netflower Ltd. & Co. KG][n].
37
144
 
38
145
  ## License
39
146
 
40
147
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
148
 
149
+ [vb]: https://badge.fury.io/rb/adequate_serializer.svg
150
+ [vl]: http://badge.fury.io/rb/adequate_serializer
151
+ [tb]: https://travis-ci.org/netflower/adequate_serializer.svg?branch=master
152
+ [tl]: https://travis-ci.org/netflower/adequate_serializer
153
+ [cb]: https://codeclimate.com/github/netflower/adequate_serializer/badges/gpa.svg
154
+ [cl]: https://codeclimate.com/github/netflower/adequate_serializer
155
+ [rg]: https://rubygems.org
156
+ [gh]: https://github.com/netflower/adequate_serializer
157
+ [cc]: contributor-covenant.org
158
+ [n]: http://netflower.de
159
+ [nl]: https://cloud.githubusercontent.com/assets/464565/5997997/91da2232-aac2-11e4-9278-fdf21fb8a6e9.png
@@ -81,8 +81,16 @@ module AdequateSerializer
81
81
  end
82
82
 
83
83
  def serialize_association(association_key)
84
- associated_objects = object.send(association_key)
84
+ associated_objects = associated_objects(association_key)
85
85
  serialize(associated_objects, root: false)
86
86
  end
87
+
88
+ def associated_objects(association_key)
89
+ if respond_to?(association_key)
90
+ send(association_key)
91
+ else
92
+ object.send(association_key)
93
+ end
94
+ end
87
95
  end
88
96
  end
@@ -1,3 +1,3 @@
1
1
  module AdequateSerializer
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adequate_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bastian Bartmann
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-21 00:00:00.000000000 Z
11
+ date: 2015-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport