shreddies 0.4.0 → 0.7.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
  SHA256:
3
- metadata.gz: 362313f00cf053782a4cce7d2ce46b83e8ae078e6812c7c8bb485440f967c3a5
4
- data.tar.gz: e1f3f78c27fc4def477bfc1f3adb13fb101a57fbc6870add99dbac9f8efb0db4
3
+ metadata.gz: 363f96d8b275cca63644e6f00b652f282b5129b81c3a12fb17327008fc61718f
4
+ data.tar.gz: 5bbb94d2a08893a4f1a42689197d283a6c614d41d021269c67378bf2f3fc90ad
5
5
  SHA512:
6
- metadata.gz: 67d80f7708e475b54d8e99a5b24d4198091d0ccfef45a84c131539985bdc682bad234e3df12eb4e8351b3648296a2adffc71e796cd8b149a8336a85495d3acb3
7
- data.tar.gz: 62e767958dd0b0cd314204a2a7cd573ae7d4df0dd27bdf23a528e64eb4574c2b3e45a5aa9879674a9264c7b57c462f784741a8926e0a82108043396a6caded9e
6
+ metadata.gz: 335a534a2da060a82c73178e4cf1353355179bfc1e53c20e14e1b65e3e4d52f60b2eeb71ebe312119b127932b864e6bd112629b0f56cd438fbb714e288a46888
7
+ data.tar.gz: c951402f510d006353c8b289908b50a2ab7eea5f07c093f173541f796a429c2e116ae384752042b131a124b87ebe17ea5b69108870a0070107f3ba6b8ab51a0a
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'
data/Gemfile.lock CHANGED
@@ -1,54 +1,54 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shreddies (0.3.0)
4
+ shreddies (0.7.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.3.1)
12
+ actionview (= 6.1.3.1)
13
+ activesupport (= 6.1.3.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.3.1)
19
+ activesupport (= 6.1.3.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.3.1)
25
+ activesupport (= 6.1.3.1)
26
+ activerecord (6.1.3.1)
27
+ activemodel (= 6.1.3.1)
28
+ activesupport (= 6.1.3.1)
29
+ activesupport (6.1.3.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.10)
45
45
  concurrent-ruby (~> 1.0)
46
- loofah (2.6.0)
46
+ loofah (2.9.1)
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.4)
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.3)
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.3.1)
73
+ actionpack (= 6.1.3.1)
74
+ activesupport (= 6.1.3.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,9 +91,9 @@ 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
 
97
98
  BUNDLED WITH
98
- 2.1.4
99
+ 2.2.15
data/README.md CHANGED
@@ -165,6 +165,24 @@ The `Collection` and `Single` modules can be defined and they will be automatica
165
165
 
166
166
  If false, the returned keys will not be transformed. The default is to deeply transform all keys to camelCase.
167
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
+
168
186
  #### `index_by`
169
187
 
170
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,10 +65,18 @@ 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
77
  res = public_send(attr)
70
78
  if res.is_a?(ActiveRecord::Relation) || res.is_a?(ActiveRecord::Base)
71
- res = res.as_json(transform_keys: options[:transform_keys])
79
+ res = res.as_json(transform_keys: @options[:transform_keys])
72
80
  end
73
81
 
74
82
  output[attr] = res
@@ -76,7 +84,7 @@ module Shreddies
76
84
 
77
85
  output = before_render(output)
78
86
 
79
- return output unless options[:transform_keys]
87
+ return output unless @options[:transform_keys]
80
88
 
81
89
  output.deep_transform_keys { |key| key.to_s.camelize :lower }
82
90
  end
@@ -93,7 +101,7 @@ module Shreddies
93
101
 
94
102
  # Extend with Collection module if it exists, and a collection is being rendered. Otherwise,
95
103
  # extend with the Single module if that exists.
96
- if options[:from_collection]
104
+ if @options[:from_collection]
97
105
  (collection_mod = "#{ancestor}::Collection".safe_constantize) && extend(collection_mod)
98
106
  else
99
107
  (single_mod = "#{ancestor}::Single".safe_constantize) && extend(single_mod)
@@ -101,8 +109,8 @@ module Shreddies
101
109
  end
102
110
 
103
111
  # Extend with the :module option if given.
104
- if options[:module]
105
- Array(options[:module]).each do |m|
112
+ if @options[:module]
113
+ Array(@options[:module]).each do |m|
106
114
  extend m.is_a?(Module) ? m : "#{self.class}::#{m}".constantize
107
115
  end
108
116
  end
@@ -1,3 +1,3 @@
1
1
  module Shreddies
2
- VERSION = "0.4.0"
2
+ VERSION = "0.7.1"
3
3
  end
data/shreddies.gemspec CHANGED
@@ -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.4.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Moss
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-15 00:00:00.000000000 Z
11
+ date: 2021-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '5'
41
- description:
41
+ description:
42
42
  email:
43
43
  - joel@developwithstyle.com
44
44
  executables: []
@@ -69,7 +69,7 @@ metadata:
69
69
  homepage_uri: https://github.com/joelmoss/shreddies
70
70
  source_code_uri: https://github.com/joelmoss/shreddies
71
71
  changelog_uri: https://github.com/joelmoss/shreddies/releases
72
- post_install_message:
72
+ post_install_message:
73
73
  rdoc_options: []
74
74
  require_paths:
75
75
  - lib
@@ -77,15 +77,15 @@ 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
88
- signing_key:
87
+ rubygems_version: 3.2.3
88
+ signing_key:
89
89
  specification_version: 4
90
90
  summary: Stupid simple Rails model and object serializer
91
91
  test_files: []