bright_serializer 0.4.1 → 0.5.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: 840379a67e4354592d042785306facc35b8e663ed8cc27da09b9158b3d43fc6f
4
- data.tar.gz: 817cf7a8dca2434c8e89cf3813678c1005086d9edb0fd11c76c4a3461470873f
3
+ metadata.gz: ccd3ca73a3a175ff41e1b661b1d4287e330e51fac24cc4483e88f231d711006c
4
+ data.tar.gz: 2d326508db9454e2ff3923731572710caa07f11584f3cf06bf9634e851e5bbcc
5
5
  SHA512:
6
- metadata.gz: 1a598fcf576edf3395723c01dbaa7c00c45bc76ad2363599b9e16af0a7f4662a55007a6de49073cc2bb2bf36ceb63320c3fcfd58459147917b041deda1ae3204
7
- data.tar.gz: 711ca41015c6fed810bd22ae1ce1914d87f9b40fdb33b084b891b4134ea5af1d291a1bce95e608f9f8cd3babf25440f1cce05433c6a8ddd935a4b2c43eebc907
6
+ metadata.gz: 2c025a5b514015a760587254fb18e5a8eaf3d58e522031300f6d4a5a3049ba7af0462371cb951ec3637f471ffd015981a74affd8fc1d4d20b9fe87874d598082
7
+ data.tar.gz: 4a6182583ccb22ee99360852c3dc0ca6d8b097cba21dba8608062325f7958bc1a90f774b87e19e389785e042a90c7aab4da30f548efda101085e0f7af08bdc5e
data/README.md CHANGED
@@ -205,6 +205,16 @@ class AccountSerializer
205
205
  end
