shreddies 0.3.0 → 0.7.0

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
  SHA256:
3
- metadata.gz: 994ca7a8e1be900a520c23fb51d1d3105ef7ed5354204840b3e4531f1b02bc35
4
- data.tar.gz: 2bb7c93e959057b0d2f3af612bb53ce0bb0a93ca20c2338131971455b8deaa31
3
+ metadata.gz: b512a5aaa15bead3897d186b72c0232b806d21ec1b884689e7fac2b1f6c7db7b
4
+ data.tar.gz: 45c0d21700671b9ecb9c0ef6a19d0c29c1e45172f9567513b12182a6819d7b55
5
5
  SHA512:
6
- metadata.gz: bfd06349411570a2794796d9c4dc9cb6aa5d578123f9b5013aa3882a0547684f27714832901bfc891b8d0a65ed75775156c17222317163dc3621a6191f95d5c3
7
- data.tar.gz: 8c3e4fd74a2fca6c93c28db3ba0394fce74047b12f555d315061e949601bb3d69ae1a266dce3a617986da9f3b4de8592521e5b3783b79c6d48b84fc042acc3b9
6
+ metadata.gz: 5c4aa3c08206be63b44798b22a644220fc536506810ad756f7a73b913bf9e8fd23b8f94014b2b1c6e00eaa1d590473b9786ea6dbb8585e52f16d0b465c528f0b
7
+ data.tar.gz: fa170d48d3d9e4f512bc3fdc06de984fc4370af74ee69a5e01a112b9e0ffb80ef05b75f52f5a9b0feaec643677a90f442120984a4c4c3587481fcdafec7676e6
data/Gemfile CHANGED
@@ -5,7 +5,7 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in shreddies.gemspec
6
6
  gemspec
7
7
 
8
- gem 'rake', '~> 12.0'
8
+ gem 'rake', '~> 13.0'
9
9
  gem 'minitest', '~> 5.0'
10
10
  gem 'autotest-suffix'
11
11
  gem 'minitest-autotest'
@@ -1,54 +1,54 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shreddies (0.2.0)
4
+ shreddies (0.6.0)
5
5
  activerecord (>= 5)
6
6
  railties (>= 5)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- actionpack (6.0.3.2)
12
- actionview (= 6.0.3.2)
13
- activesupport (= 6.0.3.2)
14
- rack (~> 2.0, >= 2.0.8)
11
+ actionpack (6.1.1)
12
+ actionview (= 6.1.1)
13
+ activesupport (= 6.1.1)
14
+ rack (~> 2.0, >= 2.0.9)
15
15
  rack-test (>= 0.6.3)
16
16
  rails-dom-testing (~> 2.0)
17
17
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
18
- actionview (6.0.3.2)
19
- activesupport (= 6.0.3.2)
18
+ actionview (6.1.1)
19
+ activesupport (= 6.1.1)
20
20
  builder (~> 3.1)
21
21
  erubi (~> 1.4)
22
22
  rails-dom-testing (~> 2.0)
23
23
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
24
- activemodel (6.0.3.2)
25
- activesupport (= 6.0.3.2)
26
- activerecord (6.0.3.2)
27
- activemodel (= 6.0.3.2)
28
- activesupport (= 6.0.3.2)
29
- activesupport (6.0.3.2)
24
+ activemodel (6.1.1)
25
+ activesupport (= 6.1.1)
26
+ activerecord (6.1.1)
27
+ activemodel (= 6.1.1)
28
+ activesupport (= 6.1.1)
29
+ activesupport (6.1.1)
30
30
  concurrent-ruby (~> 1.0, >= 1.0.2)
31
- i18n (>= 0.7, < 2)
32
- minitest (~> 5.1)
33
- tzinfo (~> 1.1)
34
- zeitwerk (~> 2.2, >= 2.2.2)
31
+ i18n (>= 1.6, < 2)
32
+ minitest (>= 5.1)
33
+ tzinfo (~> 2.0)
34
+ zeitwerk (~> 2.3)
35
35
  autotest-suffix (1.1.0)
36
36
  builder (3.2.4)
37
- combustion (1.3.0)
37
+ combustion (1.3.1)
38
38
  activesupport (>= 3.0.0)
39
39
  railties (>= 3.0.0)
40
40
  thor (>= 0.14.6)
