bright_serializer 0.2.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: be9127288c21561c1c2b6e39d8e4e3620cbf5ffae9e416f6bc2716ad3db82aa5
4
+ data.tar.gz: 51e625f2f892ef31288821286fc029bba2bb3a5df26c7d8753481c58e4f3b47c
5
+ SHA512:
6
+ metadata.gz: 8ade52adac1afbc0f25a00364db7b8477bf69afeb0636874e2104dbc385a48e7b28a6a7e2e56ac3958794634edf12bbd00af6ff1ac390f384cbe59c6a05dc6d5
7
+ data.tar.gz: 8f4867b850363eb2022d231e78caf10827c0ada7f7972830984980bfb75cde62db9a4484945a2047c692fa3c429f57be3d6a73e9f05ffea8af390f3ad0ff6b9e
@@ -0,0 +1,23 @@
1
+ name: Build
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v1
12
+ - name: Set up Ruby 2.7
13
+ uses: actions/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.7
16
+ - name: Bundle install
17
+ run: |
18
+ gem install bundler
19
+ bundle install --jobs 4 --retry 3
20
+ - name: Specs
21
+ run: bundle exec rake
22
+ - name: Rubocop
23
+ run: bundle exec rubocop --parallel
@@ -0,0 +1,14 @@
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 ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,33 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+
5
+ Metrics/BlockLength:
6
+ Enabled: false
7
+
8
+ Layout/LineLength:
9
+ Max: 120
10
+
11
+ Style/Documentation:
12
+ Enabled: false
13
+
14
+ RSpec/ExampleLength:
15
+ Enabled: false
16
+
17
+ RSpec/NamedSubject:
18
+ Enabled: false
19
+
20
+ Gemspec/RequiredRubyVersion:
21
+ Enabled: false
22
+
23
+ Metrics/PerceivedComplexity:
24
+ Enabled: false
25
+
26
+ Metrics/MethodLength:
27
+ Enabled: false
28
+
29
+ Metrics/CyclomaticComplexity:
30
+ Enabled: false
31
+
32
+ Metrics/AbcSize:
33
+ Enabled: false
@@ -0,0 +1 @@
1
+ 2.7.1
@@ -0,0 +1,24 @@
1
+ # Change log
2
+
3
+ ## master (unreleased)
4
+
5
+ * Your contribution
6
+
7
+ ## 0.2.1 (2020-07-21)
8
+
9
+ * Handle set_key_transform inherited from a parent serializer (#10)
10
+
11
+ ## 0.2.0 (2020-07-17)
12
+
13
+ * Add RubyGems version badge
14
+ * Handle inherit from a parent serializer
15
+ * Define entity in serializer for grape_swagger (#9)
16
+
17
+ ## 0.1.1 (2020-07-13)
18
+
19
+ * Add description in gemspec file
20
+ * Add content in CHANGELOG.md
21
+
22
+ ## 0.1.0 (2020-07-13)
23
+
24
+ * First release
data/Gemfile ADDED
@@ -0,0 +1,17 @@
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 'faker'
14
+ gem 'rubocop'
15
+ gem 'rubocop-performance'
16
+ gem 'rubocop-rspec'
17
+ end
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bright_serializer (0.2.1)
5
+ oj (~> 3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.0)
11
+ concurrent-ruby (1.1.5)
12
+ diff-lcs (1.4.4)
13
+ faker (2.9.0)
14
+ i18n (>= 1.6, < 1.8)
15
+ i18n (1.7.0)
16
+ concurrent-ruby (~> 1.0)
17
+ jaro_winkler (1.5.4)
18
+ oj (3.10.0)
19
+ parallel (1.19.1)
20
+ parser (2.7.0.0)
21
+ ast (~> 2.4.0)
22
+ rainbow (3.0.0)
23
+ rake (13.0.1)
24
+ rspec (3.9.0)
25
+ rspec-core (~> 3.9.0)
26
+ rspec-expectations (~> 3.9.0)
27
+ rspec-mocks (~> 3.9.0)
28
+ rspec-core (3.9.2)
29
+ rspec-support (~> 3.9.3)
30
+ rspec-expectations (3.9.2)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.9.0)
33
+ rspec-mocks (3.9.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.9.0)
36
+ rspec-support (3.9.3)
37
+ rubocop (0.78.0)
38
+ jaro_winkler (~> 1.5.1)
39
+ parallel (~> 1.10)
40
+ parser (>= 2.6)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ ruby-progressbar (~> 1.7)
43
+ unicode-display_width (>= 1.4.0, < 1.7)
44
+ rubocop-performance (1.5.2)
45
+ rubocop (>= 0.71.0)
46
+ rubocop-rspec (1.37.1)
47
+ rubocop (>= 0.68.1)
48
+ ruby-progressbar (1.10.1)
49
+ unicode-display_width (1.6.0)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ bright_serializer!
56
+ bundler (~> 2)
57
+ faker
58
+ oj
59
+ rake (~> 13.0)
60
+ rspec (~> 3.0)
61
+ rubocop
62
+ rubocop-performance
63
+ rubocop-rspec
64
+
65
+ BUNDLED WITH
66
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Jean-Francis Bastien
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,165 @@
1
+ [![Actions Status](https://github.com/petalmd/bright_serializer/workflows/Build/badge.svg)](https://github.com/petalmd/bright_serializer/actions?query=workflow%3ABuild)
2
+ [![Gem Version](https://badge.fury.io/rb/bright_serializer.svg)](https://badge.fury.io/rb/bright_serializer)
3
+
4
+ # BrightSerializer
5
+
6
+ This is a very light and fast gem to serialize object in a Ruby project.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'bright_serializer'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install bright_serializer
23
+
24
+ ## Usage and features
25
+
26
+ ### Basic
27
+
28
+ Create a class and include `BrightSerializer::Serializer`
29
+
30
+ ```ruby
31
+ class AccountSerializer
32
+ include BrightSerializer::Serializer
33
+ attributes :id, :first_name, :last_name
34
+
35
+ # With a block
36
+ attribute :name do |object|
37
+ "#{object.first_name} #{object.last_name}"
38
+ end
39
+
40
+ # With a block shorter
41
+ attribute :created_at, &:to_s
42
+ end
43
+
44
+ AccountSerializer.new(Account.first).to_h
45
+ AccountSerializer.new(Account.first).to_json
46
+ ```
47
+
48
+ ### Params
49
+
50
+ You can pass params to your serializer. For example to have more context with the authenticated user.
51
+
52
+ ```ruby
53
+ class AccountSerializer
54
+ include BrightSerializer::Serializer
55
+ attributes :id, :first_name, :last_name
56
+
57
+ attribute :friend do |object, params|
58
+ object.is_friend_with? params[:current_user]
59
+ end
60
+ end
61
+
62
+ current_user = Account.find(authenticated_account_id)
63
+ AccountSerializer.new(Account.first, params: { current_user: current_user }).to_json
64
+ ```
65
+
66
+ ### Conditional Attributes
67
+
68
+ Attribute can be remove from serialization by passing a `proc` to the option `if`. If the proc return `true` the attibute
69
+ will be serialize. `object` and `params` or accessible.
70
+
71
+ ```ruby
72
+ class AccountSerializer
73
+ include BrightSerializer::Serializer
74
+ attributes :id, :first_name, :last_name
75
+
76
+ attribute :email, if: -> { |object, params| params[:current_user].is_admin? }
77
+ end
78
+ ```
79
+
80
+ ### Transform keys
81
+
82
+ By default, keys or not transformed.
83
+
84
+ ```ruby
85
+ class AccountSerializer
86
+ include BrightSerializer::Serializer
87
+ set_key_transform :underscore
88
+ end
89
+
90
+ set_key_transform :underscore # "first_name" => "first_name"
91
+ set_key_transform :camel # "first_name" => "FirstName"
92
+ set_key_transform :camel_lower # "first_name" => "firstName"
93
+ set_key_transform :dash # "first_name" => "first-name"
94
+ ```
95
+
96
+ ### Instance serializer fieldsets
97
+
98
+ ```ruby
99
+ class AccountSerializer
100
+ include BrightSerializer::Serializer
101
+ attributes :id, :first_name, :last_name
102
+ end
103
+
104
+ # Only serialize first_name and last_name
105
+ AccountSerializer.new(Account.first, fields: [:first_name, :last_name]).to_json
106
+ ```
107
+
108
+ ### Relations
109
+
110
+ For now, relations or declared like any other attribute.
111
+
112
+ ```ruby
113
+ class FriendSerializer
114
+ include BrightSerializer::Serializer
115
+ attributes :id, :first_name, :last_name
116
+ end
117
+
118
+ class AccountSerializer
119
+ include BrightSerializer::Serializer
120
+ attributes :id, :first_name, :last_name
121
+
122
+ attribute :friends do |object|
123
+ FriendSerializer.new(object.friends)
124
+ end
125
+ end
126
+ ```
127
+
128
+ ### Entity
129
+
130
+ You can define the entity of your serializer to generate documentation with the option `entity`.
131
+ The feature was build to work with [grape-swagger](https://github.com/ruby-grape/grape-swagger).
132
+ For more information about defining a model entity see the [Swagger documentation](https://swagger.io/specification/v2/?sbsearch=array%20response#schema-object).
133
+
134
+ ```ruby
135
+ class AccountSerializer
136
+ include BrightSerializer::Serializer
137
+ attribute :id, entity: { type: :string, description: 'The id of the account' }
138
+ attribute :name
139
+
140
+ attribute :friends,
141
+ entity: {
142
+ type: :array, items: { ref: 'FriendSerializer' }, description: 'The list the account friends.'
143
+ } do |object|
144
+ FriendSerializer.new(object.friends)
145
+ end
146
+ end
147
+ ```
148
+
149
+ ## Development
150
+
151
+ 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.
152
+
153
+ To install this gem onto your local machine, run `bundle exec rake install`.
154
+
155
+ ## New release
156
+
157
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
158
+
159
+ ## Contributing
160
+
161
+ Bug reports and pull requests are welcome on GitHub at https://github.com/petalmd/bright_serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
162
+
163
+ ## License
164
+
165
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,44 @@
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.5'
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)/}) }
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 'bundler', '~> 2'
41
+ spec.add_development_dependency 'faker', '~> 2'
42
+ spec.add_development_dependency 'rake', '~> 13.0'
43
+ spec.add_development_dependency 'rspec', '~> 3.0'
44
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bright_serializer/version'
4
+
5
+ module BrightSerializer
6
+ require_relative 'bright_serializer/serializer'
7
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'entity/base'
4
+
5
+ module BrightSerializer
6
+ class Attribute
7
+ attr_reader :key, :block, :condition, :entity
8
+ attr_accessor :transformed_key
9
+
10
+ def initialize(key, condition, entity, &block)
11
+ @key = key
12
+ @condition = condition
13
+ @block = block
14
+ @entity = entity ? Entity::Base.new(entity) : nil
15
+ end
16
+
17
+ def serialize(object, params)
18
+ return unless object
19
+
20
+ value =
21
+ if @block
22
+ @block.arity.abs == 1 ? object.instance_eval(&@block) : object.instance_exec(object, params, &@block)
23
+ elsif object.is_a?(Hash)
24
+ object[key]
25
+ else
26
+ object.send(key)
27
+ end
28
+
29
+ value.respond_to?(:serializable_hash) ? value.serializable_hash : value
30
+ end
31
+
32
+ def condition?(object, params)
33
+ return true unless @condition
34
+
35
+ @condition.call(object, params)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'parser'
4
+
5
+ module BrightSerializer
6
+ module Entity
7
+ class Base
8
+ DEFAULT_DEFINITION = { type: :undefined }.freeze
9
+
10
+ # https://swagger.io/specification/v2/?sbsearch=array%20response#schema-object
11
+
12
+ def initialize(definition)
13
+ @definition = definition
14
+ end
15
+
16
+ def to_h
17
+ @definition.transform_keys! { |k| Inflector.camel_lower k.to_s }
18
+ parse_ref!
19
+ @definition
20
+ end
21
+
22
+ def parse_ref!
23
+ object = nested_hash(@definition, 'ref')
24
+ return unless object
25
+
26
+ ref_entity_name = Inflector.constantize(object.delete('ref')).entity_name
27
+ relation = "#/definitions/#{ref_entity_name}"
28
+ object['$ref'] = relation
29
+ end
30
+
31
+ def nested_hash(obj, key)
32
+ if obj.respond_to?(:key?) && obj.key?(key)
33
+ obj
34
+ elsif obj.respond_to?(:each)
35
+ r = nil
36
+ obj.find { |*a| r = nested_hash(a.last, key) }
37
+ r
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrightSerializer
4
+ module Entity
5
+ class Parser
6
+ attr_reader :model
7
+ attr_reader :endpoint
8
+
9
+ def initialize(model, endpoint)
10
+ @model = model
11
+ @endpoint = endpoint
12
+ end
13
+
14
+ def call
15
+ @model.entity
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Inflector
4
+ CAMEL_REGEX = /(_[a-zA-Z])/.freeze
5
+ UNDERSCORE_REGEX = /(.)([A-Z])/.freeze
6
+
7
+ class << self
8
+ def camel(term)
9
+ return term.capitalize! unless term.include?('_')
10
+
11
+ term.capitalize!
12
+ term.gsub!(CAMEL_REGEX) { Regexp.last_match(1).delete('_').upcase! }
13
+ end
14
+
15
+ def camel_lower(term)
16
+ return term unless term.include?('_')
17
+
18
+ term.gsub!(CAMEL_REGEX) { Regexp.last_match(1).delete('_').upcase! }
19
+ end
20
+
21
+ def underscore(camel_cased_word)
22
+ camel_cased_word.gsub!(UNDERSCORE_REGEX, '\1_\2')
23
+ camel_cased_word.downcase!
24
+ camel_cased_word
25
+ end
26
+
27
+ def dash(underscored_word)
28
+ underscored_word.tr!('_', '-')
29
+ underscored_word
30
+ end
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
+ end
63
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'oj'
4
+ require 'set'
5
+ require_relative 'attribute'
6
+ require_relative 'inflector'
7
+ require_relative 'entity/base'
8
+
9
+ module BrightSerializer
10
+ module Serializer
11
+ SUPPORTED_TRANSFORMATION = %i[camel camel_lower dash underscore].freeze
12
+ DEFAULT_OJ_OPTIONS = { mode: :compat, time_format: :ruby, use_to_json: true }.freeze
13
+
14
+ def self.included(base)
15
+ base.extend ClassMethods
16
+ base.instance_variable_set(:@attributes_to_serialize, [])
17
+ end
18
+
19
+ def initialize(object, **options)
20
+ @object = object
21
+ @params = options.delete(:params)
22
+ @fields = Set.new(options.delete(:fields))
23
+ end
24
+
25
+ def serialize(object)
26
+ self.class.attributes_to_serialize.each_with_object({}) do |attribute, result|
27
+ next if @fields.any? && !@fields.include?(attribute.key)
28
+ next unless attribute.condition?(object, @params)
29
+
30
+ result[attribute.transformed_key] = attribute.serialize(object, @params)
31
+ end
32
+ end
33
+
34
+ def serializable_hash
35
+ if @object.respond_to?(:each) && !@object.respond_to?(:each_pair)
36
+ @object.map { |o| serialize o }
37
+ else
38
+ serialize(@object)
39
+ end
40
+ end
41
+
42
+ alias to_hash serializable_hash
43
+
44
+ def serializable_json(*_args)
45
+ ::Oj.dump(to_hash, DEFAULT_OJ_OPTIONS)
46
+ end
47
+
48
+ alias to_json serializable_json
49
+
50
+ module ClassMethods
51
+ attr_reader :attributes_to_serialize, :transform_method
52
+
53
+ def inherited(subclass)
54
+ super
55
+ subclass.instance_variable_set(:@attributes_to_serialize, []) unless subclass.attributes_to_serialize
56
+ subclass.attributes_to_serialize.concat(@attributes_to_serialize)
57
+ subclass.instance_variable_set(:@transform_method, @transform_method) unless subclass.transform_method
58
+ end
59
+
60
+ def attributes(*attributes, **options, &block)
61
+ attributes.each do |key|
62
+ attribute = Attribute.new(key, options[:if], options[:entity], &block)
63
+ attribute.transformed_key = run_transform_key(key)
64
+ @attributes_to_serialize << attribute
65
+ end
66
+ end
67
+
68
+ alias attribute attributes
69
+
70
+ def set_key_transform(transform_name) # rubocop:disable Naming/AccessorMethodName
71
+ unless SUPPORTED_TRANSFORMATION.include?(transform_name)
72
+ raise ArgumentError "Invalid transformation: #{SUPPORTED_TRANSFORMATION}"
73
+ end
74
+
75
+ @transform_method = transform_name
76
+ end
77
+
78
+ def run_transform_key(input)
79
+ if transform_method
80
+ Inflector.send(@transform_method, input.to_s).to_sym
81
+ else
82
+ input.to_sym
83
+ end
84
+ end
85
+
86
+ def entity
87
+ {}.tap do |result|
88
+ @attributes_to_serialize.each do |attribute|
89
+ entity_value = attribute.entity&.to_h || BrightSerializer::Entity::Base::DEFAULT_DEFINITION
90
+ result.merge!(attribute.transformed_key => entity_value)
91
+ end
92
+ end
93
+ end
94
+
95
+ def entity_name
96
+ name.split('::').last.downcase
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrightSerializer
4
+ VERSION = '0.2.1'
5
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bright_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Jean-Francis Bastien
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-21 00:00:00.000000000 Z
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
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faker
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: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: BrightSerializer is a minimalist implementation serializer for Ruby objects.
84
+ email:
85
+ - jfbastien@petalmd.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".github/workflows/build.yml"
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".rubocop.yml"
94
+ - ".ruby-version"
95
+ - CHANGELOG.md
96
+ - Gemfile
97
+ - Gemfile.lock
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - bright_serializer.gemspec
102
+ - lib/bright_serializer.rb
103
+ - lib/bright_serializer/attribute.rb
104
+ - lib/bright_serializer/entity/base.rb
105
+ - lib/bright_serializer/entity/parser.rb
106
+ - lib/bright_serializer/inflector.rb
107
+ - lib/bright_serializer/serializer.rb
108
+ - lib/bright_serializer/version.rb
109
+ homepage: https://github.com/petalmd/bright_serializer
110
+ licenses:
111
+ - MIT
112
+ metadata:
113
+ homepage_uri: https://github.com/petalmd/bright_serializer
114
+ source_code_uri: https://github.com/petalmd/bright_serializer
115
+ changelog_uri: https://github.com/petalmd/bright_serializer/blob/master/CHANGELOG.md
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '2.5'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubygems_version: 3.1.2
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Light and fast Ruby serializer
135
+ test_files: []