shreddies 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +32 -0
- data/lib/shreddies/json.rb +7 -4
- data/lib/shreddies/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 362313f00cf053782a4cce7d2ce46b83e8ae078e6812c7c8bb485440f967c3a5
|
4
|
+
data.tar.gz: e1f3f78c27fc4def477bfc1f3adb13fb101a57fbc6870add99dbac9f8efb0db4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67d80f7708e475b54d8e99a5b24d4198091d0ccfef45a84c131539985bdc682bad234e3df12eb4e8351b3648296a2adffc71e796cd8b149a8336a85495d3acb3
|
7
|
+
data.tar.gz: 62e767958dd0b0cd314204a2a7cd573ae7d4df0dd27bdf23a528e64eb4574c2b3e45a5aa9879674a9264c7b57c462f784741a8926e0a82108043396a6caded9e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Shreddies is a JSON serialization library for Rails that focuses on simplicity and speed. No more "magic" DSL's - just plain old Ruby objects! It's primarily intended to serialize Rails models as JSON, but will also work with pretty much anything at all.
|
4
4
|
|
5
|
+
Shreddies primary principle is to be explicit. So a serializer will return nothing until you define some methods. This gives you complete control and everything is a known quantity - no surprises.
|
6
|
+
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
Add this line to your application's Gemfile:
|
@@ -91,6 +93,36 @@ ArticleSerializer < Shreddies::Json
|
|
91
93
|
end
|
92
94
|
```
|
93
95
|
|
96
|
+
### ActiveRecord Associations
|
97
|
+
|
98
|
+
ActiveRecord associations are supported with no additional work on your part. Shreddies will simply call `#as_json` on any method that returns an ActiveRecord model or relation.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
# app/serializers/user_serializer.rb
|
102
|
+
class UserSerializer < Shreddies::Json
|
103
|
+
delegate :articles
|
104
|
+
|
105
|
+
def latest_article
|
106
|
+
articles.latest
|
107
|
+
end
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
And if you need to be specific about what you render, just call the serializer or `#as_json` directly:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
# app/serializers/user_serializer.rb
|
115
|
+
class UserSerializer < Shreddies::Json
|
116
|
+
def articles
|
117
|
+
subject.articles.as_json index_by: :slug
|
118
|
+
end
|
119
|
+
|
120
|
+
def latest_article
|
121
|
+
LatestArticleSerializer.render articles.latest
|
122
|
+
end
|
123
|
+
end
|
124
|
+
```
|
125
|
+
|
94
126
|
### `before_render` callback
|
95
127
|
|
96
128
|
You can define a `#before_render` private method in your serializers, which will act as a callback. It receives the object to be output, and expects you to return the object, which allows you to modify it before rendering.
|
data/lib/shreddies/json.rb
CHANGED
@@ -66,7 +66,12 @@ module Shreddies
|
|
66
66
|
end
|
67
67
|
|
68
68
|
methods.map do |attr|
|
69
|
-
|
69
|
+
res = public_send(attr)
|
70
|
+
if res.is_a?(ActiveRecord::Relation) || res.is_a?(ActiveRecord::Base)
|
71
|
+
res = res.as_json(transform_keys: options[:transform_keys])
|
72
|
+
end
|
73
|
+
|
74
|
+
output[attr] = res
|
70
75
|
end
|
71
76
|
|
72
77
|
output = before_render(output)
|
@@ -98,9 +103,7 @@ module Shreddies
|
|
98
103
|
# Extend with the :module option if given.
|
99
104
|
if options[:module]
|
100
105
|
Array(options[:module]).each do |m|
|
101
|
-
|
102
|
-
|
103
|
-
extend mod
|
106
|
+
extend m.is_a?(Module) ? m : "#{self.class}::#{m}".constantize
|
104
107
|
end
|
105
108
|
end
|
106
109
|
end
|
data/lib/shreddies/version.rb
CHANGED