41
- concurrent-ruby (1.1.6)
41
+ concurrent-ruby (1.1.8)
42
42
  crass (1.0.6)
43
- erubi (1.9.0)
44
- i18n (1.8.3)
43
+ erubi (1.10.0)
44
+ i18n (1.8.7)
45
45
  concurrent-ruby (~> 1.0)
46
- loofah (2.6.0)
46
+ loofah (2.9.0)
47
47
  crass (~> 1.0.2)
48
48
  nokogiri (>= 1.5.9)
49
49
  method_source (1.0.0)
50
- mini_portile2 (2.4.0)
51
- minitest (5.14.1)
50
+ mini_portile2 (2.5.0)
51
+ minitest (5.14.3)
52
52
  minitest-autotest (1.1.1)
53
53
  minitest-server (~> 1.0)
54
54
  path_expander (~> 1.0)
@@ -56,9 +56,11 @@ GEM
56
56
  minitest (>= 4, < 6)
57
57
  minitest-server (1.0.6)
58
58
  minitest (~> 5.0)
59
- nokogiri (1.10.9)
60
- mini_portile2 (~> 2.4.0)
59
+ nokogiri (1.11.1)
60
+ mini_portile2 (~> 2.5.0)
61
+ racc (~> 1.4)
61
62
  path_expander (1.1.0)
63
+ racc (1.5.2)
62
64
  rack (2.2.3)
63
65
  rack-test (1.1.0)
64
66
  rack (>= 1.0, < 3)
@@ -67,19 +69,18 @@ GEM
67
69
  nokogiri (>= 1.6)
68
70
  rails-html-sanitizer (1.3.0)
69
71
  loofah (~> 2.3)
70
- railties (6.0.3.2)
71
- actionpack (= 6.0.3.2)
72
- activesupport (= 6.0.3.2)
72
+ railties (6.1.1)
73
+ actionpack (= 6.1.1)
74
+ activesupport (= 6.1.1)
73
75
  method_source
74
76
  rake (>= 0.8.7)
75
- thor (>= 0.20.3, < 2.0)
76
- rake (12.3.3)
77
+ thor (~> 1.0)
78
+ rake (13.0.3)
77
79
  sqlite3 (1.4.2)
78
- thor (1.0.1)
79
- thread_safe (0.3.6)
80
- tzinfo (1.2.7)
81
- thread_safe (~> 0.1)
82
- zeitwerk (2.3.1)
80
+ thor (1.1.0)
81
+ tzinfo (2.0.4)
82
+ concurrent-ruby (~> 1.0)
83
+ zeitwerk (2.4.2)
83
84
 
84
85
  PLATFORMS
85
86
  ruby
@@ -90,7 +91,7 @@ DEPENDENCIES
90
91
  minitest (~> 5.0)
91
92
  minitest-autotest
92
93
  minitest-focus
93
- rake (~> 12.0)
94
+ rake (~> 13.0)
94
95
  shreddies!
95
96
  sqlite3
96
97
 
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.
@@ -133,6 +165,24 @@ The `Collection` and `Single` modules can be defined and they will be automatica
133
165
 
134
166
  If false, the returned keys will not be transformed. The default is to deeply transform all keys to camelCase.
135
167
 
168
+ #### `except`
169
+
170
+ Pass one or more attribute names as a Symbol or Array of Symbols, and these will be excluded from the results:
171
+
172
+ ```ruby
173
+ User.all.as_json(except: [:first_name, :age])
174
+ ```
175
+
176
+ #### `only`
177
+
178
+ Pass one or more attribute names as a Symbol or Array of Symbols, and _ONLY_ these will be included in the results:
179
+
180
+ ```ruby
181
+ User.all.as_json(only: :first_name)
182
+ ```
183
+
184
+ > Attributes must still be defined within the Serializer.
185
+
136
186
  #### `index_by`
137
187
 
138
188
  Give this option a property of your serialized subject as a Symbol, and the returned collection will be a Hash keyed by that property.
@@ -8,6 +8,8 @@ module Shreddies
8
8
 
9
9
  if serializer.is_a?(String) || serializer.is_a?(Symbol)
10
10
  serializer = serializer.to_s.safe_constantize
11
+ elsif serializer.is_a?(Proc)
12
+ return serializer.call
11
13
  end
12
14
 
13
15
  serializer ? serializer.render_as_json(self, options) : super
