bright_serializer 0.1.0 → 0.2.3

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: 411805a8af30f74b73f325ddbd820914d524a3134d1d5d0b9346eb738e9eda59
4
- data.tar.gz: 13f44da90680dc3e4058c9888c3711d7c4adb0933ff827757d3aafd95696fbc1
3
+ metadata.gz: 77aa6c33f823a9f96b4d8a73fac9f7a87cf7892431d6e0f3cd1186b9da9fdf6b
4
+ data.tar.gz: b9820f01f3cf1d5d86588741b0f7af93521c4b55c5dc7352940b3cb1bebdc3e7
5
5
  SHA512:
6
- metadata.gz: d962a5e41755e14504f97a61b32530e7e5e5236e497b884a6fdf23e599b07ad41ab62acb0e9487b7b0fa39882bbe65dbd6df0f99c74a4a15dc10800095fab422
7
- data.tar.gz: 9ed35fa977a777408c84a93ff4e917c0a1f12cadd785b5d88906df8d691a3801138c9144c173ce375e5140bdfe785272149839f7f5358cc843eb0b1459c5223c
6
+ metadata.gz: 2093dc003212c566476fb83ebb95cc0e9951e5652d0e4a0e1ee667a110581c09cf48ea6170948d3636792f70fa7c10be8acc5f1ff068d567c51fee81fa87235b
7
+ data.tar.gz: f1163513f3b3d2085f13165fd9f9f29712a9f2cab7486c8041daa7fb04d6719dc102df7fea428451c14eb7ff8677c7bde0c62f8ba4d64f0cef0262e7604387bd
@@ -2,6 +2,9 @@ require:
2
2
  - rubocop-performance
3
3
  - rubocop-rspec
4
4
 
5
+ AllCops:
6
+ NewCops: enable
7
+
5
8
  Metrics/BlockLength:
6
9
  Enabled: false
7
10
 
