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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f3a9878d5d25ca7eb02bc3d9f3e4e4dc6f71ab6f970312331ed01fe864923b4
4
- data.tar.gz: bfba9356df3012c94527f1214d1862c5a507f91aab243f43fc85c8a30b0d50e3
3
+ metadata.gz: 994ca7a8e1be900a520c23fb51d1d3105ef7ed5354204840b3e4531f1b02bc35
4
+ data.tar.gz: 2bb7c93e959057b0d2f3af612bb53ce0bb0a93ca20c2338131971455b8deaa31
5
5
  SHA512:
6
- metadata.gz: '069be646751138acce14f15960e417c18dcb2f57981a49b77d7d1a3a71ef4c2c079c49be4535c8ae193e752cb90298ddca0a72d5f51d12a9f2f2860c940a1e84'
7
- data.tar.gz: 8cf4dd148fe6ab7a6989e52462bc3fd3b14cddf592d17b45bd6470ec60f7482fe21e64a12af1909181bf789981b64a964b30f241e746ff3150de7bb4edfa3245
6
+ metadata.gz: bfd06349411570a2794796d9c4dc9cb6aa5d578123f9b5013aa3882a0547684f27714832901bfc891b8d0a65ed75775156c17222317163dc3621a6191f95d5c3
7
+ data.tar.gz: 8c3e4fd74a2fca6c93c28db3ba0394fce74047b12f555d315061e949601bb3d69ae1a266dce3a617986da9f3b4de8592521e5b3783b79c6d48b84fc042acc3b9
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shreddies (0.1.0)
4
+ shreddies (0.2.0)
5
5
  activerecord (>= 5)
6
6
  railties (>= 5)
7
7
 
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.
@@ -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
- json = {}
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
- json[attr.to_s.camelize :lower] = public_send(attr)
69
+ output[attr] = public_send(attr)
70
70
  end
71
71
 
72
- json.deep_transform_keys { |key| key.to_s.camelize :lower }
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')
@@ -1,3 +1,3 @@
1
1
  module Shreddies
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.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-14 00:00:00.000000000 Z
11
+ date: 2020-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord