bright_serializer 0.4.0 → 0.5.1

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: 948f2e438a96ccd17416ace3e91375fae18b749795937f6db2494e3f3358ab8a
4
- data.tar.gz: 6541ab4c808bb8a872fcc02dfb8dc5747bda9a6344dc50594ad0dec5222ed676
3
+ metadata.gz: 6b1e6a74356becfd673dd4dac712c8928aeea165d28c2a8f32ae95721b2e3a8c
4
+ data.tar.gz: 555e6f814e3ae8f2a7bd2754f6084bfa5a52bae88f129928afdad4e20beed8f0
5
5
  SHA512:
6
- metadata.gz: 90cb6ed0a7eb05ca181a52a4597693382b0976ecf5753f856fa0b81eadb71be3b4ac7e7da3a41df7a18b1ef884b42007dae9c0daf6711d73686ad6c5c230b962
7
- data.tar.gz: bf93c9d4b769e9f754b238d1efb297560647a1899679d2180af8ad16d0d554c80a124c056b9659481ecae6e326facf1922ccedff2dd5d7f5dd7d191c3828c8ed
6
+ metadata.gz: d7b819bcdef793f0e4cb07d575ec7a23632ae11f51352db4d561cf9e705345eca0f77086bc5f9962e660368bb512b5d982040b4b89dfcfc99560abcc1d5b5140
7
+ data.tar.gz: a94b5dabd9b6ad3a7b5ebe47f3ef50247f02cd21c42de603e25372969268d60be00572943f740e780c97129673bc7189d3f7f7d9226390a89e693f055f51e447
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
@@ -7,7 +7,7 @@ module BrightSerializer
7
7
  end
8
8
 
9
9
  def self.instrumentation_extension(base)
10
- return unless defined? ActiveSupport
10
+ return unless defined? ActiveSupport::Notifications
11
11
 
12
12
  require_relative 'extensions/instrumentation'
13
13
  base.prepend Instrumentation
@@ -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
@@ -27,6 +27,8 @@ module BrightSerializer
27
27
  end
28
28
 
29
29
  def serialize(object, attributes_to_serialize)
30
+ return nil if @object.nil?
31
+
30
32
  attributes_to_serialize.each_with_object({}) do |attribute, result|
31
33
  next unless attribute.condition?(object, @params)
32
34
 
@@ -60,8 +62,9 @@ module BrightSerializer
60
62
  subclass.instance_variable_set(:@transform_method, @transform_method) unless subclass.transform_method
61
63
  end
62
64
 
63
- def attributes(*attributes, **options, &block)
64
- attributes.each do |key|
65
+ def attributes(*args, &block)
66
+ options = args.extract_options!
67
+ args.each do |key|
65
68
  attribute = Attribute.new(key, options[:if], options[:entity], &block)
66
69
  attribute.transformed_key = run_transform_key(key)
67
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.0'
4
+ VERSION = '0.5.1'
5
5
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'bright_serializer/version'
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'active_support/core_ext/array/extract_options'
5
+ require_relative 'bright_serializer/version'
4
6
 
5
7
  module BrightSerializer
6
8
  require_relative 'bright_serializer/serializer'
metadata CHANGED
@@ -1,93 +1,37 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bright_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean-Francis Bastien
8
- autorequire:
9
- bindir: exe
8
+ autorequire:
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-10 00:00:00.000000000 Z
11
+ date: 2023-04-05 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
30
16
  requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '5.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '5.0'
41
- - !ruby/object:Gem::Dependency
42
- name: bundler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
17
+ - - ">="
46
18
  - !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
19
+ version: '5.2'
20
+ type: :runtime
77
21
  prerelease: false
78
22
  version_requirements: !ruby/object:Gem::Requirement
79
23
  requirements:
80
- - - "~>"
24
+ - - ">="
81
25
  - !ruby/object:Gem::Version
82
- version: '13.0'
26
+ version: '5.2'
83
27
  - !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,9 +64,11 @@ 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
132
- post_install_message:
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'
71
+ post_install_message:
133
72
  rdoc_options: []
134
73
  require_paths:
135
74
  - lib
@@ -144,8 +83,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
83
  - !ruby/object:Gem::Version
145
84
  version: '0'
146
85
  requirements: []
147
- rubygems_version: 3.0.3
148
- signing_key:
86
+ rubygems_version: 3.4.1
87
+ signing_key:
149
88
  specification_version: 4
150
89
  summary: Light and fast Ruby serializer