@@ -0,0 +1,34 @@
1
+ # Change log
2
+
3
+ ## master (unreleased)
4
+
5
+ * Your contribution
6
+
7
+ ## 0.2.3 (2021-01-04)
8
+
9
+ * Update dependencies ([v0.2.2...v0.2.3](https://github.com/petalmd/bright_serializer/compare/v0.2.2...v0.2.3))
10
+
11
+ ## 0.2.2 (2020-07-22)
12
+
13
+ * Run CI build on all supported Ruby versions ([#11](https://github.com/petalmd/bright_serializer/pull/11))
14
+ * Update Rubocop 0.78.0 => 0.88.0 and run auto-correction
15
+ * Deep transform entity keys ([#12](https://github.com/petalmd/bright_serializer/pull/12))
16
+
17
+ ## 0.2.1 (2020-07-21)
18
+
19
+ * Handle set_key_transform inherited from a parent serializer ([#10](https://github.com/petalmd/bright_serializer/pull/10))
20
+
21
+ ## 0.2.0 (2020-07-17)
22
+
23
+ * Add RubyGems version badge
24
+ * Handle inherit from a parent serializer
25
+ * Define entity in serializer for grape_swagger ([#9](https://github.com/petalmd/bright_serializer/pull/9))
26
+
27
+ ## 0.1.1 (2020-07-13)
28
+
29
+ * Add description in gemspec file
30
+ * Add content in CHANGELOG.md
31
+
32
+ ## 0.1.0 (2020-07-13)
33
+
34
+ * First release
@@ -1,52 +1,59 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bright_serializer (0.1.0)
4
+ bright_serializer (0.2.3)
5
5
  oj (~> 3)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- ast (2.4.0)
11
- concurrent-ruby (1.1.5)
10
+ ast (2.4.1)
11
+ concurrent-ruby (1.1.7)
12
12
  diff-lcs (1.4.4)
13
- faker (2.9.0)
14
- i18n (>= 1.6, < 1.8)
15
- i18n (1.7.0)
13
+ faker (2.15.1)
14
+ i18n (>= 1.6, < 2)
15
+ i18n (1.8.5)
16
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)
17
+ oj (3.10.18)
18
+ parallel (1.20.1)
19
+ parser (3.0.0.0)
20
+ ast (~> 2.4.1)
22
21
  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)
22
+ rake (13.0.3)
23
+ regexp_parser (2.0.3)
24
+ rexml (3.2.4)
25
+ rspec (3.10.0)
26
+ rspec-core (~> 3.10.0)
27
+ rspec-expectations (~> 3.10.0)
28
+ rspec-mocks (~> 3.10.0)
29
+ rspec-core (3.10.0)
30
+ rspec-support (~> 3.10.0)
31
+ rspec-expectations (3.10.0)
31
32
  diff-lcs (>= 1.2.0, < 2.0)
32
- rspec-support (~> 3.9.0)
33
- rspec-mocks (3.9.1)
33
+ rspec-support (~> 3.10.0)
34
+ rspec-mocks (3.10.0)
34
35
  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)
36
+ rspec-support (~> 3.10.0)
37
+ rspec-support (3.10.0)
38
+ rubocop (0.93.1)
39
39
  parallel (~> 1.10)
40
- parser (>= 2.6)
40
+ parser (>= 2.7.1.5)
41
41
  rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 1.8)
43
+ rexml
44
+ rubocop-ast (>= 0.6.0)
42
45
  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)
46
+ unicode-display_width (>= 1.4.0, < 2.0)
47
+ rubocop-ast (1.4.0)
48
+ parser (>= 2.7.1.5)
49
+ rubocop-performance (1.9.2)
50
+ rubocop (>= 0.90.0, < 2.0)
51
+ rubocop-ast (>= 0.4.0)
52
+ rubocop-rspec (1.44.1)
53
+ rubocop (~> 0.87)
54
+ rubocop-ast (>= 0.7.1)
55
+ ruby-progressbar (1.11.0)
56
+ unicode-display_width (1.7.0)
50
57
 
51
58
  PLATFORMS
52
59
  ruby
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
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)
2
3
 
3
4
  # BrightSerializer
4
5
 
@@ -124,6 +125,27 @@ class AccountSerializer
124
125
  end
125
126
  ```
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
+
127
149
  ## Development
128
150
 
129
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.
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ['Jean-Francis Bastien']
11
11
  spec.email = ['jfbastien@petalmd.com']
12
12
 
13
- spec.summary = 'T O D O: Write a short summary, because RubyGems requires one.'
14
- spec.description = 'T O D O: Write a longer description or delete this line.'
13
+ spec.summary = 'Light and fast Ruby serializer'
14
+ spec.description = 'BrightSerializer is a minimalist implementation serializer for Ruby objects.'
15
15
  spec.homepage = 'https://github.com/petalmd/bright_serializer'
16
16
  spec.license = 'MIT'
17
17
  spec.required_ruby_version = '>= 2.5'
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
30
30
  # Specify which files should be added to the gem when it is released.
31
31
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
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)/}) }
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|.github)/}) }
34
34
  end
35
35
  spec.bindir = 'exe'
36
36
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
@@ -1,14 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'entity/base'
4
+
3
5
  module BrightSerializer
4
6
  class Attribute
5
- attr_reader :key, :block, :condition
7
+ attr_reader :key, :block, :condition, :entity
6
8
  attr_accessor :transformed_key
7
9
 
8
- def initialize(key, condition, &block)
10
+ def initialize(key, condition, entity, &block)
9
11
  @key = key
10
12
  @condition = condition
11
13
  @block = block
14
+ @entity = entity ? Entity::Base.new(entity) : nil
12
15
  end
13
16
 
14
17
  def serialize(object, params)
@@ -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
+ Inflector.deep_transform_keys_in_object!(@definition) { |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,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrightSerializer
4
+ module Entity
5
+ class Parser
6
+ attr_reader :model, :endpoint
7
+
8
+ def initialize(model, endpoint)
9
+ @model = model
10
+ @endpoint = endpoint
11
+ end
12
+
13
+ def call
14
+ @model.entity
15
+ end
16
+ end
17
+ end
18
+ end
@@ -28,5 +28,52 @@ class Inflector
28
28
  underscored_word.tr!('_', '-')
29
29
  underscored_word
30
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
+
63
+ # File active_support/core_ext/hash/keys.rb, line 156
64
+ def deep_transform_keys_in_object!(object, &block)
65
+ case object
66
+ when Hash
67
+ object.keys.each do |key| # rubocop:disable Style/HashEachMethods
68
+ value = object.delete(key)
69
+ object[yield(key)] = deep_transform_keys_in_object!(value, &block)
70
+ end
71
+ object
72
+ when Array
73
+ object.map! { |e| deep_transform_keys_in_object!(e, &block) }
74
+ else
75
+ object
76
+ end
77
+ end
31
78
  end
32
79
  end
@@ -4,6 +4,7 @@ require 'oj'
4
4
  require 'set'
5
5
  require_relative 'attribute'
6
6
  require_relative 'inflector'
7
+ require_relative 'entity/base'
7
8
 
8
9
  module BrightSerializer
9
10
  module Serializer
@@ -11,6 +12,7 @@ module BrightSerializer
11
12
  DEFAULT_OJ_OPTIONS = { mode: :compat, time_format: :ruby, use_to_json: true }.freeze
12
13
 
13
14
  def self.included(base)
15
+ super
14
16
  base.extend ClassMethods
15
17
  base.instance_variable_set(:@attributes_to_serialize, [])
16
18
  end
@@ -31,7 +33,7 @@ module BrightSerializer
31
33
  end
32
34
 
33
35
  def serializable_hash
34
- if @object.respond_to?(:size) && !@object.respond_to?(:each_pair)
36
+ if @object.respond_to?(:each) && !@object.respond_to?(:each_pair)
35
37
  @object.map { |o| serialize o }
36
38
  else
37
39
  serialize(@object)
@@ -49,9 +51,16 @@ module BrightSerializer
49
51
  module ClassMethods
50
52
  attr_reader :attributes_to_serialize, :transform_method
51
53
 
54
+ def inherited(subclass)
55
+ super
56
+ subclass.instance_variable_set(:@attributes_to_serialize, []) unless subclass.attributes_to_serialize
57
+ subclass.attributes_to_serialize.concat(@attributes_to_serialize)
58
+ subclass.instance_variable_set(:@transform_method, @transform_method) unless subclass.transform_method
59
+ end
60
+
52
61
  def attributes(*attributes, **options, &block)
53
62
  attributes.each do |key|
54
- attribute = Attribute.new(key, options[:if], &block)
63
+ attribute = Attribute.new(key, options[:if], options[:entity], &block)
55
64
  attribute.transformed_key = run_transform_key(key)
56
65
  @attributes_to_serialize << attribute
57
66
  end
@@ -74,6 +83,20 @@ module BrightSerializer
74
83
  input.to_sym
75
84
  end
76
85
  end
86
+
87
+ def entity
88
+ {}.tap do |result|
89
+ @attributes_to_serialize.each do |attribute|
90
+ entity_value = attribute.entity&.to_h ||
91
+ BrightSerializer::Entity::Base::DEFAULT_DEFINITION
92
+ result.merge!(attribute.transformed_key => entity_value)
93
+ end
94
+ end
95
+ end
96
+
97
+ def entity_name
98
+ name.split('::').last.downcase
99
+ end
77
100
  end
78
101
  end
79
102
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BrightSerializer
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.3'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bright_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean-Francis Bastien
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-13 00:00:00.000000000 Z
11
+ date: 2021-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -80,14 +80,13 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
83
- description: 'T O D O: Write a longer description or delete this line.'
83
+ description: BrightSerializer is a minimalist implementation serializer for Ruby objects.
84
84
  email:
85
85
  - jfbastien@petalmd.com
86
86
  executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - ".github/workflows/build.yml"
91
90
  - ".gitignore"
92
91
  - ".rspec"
93
92
  - ".rubocop.yml"
@@ -101,6 +100,8 @@ files:
101
100
  - bright_serializer.gemspec
102
101
  - lib/bright_serializer.rb
103
102
  - lib/bright_serializer/attribute.rb
103
+ - lib/bright_serializer/entity/base.rb
104
+ - lib/bright_serializer/entity/parser.rb
104
105
  - lib/bright_serializer/inflector.rb
105
106
  - lib/bright_serializer/serializer.rb
106
107
  - lib/bright_serializer/version.rb
@@ -129,5 +130,5 @@ requirements: []
129
130
  rubygems_version: 3.1.2
130
131
  signing_key:
131
132
  specification_version: 4
132
- summary: 'T O D O: Write a short summary, because RubyGems requires one.'
133
+ summary: Light and fast Ruby serializer
133
134
  test_files: []
@@ -1,24 +0,0 @@
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
24
-