fast_serializer_ruby 0.6.5 → 0.6.7

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: 57d166375199eba26336a76a5fd98fdd3d434c2db20d08857f896137ae8c390a
4
- data.tar.gz: 96aba9fff396a567ee408dad54c1485e39449a9258324fe811c62b98bc5fcf7a
3
+ metadata.gz: a8f0ab89ab3ce2f693f4c850057ecbf4a0651dfb82c7de1fccd2142c5788070a
4
+ data.tar.gz: d767dcb5b4e8b21c8ca1b558fed4860cca7ce7062d94dd32b76d57deb16d7263
5
5
  SHA512:
6
- metadata.gz: e2fbd568da824165230e83774d86a5765592fa03dd494fc9768050db593ce8dba32f257e0542159cdfff9d8756a355c9ca597f5752bd3170baa0092dc9fd1690
7
- data.tar.gz: 13f090892e0d9dd7c97517d46512f2af0bedb5e9c0a1d0a4604da80da704c35c7c7b8276e873055ca15d4f20cd847ce9c6e20c8511ada38e39e49bf4a823266b
6
+ metadata.gz: 0555c28854299014c62e812d3e4c2f9c124ed3d016994202224eea9d8e29188c8c8f7a3f18371f04c892b4dabca2130269fd3212ada2056942c7856d9bbbb042
7
+ data.tar.gz: bf17f9ff6be9eae047392c49d567d9ab246fa088eeeba2c9b90688fc206646b992bb075d6f6024bd73a5e367533d58738bdfc797adf81d9bb14bb0a6eb65f716
@@ -11,7 +11,7 @@ jobs:
11
11
  runs-on: ubuntu-latest
12
12
  strategy:
13
13
  matrix:
14
- ruby-version: ['2.3', '2.4', '2.5', '2.6', '2.7', 'jruby']
14
+ ruby-version: ['2.3', '2.4', '2.5', '2.6', '2.7', '3.0', 'jruby']
15
15
 
16
16
  steps:
17
17
  - uses: actions/checkout@v2
data/Gemfile CHANGED
@@ -21,8 +21,4 @@ group :test do
21
21
  gem 'benchmark-memory'
22
22
  gem 'rspec'
23
23
  gem 'rspec-benchmark'
24
-
25
- platform :mri do
26
- gem 'pry-byebug'
27
- end
28
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
 
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FastSerializer
4
- VERSION = '0.6.5'
4
+ VERSION = '0.6.7'
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.5
4
+ version: 0.6.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeny Stepanov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-13 00:00:00.000000000 Z
11
+ date: 2021-11-14 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.
@@ -50,7 +50,7 @@ metadata:
50
50
  homepage_uri: https://github.com/estepnv/fast_serializer
51
51
  source_code_uri: https://github.com/estepnv/fast_serializer
52
52
  changelog_uri: https://github.com/estepnv/fast_serializer/CHANGELOG.md
53
- post_install_message:
53
+ post_install_message:
54
54
  rdoc_options: []
55
55
  require_paths:
56
56
  - lib
@@ -65,8 +65,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
65
  - !ruby/object:Gem::Version
66
66
  version: '0'
67
67
  requirements: []
68
- rubygems_version: 3.2.27
69
- signing_key:
68
+ rubygems_version: 3.1.6
69
+ signing_key:
70
70
  specification_version: 4
71
71
  summary: fast_serializer is a lightweight ruby objects serializer.
72
72
  test_files: []