151
90
  test_files: []
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,59 +0,0 @@
1
- # Change log
2
-
3
- ## master (unreleased)
4
-
5
- ## 0.4.0 (2022-11-10)
6
-
7
- * Added relation helper methods `has_one`, `has_many`, `belongs_to` ([#49](https://github.com/petalmd/bright_serializer/pull/49))
8
- * Performance improvements, save in instance attributes to serialize. ([#100](https://github.com/petalmd/bright_serializer/pull/100))
9
- * Performance improvements, calculate attributes to serialize only once. ([#98](https://github.com/petalmd/bright_serializer/pull/98))
10
- * Add instrumentation. ([#90](https://github.com/petalmd/bright_serializer/pull/90))
11
-
12
- ## 0.3.1 (2022-09-28)
13
-
14
- * Performance improvements, use nil instead of empty set. ([#97](https://github.com/petalmd/bright_serializer/pull/97))
15
- * Move specs out of lib. ([#96](https://github.com/petalmd/bright_serializer/pull/96))
16
-
17
- ## 0.3.0 (2022-05-26)
18
-
19
- * Allow to evaluate entity values with a callable lambda. ([#88](https://github.com/petalmd/bright_serializer/pull/88))
20
- * Fix `FrozenError (can't modify frozen Array)` when parsing entity. ([#83](https://github.com/petalmd/bright_serializer/pull/83))
21
- * Added the support to use instance methods from a serializer class in the library ([#85](https://github.com/petalmd/bright_serializer/pull/85))
22
- * Use real coveralls_reborn gem
23
-
24
- ## 0.2.5 (2021-03-08)
25
-
26
- * When serializing an Hash, check present of the key before trying string ([#57](https://github.com/petalmd/bright_serializer/pull/57))
27
-
28
- ## 0.2.4 (2021-02-19)
29
-
30
- * Try symbol and string keys when the object to serialize is an Hash ([#54](https://github.com/petalmd/bright_serializer/pull/54))
31
-
32
- ## 0.2.3 (2021-01-04)
33
-
34
- * Update dependencies ([v0.2.2...v0.2.3](https://github.com/petalmd/bright_serializer/compare/v0.2.2...v0.2.3))
35
-
36
- ## 0.2.2 (2020-07-22)
37
-
38
- * Run CI build on all supported Ruby versions ([#11](https://github.com/petalmd/bright_serializer/pull/11))
39
- * Update Rubocop 0.78.0 => 0.88.0 and run auto-correction
40
- * Deep transform entity keys ([#12](https://github.com/petalmd/bright_serializer/pull/12))
41
-
42
- ## 0.2.1 (2020-07-21)
43
-
44
- * Handle set_key_transform inherited from a parent serializer ([#10](https://github.com/petalmd/bright_serializer/pull/10))
45
-
46
- ## 0.2.0 (2020-07-17)
47
-
48
- * Add RubyGems version badge
49
- * Handle inherit from a parent serializer
50
- * Define entity in serializer for grape_swagger ([#9](https://github.com/petalmd/bright_serializer/pull/9))
51
-
52
- ## 0.1.1 (2020-07-13)
53
-
54
- * Add description in gemspec file
55
- * Add content in CHANGELOG.md
56
-
57
- ## 0.1.0 (2020-07-13)
58
-
59
- * 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.0)
5
- oj (~> 3)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activesupport (5.2.8.1)
11
- concurrent-ruby (~> 1.0, >= 1.0.2)
12
- i18n (>= 0.7, < 2)
13
- minitest (~> 5.1)
14
- tzinfo (~> 1.1)
15
- ast (2.4.1)
16
- concurrent-ruby (1.1.10)
17
- coveralls_reborn (0.22.0)
18
- simplecov (>= 0.18.1, < 0.22.0)
19
- term-ansicolor (~> 1.6)
20
- thor (>= 0.20.3, < 2.0)
21
- tins (~> 1.16)
22
- diff-lcs (1.4.4)
23
- docile (1.4.0)
24
- faker (2.15.1)
25
- i18n (>= 1.6, < 2)
26
- i18n (1.12.0)
27
- concurrent-ruby (~> 1.0)
28
- minitest (5.16.3)
29
- oj (3.11.1)
30
- parallel (1.20.1)
31
- parser (3.0.0.0)
32
- ast (~> 2.4.1)
33
- rainbow (3.0.0)
34
- rake (13.0.3)
35
- regexp_parser (2.0.3)
36
- rexml (3.2.4)
37
- rspec (3.10.0)
38
- rspec-core (~> 3.10.0)
39
- rspec-expectations (~> 3.10.0)
40
- rspec-mocks (~> 3.10.0)
41
- rspec-core (3.10.0)
42
- rspec-support (~> 3.10.0)
43
- rspec-expectations (3.10.0)
44
- diff-lcs (>= 1.2.0, < 2.0)
45
- rspec-support (~> 3.10.0)
46
- rspec-mocks (3.10.0)
47
- diff-lcs (>= 1.2.0, < 2.0)
48
- rspec-support (~> 3.10.0)
49
- rspec-support (3.10.0)
50
- rubocop (0.93.1)
51
- parallel (~> 1.10)
52
- parser (>= 2.7.1.5)
53
- rainbow (>= 2.2.2, < 4.0)
54
- regexp_parser (>= 1.8)
55
- rexml
56
- rubocop-ast (>= 0.6.0)
57
- ruby-progressbar (~> 1.7)
58
- unicode-display_width (>= 1.4.0, < 2.0)
59
- rubocop-ast (1.4.0)
60
- parser (>= 2.7.1.5)
61
- rubocop-performance (1.9.2)
62
- rubocop (>= 0.90.0, < 2.0)
63
- rubocop-ast (>= 0.4.0)
64
- rubocop-rspec (1.44.1)
65
- rubocop (~> 0.87)
66
- rubocop-ast (>= 0.7.1)
67
- ruby-progressbar (1.11.0)
68
- simplecov (0.21.2)
69
- docile (~> 1.1)
70
- simplecov-html (~> 0.11)
71
- simplecov_json_formatter (~> 0.1)
72
- simplecov-html (0.12.3)
73
- simplecov_json_formatter (0.1.3)
74
- sync (0.5.0)
75
- term-ansicolor (1.7.1)
76
- tins (~> 1.0)
77
- thor (1.1.0)
78
- thread_safe (0.3.6)
79
- tins (1.29.1)
80
- sync
81
- tzinfo (1.2.10)
82
- thread_safe (~> 0.1)
83
- unicode-display_width (1.7.0)
84
-
85
- PLATFORMS
86
- ruby
87
-
88
- DEPENDENCIES
89
- activesupport (~> 5.0)
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_development_dependency 'activesupport', '~> 5.0'
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