@@ -20,6 +22,8 @@ module Shreddies
20
22
 
21
23
  if serializer.is_a?(String) || serializer.is_a?(Symbol)
22
24
  serializer = serializer.to_s.safe_constantize
25
+ elsif serializer.is_a?(Proc)
26
+ return serializer.call
23
27
  end
24
28
 
25
29
  serializer ? serializer.render_as_json(self, options) : super
@@ -44,11 +44,11 @@ module Shreddies
44
44
  super(*methods, to: to, prefix: prefix, allow_nil: allow_nil, private: private)
45
45
  end
46
46
 
47
- attr_reader :subject, :options, :from_collection
47
+ attr_reader :subject, :options
48
48
 
49
- def initialize(subject, options)
49
+ def initialize(subject, opts = {})
50
50
  @subject = subject.is_a?(Hash) ? OpenStruct.new(subject) : subject
51
- @options = { transform_keys: true }.merge(options)
51
+ @options = { transform_keys: true }.merge(opts).with_indifferent_access
52
52
 
53
53
  extend_with_modules
54
54
  end
@@ -65,13 +65,26 @@ module Shreddies
65
65
  end
66
66
  end
67
67
 
68
+ # Filter out methods using the `only` or `except` options.
69
+ if @options[:only]
70
+ @options[:only] = Array(@options[:only])
71
+ methods = methods.select { |x| @options[:only].include? x }
72
+ elsif @options[:except]
73
+ methods = methods.excluding(@options[:except])
74
+ end
75
+
68
76
  methods.map do |attr|
69
- output[attr] = public_send(attr)
77
+ res = public_send(attr)
78
+ if res.is_a?(ActiveRecord::Relation) || res.is_a?(ActiveRecord::Base)
79
+ res = res.as_json(transform_keys: @options[:transform_keys])
80
+ end
81
+
82
+ output[attr] = res
70
83
  end
71
84
 
72
85
  output = before_render(output)
73
86
 
74
- return output unless options[:transform_keys]
87
+ return output unless @options[:transform_keys]
75
88
 
76
89
  output.deep_transform_keys { |key| key.to_s.camelize :lower }
77
90
  end
@@ -88,7 +101,7 @@ module Shreddies
88
101
 
89
102
  # Extend with Collection module if it exists, and a collection is being rendered. Otherwise,
90
103
  # extend with the Single module if that exists.
91
- if options[:from_collection]
104
+ if @options[:from_collection]
92
105
  (collection_mod = "#{ancestor}::Collection".safe_constantize) && extend(collection_mod)
93
106
  else
94
107
  (single_mod = "#{ancestor}::Single".safe_constantize) && extend(single_mod)
@@ -96,11 +109,9 @@ module Shreddies
96
109
  end
97
110
 
98
111
  # Extend with the :module option if given.
99
- if options[:module]
100
- Array(options[:module]).each do |m|
101
- mod = m.is_a?(Module) ? m : "#{self.class}::#{m}".constantize
102
-
103
- extend mod
112
+ if @options[:module]
113
+ Array(@options[:module]).each do |m|
114
+ extend m.is_a?(Module) ? m : "#{self.class}::#{m}".constantize
104
115
  end
105
116
  end
106
117
  end
@@ -1,3 +1,3 @@
1
1
  module Shreddies
2
- VERSION = "0.3.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.summary = 'Stupid simple Rails model and object serializer'
12
12
  spec.homepage = 'https://github.com/joelmoss/shreddies'
13
13
  spec.license = 'MIT'
14
- spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
15
15
 
16
16
  spec.metadata['homepage_uri'] = spec.homepage
17
17
  spec.metadata['source_code_uri'] = spec.homepage
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.3.0
4
+ version: 0.7.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-15 00:00:00.000000000 Z
11
+ date: 2021-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -77,14 +77,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
77
  requirements:
78
78
  - - ">="
79
79
  - !ruby/object:Gem::Version
80
- version: 2.3.0
80
+ version: 2.7.0
81
81
  required_rubygems_version: !ruby/object:Gem::Requirement
82
82
  requirements:
83
83
  - - ">="
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
86
  requirements: []
87
- rubygems_version: 3.1.2
87
+ rubygems_version: 3.1.4
88
88
  signing_key:
89
89
  specification_version: 4
90
90
  summary: Stupid simple Rails model and object serializer