206
206
  ```
207
207
 
208
+ ## Benchmark
209
+
210
+ Event if the main goal is not performance, it has very good result.
211
+
212
+ ```sh
213
+ ruby benchmarks/collection.rb
214
+ ```
215
+
216
+ <img src="benchmarks/ips.png" width="400px">
217
+
208
218
  ## Development
209
219
 
210
220
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -32,7 +32,7 @@ module BrightSerializer
32
32
 
33
33
  def attribute_value(serializer_instance, object, params)
34
34
  if @block
35
- if @block.arity.abs == 1
35
+ if @block.arity.negative?
36
36
  serializer_instance.instance_exec(object, &@block)
37
37
  else
38
38
  serializer_instance.instance_exec(object, params, &@block)
@@ -23,7 +23,7 @@ module BrightSerializer
23
23
  private
24
24
 
25
25
  def class_serializer
26
- @class_serializer ||= @serializer.is_a?(String) ? Inflector.constantize(@serializer) : @serializer
26
+ @class_serializer ||= @serializer.is_a?(String) ? @serializer.constantize : @serializer
27
27
  end
28
28
 
29
29
  def add_entity_ref!(entity)
@@ -24,7 +24,7 @@ module BrightSerializer
24
24
  object = nested_hash(@definition, 'ref')
25
25
  return unless object
26
26
 
27
- ref_entity_name = Inflector.constantize(object.delete('ref')).entity_name
27
+ ref_entity_name = object.delete('ref').constantize.entity_name
28
28
  relation = "#/definitions/#{ref_entity_name}"
29
29
  object['$ref'] = relation
30
30
  end
@@ -29,37 +29,6 @@ class Inflector
29
29
  underscored_word
30
30
  end
31
31
 
32
- # File activesupport/lib/active_support/inflector/methods.rb, line 271
33
- def constantize(camel_cased_word)
34
- names = camel_cased_word.split('::')
35
-
36
- # Trigger a built-in NameError exception including the ill-formed constant in the message.
37
- Object.const_get(camel_cased_word) if names.empty?
38
-
39
- # Remove the first blank element in case of '::ClassName' notation.
40
- names.shift if names.size > 1 && names.first.empty?
41
-
42
- names.inject(Object) do |constant, name|
43
- if constant == Object
44
- constant.const_get(name)
45
- else
46
- candidate = constant.const_get(name)
47
- next candidate if constant.const_defined?(name, false)
48
- next candidate unless Object.const_defined?(name)
49
-
50
- # Go down the ancestors to check if it is owned directly. The check
51
- # stops when we reach Object or the end of ancestors tree.
52
- constant = constant.ancestors.each_with_object(constant) do |ancestor, const|
53
- break const if ancestor == Object
54
- break ancestor if ancestor.const_defined?(name, false)
55
- end
56
-
57
- # owner is in Object, so raise
58
- constant.const_get(name, false)
59
- end
60
- end
61
- end
62
-
63
32
  # File active_support/core_ext/hash/keys.rb, line 116
64
33
  def deep_transform_keys_in_object(object, &block)
65
34
  case object
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'oj'
4
- require 'active_support/deprecation'
5
4
  require_relative 'attribute'
6
5
  require_relative 'attribute_relation'
7
6
  require_relative 'inflector'
@@ -14,11 +13,6 @@ module BrightSerializer
14
13
 
15
14
  SUPPORTED_TRANSFORMATION = %i[camel camel_lower dash underscore].freeze
16
15
  DEFAULT_OJ_OPTIONS = { mode: :compat, time_format: :ruby, use_to_json: true }.freeze
17
- DEPRECATION_MESSAGE = 'BrightSerializer: Serializing `nil` will stop returning ' \
18
- "a JSON with all attributes and null values.\n" \
19
- "To keep the old behaviour use an empty hash `MySerializer.new(object || { }).to_json`.\n" \
20
- "See: https://github.com/petalmd/bright_serializer/issues/103 for more details about this change.\n"
21
- private_constant :DEPRECATION_MESSAGE
22
16
 
23
17
  def self.included(base)
24
18
  super
@@ -33,7 +27,7 @@ module BrightSerializer
33
27
  end
34
28
 
35
29
  def serialize(object, attributes_to_serialize)
36
- ActiveSupport::Deprecation.warn(DEPRECATION_MESSAGE) if object.nil?
30
+ return nil if @object.nil?
37
31
 
38
32
  attributes_to_serialize.each_with_object({}) do |attribute, result|
39
33
  next unless attribute.condition?(object, @params)
@@ -68,8 +62,9 @@ module BrightSerializer
68
62
  subclass.instance_variable_set(:@transform_method, @transform_method) unless subclass.transform_method
69
63
  end
70
64
 
71
- def attributes(*attributes, **options, &block)
72
- attributes.each do |key|
65
+ def attributes(*args, &block)
66
+ options = args.extract_options!
67
+ args.each do |key|
73
68
  attribute = Attribute.new(key, options[:if], options[:entity], &block)
74
69
  attribute.transformed_key = run_transform_key(key)
75
70
  @attributes_to_serialize << attribute
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BrightSerializer
4
- VERSION = '0.4.1'
4
+ VERSION = '0.5.0'
5
5
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'active_support/core_ext/array/extract_options'
3
5
  require 'bright_serializer/version'
4
6
 
5
7
  module BrightSerializer
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bright_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean-Francis Bastien
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-10 00:00:00.000000000 Z
11
+ date: 2023-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: oj
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '3'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '3'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: activesupport
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -39,55 +25,13 @@ dependencies:
39
25
  - !ruby/object:Gem::Version
40
26
  version: '5.2'
41
27
  - !ruby/object:Gem::Dependency
42
- name: bundler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '2'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '2'
55
- - !ruby/object:Gem::Dependency
56
- name: faker
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '2'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '2'
69
- - !ruby/object:Gem::Dependency
70
- name: rake
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '13.0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '13.0'
83
- - !ruby/object:Gem::Dependency
84
- name: rspec
28
+ name: oj
85
29
  requirement: !ruby/object:Gem::Requirement
86
30
  requirements:
87
31
  - - "~>"
88
32
  - !ruby/object:Gem::Version
89
33
  version: '3.0'
90
- type: :development
34
+ type: :runtime
91
35
  prerelease: false
92
36
  version_requirements: !ruby/object:Gem::Requirement
93
37
  requirements:
@@ -99,19 +43,12 @@ email:
99
43
  - jfbastien@petalmd.com
100
44
  executables: []
101
45
  extensions: []
102
- extra_rdoc_files: []
46
+ extra_rdoc_files:
47
+ - LICENSE.txt
48
+ - README.md
103
49
  files:
104
- - ".gitignore"
105
- - ".rspec"
106
- - ".rubocop.yml"
107
- - ".ruby-version"
108
- - CHANGELOG.md
109
- - Gemfile
110
- - Gemfile.lock
111
50
  - LICENSE.txt
112
51
  - README.md
113
- - Rakefile
114
- - bright_serializer.gemspec
115
52
  - lib/bright_serializer.rb
116
53
  - lib/bright_serializer/attribute.rb
117
54
  - lib/bright_serializer/attribute_relation.rb
@@ -127,8 +64,10 @@ licenses:
127
64
  - MIT
128
65
  metadata:
129
66
  homepage_uri: https://github.com/petalmd/bright_serializer
130
- source_code_uri: https://github.com/petalmd/bright_serializer
131
67
  changelog_uri: https://github.com/petalmd/bright_serializer/blob/master/CHANGELOG.md
68
+ source_code_uri: https://github.com/petalmd/bright_serializer
69
+ bug_tracker_uri: https://github.com/petalmd/bright_serializer/issues
70
+ rubygems_mfa_required: 'true'
132
71
  post_install_message:
133
72
  rdoc_options: []
134
73
  require_paths:
data/.gitignore DELETED
@@ -1,14 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
-
10
- # rspec failure tracking
11
- .rspec_status
12
-
13
- # IDE files
14
- .idea/
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.rubocop.yml DELETED
@@ -1,44 +0,0 @@
1
- require:
2
- - rubocop-performance
3
- - rubocop-rspec
4
-
5
- AllCops:
6
- NewCops: enable
7
- DisplayStyleGuide: true # Include styleguide and reference URLs
8
- ExtraDetails: true # Include cop details
9
-
10
- Metrics/BlockLength:
11
- Enabled: false
12
-
13
- Layout/LineLength:
14
- Max: 120
15
-
16
- Style/Documentation:
17
- Enabled: false
18
-
19
- RSpec/ExampleLength:
20
- Enabled: false
21
-
22
- RSpec/NamedSubject:
23
- Enabled: false
24
-
25
- Gemspec/RequiredRubyVersion:
26
- Enabled: false
27
-
28
- Metrics/PerceivedComplexity:
29
- Enabled: false
30
-
31
- Metrics/MethodLength:
32
- Enabled: false
33
-
34
- Metrics/CyclomaticComplexity:
35
- Enabled: false
36
-
37
- Metrics/AbcSize:
38
- Enabled: false
39
-
40
- Metrics/ParameterLists:
41
- Max: 6
42
-
43
- RSpec/NestedGroups:
44
- Enabled: false
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.7.1
data/CHANGELOG.md DELETED
@@ -1,63 +0,0 @@
1
- # Change log
2
-
3
- ## master (unreleased)
4
-
5
- ## 0.4.1 (2022-04-10)
6
-
7
- * Add deprecation warning for serialize nil object. See [issue #103](https://github.com/petalmd/bright_serializer/issues/103). ([v0.4.0...v0.4.1](https://github.com/petalmd/bright_serializer/compare/v0.4.0...v0.4.1))
8
-
9
- ## 0.4.0 (2022-11-10)
10
-
11
- * Added relation helper methods `has_one`, `has_many`, `belongs_to` ([#49](https://github.com/petalmd/bright_serializer/pull/49))
12
- * Performance improvements, save in instance attributes to serialize. ([#100](https://github.com/petalmd/bright_serializer/pull/100))
13
- * Performance improvements, calculate attributes to serialize only once. ([#98](https://github.com/petalmd/bright_serializer/pull/98))
14
- * Add instrumentation. ([#90](https://github.com/petalmd/bright_serializer/pull/90))
15
-
16
- ## 0.3.1 (2022-09-28)
17
-
18
- * Performance improvements, use nil instead of empty set. ([#97](https://github.com/petalmd/bright_serializer/pull/97))
19
- * Move specs out of lib. ([#96](https://github.com/petalmd/bright_serializer/pull/96))
20
-
21
- ## 0.3.0 (2022-05-26)
22
-
23
- * Allow to evaluate entity values with a callable lambda. ([#88](https://github.com/petalmd/bright_serializer/pull/88))
24
- * Fix `FrozenError (can't modify frozen Array)` when parsing entity. ([#83](https://github.com/petalmd/bright_serializer/pull/83))
25
- * Added the support to use instance methods from a serializer class in the library ([#85](https://github.com/petalmd/bright_serializer/pull/85))
26
- * Use real coveralls_reborn gem
27
-
28
- ## 0.2.5 (2021-03-08)
29
-
30
- * When serializing an Hash, check present of the key before trying string ([#57](https://github.com/petalmd/bright_serializer/pull/57))
31
-
32
- ## 0.2.4 (2021-02-19)
33
-
34
- * Try symbol and string keys when the object to serialize is an Hash ([#54](https://github.com/petalmd/bright_serializer/pull/54))
35
-
36
- ## 0.2.3 (2021-01-04)
37
-
38
- * Update dependencies ([v0.2.2...v0.2.3](https://github.com/petalmd/bright_serializer/compare/v0.2.2...v0.2.3))
39
-
40
- ## 0.2.2 (2020-07-22)
41
-
42
- * Run CI build on all supported Ruby versions ([#11](https://github.com/petalmd/bright_serializer/pull/11))
43
- * Update Rubocop 0.78.0 => 0.88.0 and run auto-correction
44
- * Deep transform entity keys ([#12](https://github.com/petalmd/bright_serializer/pull/12))
45
-
46
- ## 0.2.1 (2020-07-21)
47
-
48
- * Handle set_key_transform inherited from a parent serializer ([#10](https://github.com/petalmd/bright_serializer/pull/10))
49
-
50
- ## 0.2.0 (2020-07-17)
51
-
52
- * Add RubyGems version badge
53
- * Handle inherit from a parent serializer
54
- * Define entity in serializer for grape_swagger ([#9](https://github.com/petalmd/bright_serializer/pull/9))
55
-
56
- ## 0.1.1 (2020-07-13)
57
-
58
- * Add description in gemspec file
59
- * Add content in CHANGELOG.md
60
-
61
- ## 0.1.0 (2020-07-13)
62
-
63
- * First release
data/Gemfile DELETED
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
-
7
- # Specify your gem's dependencies in bright_serializer.gemspec
8
- gemspec
9
-
10
- gem 'oj', require: false
11
-
12
- group :test do
13
- gem 'coveralls_reborn', require: false
14
- gem 'faker'
15
- gem 'rubocop'
16
- gem 'rubocop-performance'
17
- gem 'rubocop-rspec'
18
- end
data/Gemfile.lock DELETED
@@ -1,102 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- bright_serializer (0.4.1)
5
- activesupport (>= 5.2)
6
- oj (~> 3)
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- activesupport (5.2.8.1)
12
- concurrent-ruby (~> 1.0, >= 1.0.2)
13
- i18n (>= 0.7, < 2)
14
- minitest (~> 5.1)
15
- tzinfo (~> 1.1)
16
- ast (2.4.1)
17
- concurrent-ruby (1.1.10)
18
- coveralls_reborn (0.22.0)
19
- simplecov (>= 0.18.1, < 0.22.0)
20
- term-ansicolor (~> 1.6)
21
- thor (>= 0.20.3, < 2.0)
22
- tins (~> 1.16)
23
- diff-lcs (1.4.4)
24
- docile (1.4.0)
25
- faker (2.15.1)
26
- i18n (>= 1.6, < 2)
27
- i18n (1.12.0)
28
- concurrent-ruby (~> 1.0)
29
- minitest (5.16.3)
30
- oj (3.11.1)
31
- parallel (1.20.1)
32
- parser (3.0.0.0)
33
- ast (~> 2.4.1)
34
- rainbow (3.0.0)
35
- rake (13.0.3)
36
- regexp_parser (2.0.3)
37
- rexml (3.2.4)
38
- rspec (3.10.0)
39
- rspec-core (~> 3.10.0)
40
- rspec-expectations (~> 3.10.0)
41
- rspec-mocks (~> 3.10.0)
42
- rspec-core (3.10.0)
43
- rspec-support (~> 3.10.0)
44
- rspec-expectations (3.10.0)
45
- diff-lcs (>= 1.2.0, < 2.0)
46
- rspec-support (~> 3.10.0)
47
- rspec-mocks (3.10.0)
48
- diff-lcs (>= 1.2.0, < 2.0)
49
- rspec-support (~> 3.10.0)
50
- rspec-support (3.10.0)
51
- rubocop (0.93.1)
52
- parallel (~> 1.10)
53
- parser (>= 2.7.1.5)
54
- rainbow (>= 2.2.2, < 4.0)
55
- regexp_parser (>= 1.8)
56
- rexml
57
- rubocop-ast (>= 0.6.0)
58
- ruby-progressbar (~> 1.7)
59
- unicode-display_width (>= 1.4.0, < 2.0)
60
- rubocop-ast (1.4.0)
61
- parser (>= 2.7.1.5)
62
- rubocop-performance (1.9.2)
63
- rubocop (>= 0.90.0, < 2.0)
64
- rubocop-ast (>= 0.4.0)
65
- rubocop-rspec (1.44.1)
66
- rubocop (~> 0.87)
67
- rubocop-ast (>= 0.7.1)
68
- ruby-progressbar (1.11.0)
69
- simplecov (0.21.2)
70
- docile (~> 1.1)
71
- simplecov-html (~> 0.11)
72
- simplecov_json_formatter (~> 0.1)
73
- simplecov-html (0.12.3)
74
- simplecov_json_formatter (0.1.3)
75
- sync (0.5.0)
76
- term-ansicolor (1.7.1)
77
- tins (~> 1.0)
78
- thor (1.1.0)
79
- thread_safe (0.3.6)
80
- tins (1.29.1)
81
- sync
82
- tzinfo (1.2.10)
83
- thread_safe (~> 0.1)
84
- unicode-display_width (1.7.0)
85
-
86
- PLATFORMS
87
- ruby
88
-
89
- DEPENDENCIES
90
- bright_serializer!
91
- bundler (~> 2)
92
- coveralls_reborn
93
- faker
94
- oj
95
- rake (~> 13.0)
96
- rspec (~> 3.0)
97
- rubocop
98
- rubocop-performance
99
- rubocop-rspec
100
-
101
- BUNDLED WITH
102
- 2.2.3
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- task default: :spec
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- lib = File.expand_path('lib', __dir__)
4
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
- require 'bright_serializer/version'
6
-
7
- Gem::Specification.new do |spec|
8
- spec.name = 'bright_serializer'
9
- spec.version = BrightSerializer::VERSION
10
- spec.authors = ['Jean-Francis Bastien']
11
- spec.email = ['jfbastien@petalmd.com']
12
-
13
- spec.summary = 'Light and fast Ruby serializer'
14
- spec.description = 'BrightSerializer is a minimalist implementation serializer for Ruby objects.'
15
- spec.homepage = 'https://github.com/petalmd/bright_serializer'
16
- spec.license = 'MIT'
17
- spec.required_ruby_version = '>= 2.6'
18
-
19
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
- # to allow pushing to a single host or delete this section to allow pushing to any host.
21
- if spec.respond_to?(:metadata)
22
- spec.metadata['homepage_uri'] = spec.homepage
23
- spec.metadata['source_code_uri'] = 'https://github.com/petalmd/bright_serializer'
24
- spec.metadata['changelog_uri'] = 'https://github.com/petalmd/bright_serializer/blob/master/CHANGELOG.md'
25
- else
26
- raise 'RubyGems 2.0 or newer is required to protect against ' \
27
- 'public gem pushes.'
28
- end
29
-
30
- # Specify which files should be added to the gem when it is released.
31
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
33
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|.github)/}) }
34
- end
35
- spec.bindir = 'exe'
36
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
- spec.require_paths = ['lib']
38
-
39
- spec.add_runtime_dependency 'oj', '~> 3'
40
- spec.add_dependency 'activesupport', '>= 5.2'
41
- spec.add_development_dependency 'bundler', '~> 2'
42
- spec.add_development_dependency 'faker', '~> 2'
43
- spec.add_development_dependency 'rake', '~> 13.0'
44
- spec.add_development_dependency 'rspec', '~> 3.0'
45
- end