fast_serializer_ruby 0.6.4 → 0.6.8

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: ac8a1a9a688c7c866eed16ff360db7ce7e898449d01638f0a2e5ad8e25c10ec3
4
- data.tar.gz: 537b93afce1348b456bd52e0cfa6e5bad52e68067ff890930c1c7282003d0a2d
3
+ metadata.gz: 9c4af5f498164053b8b9a53fd9b8946385d4f541710cd6c3ce76ea4a4840fc64
4
+ data.tar.gz: 1ec8e454473467c832669016032c8305ba859cbee0f51acdd5e8a6c3c012d7e0
5
5
  SHA512:
6
- metadata.gz: df1582559bdd4cc6e17d976c1aaf0916d37485f60a792991038224dea46493fdc4815406e64da801b4c00ea34bcde21fbdafd196697e1f3a9ba5e2ff5cc41f75
7
- data.tar.gz: 238535794943fd0361027c85d99a5e03758e91db2b363bd9bebfc5fa5eec5197e2f8ed0387871136ec5936cdbbc28d1a0e228c280359a9b191aded5478963670
6
+ metadata.gz: 391ce5281f4ed65dbb0c6d505e008bf3c1fe90da341a1f2648cd488757ce0d763f63249aab56de2b2a8af2cdcaf33383816d3c2e477cead34267465671be6e30
7
+ data.tar.gz: 2cea4c882c7b081422d9043136b749d82894dd07515893566ae47ff11f7f4aebc9a1ff15a38173c1bd5b8364c9fe43f966b2df39eb737050239e600a9c6ebb8a
@@ -0,0 +1,42 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ ruby-version: ['2.3', '2.4', '2.5', '2.6', '2.7', '3.0', 'jruby']
15
+
16
+ steps:
17
+ - uses: actions/checkout@v2
18
+ - uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
19
+ with:
20
+ ruby-version: ${{ matrix.ruby-version }}
21
+ bundler-cache: true
22
+ - run: bundle exec rspec --tag ~performance:true
23
+
24
+ coverage:
25
+ runs-on: ubuntu-latest
26
+ strategy:
27
+ matrix:
28
+ ruby-version: ['2.7']
29
+ steps:
30
+ - uses: actions/checkout@v2
31
+ - uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
32
+ with:
33
+ ruby-version: ${{ matrix.ruby-version }}
34
+ bundler-cache: true
35
+ - name: Test & publish code coverage
36
+ uses: paambaati/codeclimate-action@v2.7.5
37
+ with:
38
+ coverageCommand: bundle exec rspec --tag ~performance:true
39
+ env:
40
+ CC_TEST_REPORTER_ID: 99325a42b78ef51a9d51574d20dbcd6acdfe75c8568f1433f73b61f17149e05d
41
+ COVERAGE: 1
42
+
data/.gitignore CHANGED
@@ -15,3 +15,4 @@
15
15
  .history
16
16
  /.vscode
17
17
  .rubocop.yml
18
+ Gemfile.lock
data/Gemfile CHANGED
@@ -12,14 +12,13 @@ group :development, :test do
12
12
  end
13
13
 
14
14
  group :test do
15
- gem 'active_model_serializers', '~> 0.10.0'
15
+ gem 'active_model_serializers'
16
16
  gem 'factory_bot'
17
17
  gem 'faker'
18
- gem 'activesupport', '< 6'
18
+ gem 'activesupport'
19
19
  gem 'allocation_stats'
20
- gem 'simplecov', '~> 0.17.1'
21
- gem 'benchmark-memory', '~> 0.1'
22
- gem 'rspec', '~> 3.0'
20
+ gem 'simplecov'
21
+ gem 'benchmark-memory'
22
+ gem 'rspec'
23
23
  gem 'rspec-benchmark'
24
- gem 'pry-byebug'
25
24
  end
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/fast_serializer_ruby.svg)](https://badge.fury.io/rb/fast_serializer_ruby)
2
- [![Build Status](https://travis-ci.org/estepnv/fast_serializer.svg?branch=master)](https://travis-ci.org/estepnv/fast_serializer)
2
+ ![Build Status](https://github.com/estepnv/fast_serializer/actions/workflows/ruby.yml/badge.svg)
3
3
  [![Maintainability](https://api.codeclimate.com/v1/badges/df7897bec85d376709bd/maintainability)](https://codeclimate.com/github/estepnv/fast_serializer/maintainability)
4
4
  [![Test Coverage](https://api.codeclimate.com/v1/badges/df7897bec85d376709bd/test_coverage)](https://codeclimate.com/github/estepnv/fast_serializer/test_coverage)
5
5
 
@@ -106,8 +106,6 @@ schema.serializable_hash
106
106
  }
107
107
  }
108
108
 
109
-
110
-
111
109
  ```
112
110
 
113
111
 
@@ -14,7 +14,6 @@ Gem::Specification.new do |spec|
14
14
  spec.description = 'This library intends to solve such a typical and on the other hand important problem as efficient ruby object to hash transformation.'
15
15
  spec.homepage = 'https://github.com/estepnv/fast_serializer'
16
16
  spec.license = 'MIT'
17
- spec.platform = Gem::Platform::RUBY
18
17
  spec.required_ruby_version = '>= 2.3.0'
19
18
 
20
19
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
@@ -3,7 +3,11 @@
3
3
  module FastSerializer
4
4
  module JsonModel
5
5
  class Attribute < Node
6
- attr_accessor :mixin,
6
+ attr_accessor :key,
7
+ :method,
8
+ :context,
9
+ :opts,
10
+ :mixin,
7
11
  :method_name,
8
12
  :method_arity,
9
13
  :cond,
@@ -11,18 +15,25 @@ module FastSerializer
11
15
  :cond_method_name,
12
16
  :injected
13
17
 
14
- def initialize(*)
15
- super
18
+ def initialize(key, method, opts = {})
19
+ super()
16
20
 
17
- @mixin = nil
18
- @method_name = nil
21
+ @opts = opts || {}
22
+ @injected_methods = Hash.new { |h, method_name| h[method_name] = true }
23
+ @key = key.to_sym
24
+ @method = !method ? key : method
25
+
26
+ @mixin = Module.new
19
27
  @injected = false
20
- @cond_method_name = nil
21
- @cond = nil
22
- @cond = @opts[:if] || @opts[:unless] || @cond
23
28
 
24
- init_with_proc if method.is_a?(Proc)
25
- init_with_cond if !cond.nil? && cond.is_a?(Proc)
29
+ @method_name = @method && !@method.is_a?(Proc) ? @method : nil
30
+ @method_arity = @method.is_a?(Proc) ? [@method.arity, 0].max : nil
31
+ @cond = @opts[:if] || @opts[:unless]
32
+ @cond_method_name = @cond && !@cond.is_a?(Proc) ? @cond : nil
33
+ @cond_arity = @cond.is_a?(Proc) ? [@cond.arity, 0].max : nil
34
+
35
+ init_with_proc if @method.is_a?(Proc)
36
+ init_with_cond if @cond && @cond.is_a?(Proc)
26
37
  end
27
38
 
28
39
  def injectable?
@@ -39,14 +50,14 @@ module FastSerializer
39
50
  # @param context [Hash]
40
51
  # @return [Object]
41
52
  def serialize(resource, params, context)
42
- can_execute_on_mixin = injected && !method_name.nil? && !context.nil?
53
+ can_execute_on_mixin = !!(injected && method_name && @injected_methods.key?(method_name) && context)
43
54
 
44
55
  val = if can_execute_on_mixin
45
56
  call_method_on_context(context, method_name, method_arity, resource, params)
46
57
  elsif method.is_a?(Proc)
47
58
  call_proc_binding_to_context(context, method, method_arity, resource, params)
48
59
  else
49
- resource.public_send(method)
60
+ resource.public_send(method_name)
50
61
  end
51
62
 
52
63
  val.freeze
@@ -59,9 +70,9 @@ module FastSerializer
59
70
  # @param context [Hash]
60
71
  # @return [Boolean]
61
72
  def included?(resource, params, context)
62
- return true if cond.nil?
73
+ return true if !cond
63
74
 
64
- can_execute_on_mixin = injected && !cond_method_name.nil? && !context.nil?
75
+ can_execute_on_mixin = !!(injected && cond_method_name && @injected_methods.key?(cond_method_name) && context)
65
76
 
66
77
  res = if can_execute_on_mixin
67
78
  call_method_on_context(context, cond_method_name, cond_arity, resource, params)
@@ -80,8 +91,7 @@ module FastSerializer
80
91
 
81
92
  def init_with_cond
82
93
  @cond_method_name = "__#{key}_cond__"
83
- @cond_arity = cond.arity.abs
84
- @mixin ||= Module.new
94
+ @injected_methods[@cond_method_name]
85
95
 
86
96
  if RUBY_VERSION <= '2.5.0'
87
97
  @mixin.redefine_method @cond_method_name, &cond
@@ -92,8 +102,7 @@ module FastSerializer
92
102
 
93
103
  def init_with_proc
94
104
  @method_name = "__#{key}__"
95
- @method_arity = method.arity.abs
96
- @mixin = Module.new
105
+ @injected_methods[@method_name]
97
106
 
98
107
  if RUBY_VERSION <= '2.5.0'
99
108
  @mixin.redefine_method @method_name, &method
@@ -3,16 +3,6 @@
3
3
  module FastSerializer
4
4
  module JsonModel
5
5
  class Node
6
- attr_accessor :key, :method, :context
7
-
8
- # @param key [String]
9
- # @param method [String]
10
- # @param opts [Hash]
11
- def initialize(key: nil, method: nil, opts: {}, **_)
12
- @key = key&.to_sym
13
- @method = method || key
14
- @opts = opts || {}
15
- end
16
6
 
17
7
  # @return [Boolean]
18
8
  def injectable?
@@ -1,11 +1,10 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  module FastSerializer
4
3
  module JsonModel
5
4
  class Object < Node
6
5
  attr_accessor :attributes
7
6
 
8
- def initialize(args = {})
7
+ def initialize
9
8
  super
10
9
  @attributes = {}
11
10
  end
@@ -20,7 +19,7 @@ module FastSerializer
20
19
  # @param context [Hash]
21
20
  # @return [Hash]
22
21
  def serialize(resource, params, context)
23
- return if resource.nil?
22
+ return if !resource
24
23
 
25
24
  result = {}
26
25
 
@@ -7,13 +7,13 @@ module FastSerializer
7
7
 
8
8
  # @param serializer [FastSerializer::Schema::Mixin]
9
9
  # @param schema [FastSerializer::Schema]
10
- def initialize(serializer: nil, schema: nil, **)
11
- super
10
+ def initialize(key, method, serializer = nil, schema = nil, opts = {})
11
+ super(key, method, opts)
12
12
 
13
13
  @serializer_klass = serializer
14
14
  @schema = schema
15
15
 
16
- if @serializer_klass.nil? && @schema.nil?
16
+ if !@serializer_klass && !@schema
17
17
  raise ArgumentError, 'must provide serializer or schema'
18
18
  end
19
19
  end
@@ -43,7 +43,7 @@ module FastSerializer
43
43
  def attributes(*attribute_names)
44
44
  attribute_names.each do |attribute_name|
45
45
  serialization_schema.add_attribute(
46
- JsonModel::Attribute.new(key: attribute_name, method: attribute_name)
46
+ JsonModel::Attribute.new(attribute_name, attribute_name)
47
47
  )
48
48
  end
49
49
  end
@@ -57,12 +57,11 @@ module FastSerializer
57
57
  # @param block [Proc] result is used as the attribute value
58
58
  #
59
59
  def attribute(attribute_name, opts = {}, &block)
60
+ method = (opts.is_a?(String) || opts.is_a?(Symbol)) ? opts : block
61
+ opts = opts.is_a?(Hash) ? opts : {}
62
+
60
63
  serialization_schema.add_attribute(
61
- JsonModel::Attribute.new(
62
- key: attribute_name,
63
- method: block,
64
- opts: opts
65
- )
64
+ JsonModel::Attribute.new(attribute_name, method, opts)
66
65
  )
67
66
  end
68
67
 
@@ -76,12 +75,14 @@ module FastSerializer
76
75
  # @option opts [FastSerializer::Schema] :schema
77
76
  #
78
77
  def has_one(attribute_name, opts = {})
79
- serialization_schema.add_attribute JsonModel::HasOneRelationship.new(
80
- key: opts.delete(:key) || attribute_name,
81
- method: opts.delete(:method) || attribute_name,
82
- opts: opts,
83
- schema: opts.delete(:schema),
84
- serializer: opts.delete(:serializer)
78
+ serialization_schema.add_attribute(
79
+ JsonModel::HasOneRelationship.new(
80
+ opts.delete(:key) || attribute_name,
81
+ opts.delete(:method) || attribute_name,
82
+ opts.delete(:serializer),
83
+ opts.delete(:schema),
84
+ opts,
85
+ )
85
86
  )
86
87
  end
87
88
 
@@ -92,11 +93,11 @@ module FastSerializer
92
93
  def has_many(attribute_name, opts = {})
93
94
  serialization_schema.add_attribute(
94
95
  JsonModel::HasManyRelationship.new(
95
- key: opts.delete(:key) || attribute_name,
96
- method: opts.delete(:method) || attribute_name,
97
- opts: opts,
98
- schema: opts.delete(:schema),
99
- serializer: opts.delete(:serializer)
96
+ opts.delete(:key) || attribute_name,
97
+ opts.delete(:method) || attribute_name,
98
+ opts.delete(:serializer),
99
+ opts.delete(:schema),
100
+ opts,
100
101
  )
101
102
  )
102
103
  end
@@ -106,11 +107,11 @@ module FastSerializer
106
107
  def list(attribute_name, opts = {})
107
108
  serialization_schema.add_attribute(
108
109
  JsonModel::Array.new(
109
- key: attribute_name,
110
- method: attribute_name,
111
- opts: opts,
112
- schema: opts.delete(:schema),
113
- serializer: opts.delete(:serializer)
110
+ attribute_name,
111
+ attribute_name,
112
+ opts.delete(:serializer),
113
+ opts.delete(:schema),
114
+ opts,
114
115
  )
115
116
  )
116
117
  end
@@ -150,7 +151,7 @@ module FastSerializer
150
151
  # need to bind context
151
152
  resource.map { |entry| context.class.new(entry, _params_dup).serializable_hash }
152
153
  else
153
- JsonModel::Array.new(schema: serialization_schema).serialize(resource, _params_dup, context)
154
+ JsonModel::Array.new(:base, :base, nil, serialization_schema).serialize(resource, _params_dup, context)
154
155
  end
155
156
 
156
157
  else
@@ -9,6 +9,8 @@ module FastSerializer
9
9
  end
10
10
 
11
11
  def self.ref_merge(hash_a, hash_b)
12
+ return if hash_a.equal?(hash_b)
13
+
12
14
  hash_b.each do |key, value|
13
15
  hash_a[key] = value
14
16
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FastSerializer
4
- VERSION = '0.6.4'
4
+ VERSION = '0.6.8'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_serializer_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeny Stepanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-26 00:00:00.000000000 Z
11
+ date: 2022-03-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This library intends to solve such a typical and on the other hand important
14
14
  problem as efficient ruby object to hash transformation.
@@ -18,12 +18,11 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - ".github/workflows/ruby.yml"
21
22
  - ".gitignore"
22
- - ".travis.yml"
23
23
  - CHANGELOG.md
24
24
  - CODE_OF_CONDUCT.md
25
25
  - Gemfile
26
- - Gemfile.lock
27
26
  - LICENSE.txt
28
27
  - README.md
29
28
  - Rakefile
@@ -66,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
65
  - !ruby/object:Gem::Version
67
66
  version: '0'
68
67
  requirements: []
69
- rubygems_version: 3.1.2
68
+ rubygems_version: 3.3.6
70
69
  signing_key:
71
70
  specification_version: 4
72
71
  summary: fast_serializer is a lightweight ruby objects serializer.
data/.travis.yml DELETED
@@ -1,40 +0,0 @@
1
- sudo: false
2
-
3
- language: ruby
4
-
5
- rvm:
6
- - 2.3
7
- - 2.4
8
- - 2.5
9
- - 2.6
10
- - 2.7
11
- - jruby-9.2.0.0
12
- - ruby-head
13
- - jruby-head
14
-
15
- matrix:
16
- allow_failures:
17
- - rvm: jruby-9.2.0.0
18
- - rvm: ruby-head
19
- - rvm: jruby-head
20
-
21
- before_install:
22
- - yes | gem update --system --force
23
- - gem install bundler
24
-
25
- before_script:
26
- - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
27
- - chmod +x ./cc-test-reporter
28
- - ./cc-test-reporter before-build
29
-
30
- script: bundle exec rspec --tag ~performance:true
31
-
32
- after_script:
33
- - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
34
-
35
- cache: bundler
36
-
37
- env:
38
- global:
39
- CC_TEST_REPORTER_ID=99325a42b78ef51a9d51574d20dbcd6acdfe75c8568f1433f73b61f17149e05d
40
- COVERAGE=1
data/Gemfile.lock DELETED
@@ -1,139 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- fast_serializer_ruby (0.6.4)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- actionpack (5.2.4.1)
10
- actionview (= 5.2.4.1)
11
- activesupport (= 5.2.4.1)
12
- rack (~> 2.0, >= 2.0.8)
13
- rack-test (>= 0.6.3)
14
- rails-dom-testing (~> 2.0)
15
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
16
- actionview (5.2.4.1)
17
- activesupport (= 5.2.4.1)
18
- builder (~> 3.1)
19
- erubi (~> 1.4)
20
- rails-dom-testing (~> 2.0)
21
- rails-html-sanitizer (~> 1.0, >= 1.0.3)
22
- active_model_serializers (0.10.10)
23
- actionpack (>= 4.1, < 6.1)
24
- activemodel (>= 4.1, < 6.1)
25
- case_transform (>= 0.2)
26
- jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
27
- activemodel (5.2.4.1)
28
- activesupport (= 5.2.4.1)
29
- activesupport (5.2.4.1)
30
- concurrent-ruby (~> 1.0, >= 1.0.2)
31
- i18n (>= 0.7, < 2)
32
- minitest (~> 5.1)
33
- tzinfo (~> 1.1)
34
- allocation_stats (0.1.5)
35
- benchmark-malloc (0.2.0)
36
- benchmark-memory (0.1.2)
37
- memory_profiler (~> 0.9)
38
- benchmark-perf (0.6.0)
39
- benchmark-trend (0.4.0)
40
- builder (3.2.4)
41
- byebug (11.1.3)
42
- case_transform (0.2)
43
- activesupport
44
- coderay (1.1.2)
45
- concurrent-ruby (1.1.6)
46
- crass (1.0.6)
47
- diff-lcs (1.3)
48
- docile (1.3.2)
49
- erubi (1.9.0)
50
- factory_bot (5.1.1)
51
- activesupport (>= 4.2.0)
52
- faker (2.10.2)
53
- i18n (>= 1.6, < 2)
54
- ffi (1.12.2-java)
55
- i18n (1.8.2)
56
- concurrent-ruby (~> 1.0)
57
- json (2.3.0)
58
- json (2.3.0-java)
59
- jsonapi-renderer (0.2.2)
60
- loofah (2.4.0)
61
- crass (~> 1.0.2)
62
- nokogiri (>= 1.5.9)
63
- memory_profiler (0.9.14)
64
- method_source (0.9.2)
65
- mini_portile2 (2.4.0)
66
- minitest (5.14.0)
67
- nokogiri (1.10.9)
68
- mini_portile2 (~> 2.4.0)
69
- nokogiri (1.10.9-java)
70
- pry (0.12.2)
71
- coderay (~> 1.1.0)
72
- method_source (~> 0.9.0)
73
- pry (0.12.2-java)
74
- coderay (~> 1.1.0)
75
- method_source (~> 0.9.0)
76
- spoon (~> 0.0)
77
- pry-byebug (3.8.0)
78
- byebug (~> 11.0)
79
- pry (~> 0.10)
80
- rack (2.2.2)
81
- rack-test (1.1.0)
82
- rack (>= 1.0, < 3)
83
- rails-dom-testing (2.0.3)
84
- activesupport (>= 4.2.0)
85
- nokogiri (>= 1.6)
86
- rails-html-sanitizer (1.3.0)
87
- loofah (~> 2.3)
88
- rake (13.0.1)
89
- rspec (3.9.0)
90
- rspec-core (~> 3.9.0)
91
- rspec-expectations (~> 3.9.0)
92
- rspec-mocks (~> 3.9.0)
93
- rspec-benchmark (0.6.0)
94
- benchmark-malloc (~> 0.2)
95
- benchmark-perf (~> 0.6)
96
- benchmark-trend (~> 0.4)
97
- rspec (>= 3.0)
98
- rspec-core (3.9.1)
99
- rspec-support (~> 3.9.1)
100
- rspec-expectations (3.9.1)
101
- diff-lcs (>= 1.2.0, < 2.0)
102
- rspec-support (~> 3.9.0)
103
- rspec-mocks (3.9.1)
104
- diff-lcs (>= 1.2.0, < 2.0)
105
- rspec-support (~> 3.9.0)
106
- rspec-support (3.9.2)
107
- simplecov (0.17.1)
108
- docile (~> 1.1)
109
- json (>= 1.8, < 3)
110
- simplecov-html (~> 0.10.0)
111
- simplecov-html (0.10.2)
112
- spoon (0.0.6)
113
- ffi
114
- thread_safe (0.3.6)
115
- thread_safe (0.3.6-java)
116
- tzinfo (1.2.6)
117
- thread_safe (~> 0.1)
118
-
119
- PLATFORMS
120
- java
121
- ruby
122
-
123
- DEPENDENCIES
124
- active_model_serializers (~> 0.10.0)
125
- activesupport (< 6)
126
- allocation_stats
127
- benchmark-memory (~> 0.1)
128
- factory_bot
129
- faker
130
- fast_serializer_ruby!
131
- pry
132
- pry-byebug
133
- rake
134
- rspec (~> 3.0)
135
- rspec-benchmark
136
- simplecov (~> 0.17.1)
137
-
138
- BUNDLED WITH
139
- 2.1.4