shreddies 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +10 -0
- data/lib/shreddies/json.rb +13 -5
- data/lib/shreddies/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 994ca7a8e1be900a520c23fb51d1d3105ef7ed5354204840b3e4531f1b02bc35
|
4
|
+
data.tar.gz: 2bb7c93e959057b0d2f3af612bb53ce0bb0a93ca20c2338131971455b8deaa31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfd06349411570a2794796d9c4dc9cb6aa5d578123f9b5013aa3882a0547684f27714832901bfc891b8d0a65ed75775156c17222317163dc3621a6191f95d5c3
|
7
|
+
data.tar.gz: 8c3e4fd74a2fca6c93c28db3ba0394fce74047b12f555d315061e949601bb3d69ae1a266dce3a617986da9f3b4de8592521e5b3783b79c6d48b84fc042acc3b9
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -65,6 +65,8 @@ Model collections and array's are also supported:
|
|
65
65
|
User.all.as_json
|
66
66
|
```
|
67
67
|
|
68
|
+
### Collection and Single Modules
|
69
|
+
|
68
70
|
You may find that you don't want or need to return as much data in collections of objects, or may want to include differtent data. So if a serializer defines a `Collection` module, and a collection or array is being rendered, then that Collection module will automatically be included:
|
69
71
|
|
70
72
|
```ruby
|
@@ -89,6 +91,10 @@ ArticleSerializer < Shreddies::Json
|
|
89
91
|
end
|
90
92
|
```
|
91
93
|
|
94
|
+
### `before_render` callback
|
95
|
+
|
96
|
+
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.
|
97
|
+
|
92
98
|
### Options
|
93
99
|
|
94
100
|
Both `#as_json` and `.render` accepts an `options` hash, which will be forwarded to the serializer class, and available as `options`. This allows you to pass arbitrary options and use them in your serializer.
|
@@ -123,6 +129,10 @@ end
|
|
123
129
|
|
124
130
|
The `Collection` and `Single` modules can be defined and they will be automatically included. The Collection module will be included when rendering an array or ActiveRecord collection (`ActiveRecord::Relation`), and the Single module will be included when rendering a single obejct.
|
125
131
|
|
132
|
+
#### `transform_keys` (default: true)
|
133
|
+
|
134
|
+
If false, the returned keys will not be transformed. The default is to deeply transform all keys to camelCase.
|
135
|
+
|
126
136
|
#### `index_by`
|
127
137
|
|
128
138
|
Give this option a property of your serialized subject as a Symbol, and the returned collection will be a Hash keyed by that property.
|
data/lib/shreddies/json.rb
CHANGED
@@ -47,8 +47,8 @@ module Shreddies
|
|
47
47
|
attr_reader :subject, :options, :from_collection
|
48
48
|
|
49
49
|
def initialize(subject, options)
|
50
|
-
@subject = subject
|
51
|
-
@options = options
|
50
|
+
@subject = subject.is_a?(Hash) ? OpenStruct.new(subject) : subject
|
51
|
+
@options = { transform_keys: true }.merge(options)
|
52
52
|
|
53
53
|
extend_with_modules
|
54
54
|
end
|
@@ -56,7 +56,7 @@ module Shreddies
|
|
56
56
|
# Travel through the ancestors that are serializers (class name ends with "Serializer"), and
|
57
57
|
# call all public instance methods, returning a hash.
|
58
58
|
def as_json
|
59
|
-
|
59
|
+
output = {}.with_indifferent_access
|
60
60
|
methods = Set.new(public_methods(false))
|
61
61
|
|
62
62
|
self.class.ancestors.each do |ancestor|
|
@@ -66,14 +66,22 @@ module Shreddies
|
|
66
66
|
end
|
67
67
|
|
68
68
|
methods.map do |attr|
|
69
|
-
|
69
|
+
output[attr] = public_send(attr)
|
70
70
|
end
|
71
71
|
|
72
|
-
|
72
|
+
output = before_render(output)
|
73
|
+
|
74
|
+
return output unless options[:transform_keys]
|
75
|
+
|
76
|
+
output.deep_transform_keys { |key| key.to_s.camelize :lower }
|
73
77
|
end
|
74
78
|
|
75
79
|
private
|
76
80
|
|
81
|
+
def before_render(output)
|
82
|
+
output
|
83
|
+
end
|
84
|
+
|
77
85
|
def extend_with_modules
|
78
86
|
self.class.ancestors.reverse.each do |ancestor|
|
79
87
|
next unless ancestor.to_s.end_with?('Serializer')
|
data/lib/shreddies/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shreddies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Moss
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|