fast_serializer_ruby 0.2.1 → 0.4.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: 4226b5f0bc68c749226425f93db90d60bfe5b0cba0faa74a77e9cfd24680a73b
4
- data.tar.gz: 697304fc0c88cdd9848c974027689c5eb4a0f90daa3e95fb4c2d2e723ceb8040
3
+ metadata.gz: c2fe112ed6cdaf1cd66461af7dda6f655b2b818de5e3c53e9932e115daeefd77
4
+ data.tar.gz: 8d54849329a8e725d7a1cb68df6650f3bec50dc40881728fb2dd703c16cd5cf0
5
5
  SHA512:
6
- metadata.gz: ad76357998ec5b0de5768057e6dd6191f4cc8f7eba5dfc9170206245570ad725ba822f1bbf5d35dfed5364227f6dd28130aaadc70717158f42d80d906f76f98b
7
- data.tar.gz: 2a43e81fe55e7d4a8dd56b56a489030d38f4d810564b332ee0f965b0d16b3152622a5aab97779dfa45b8cfe276640b16da6fd1eb7c81c4a629bbb82939ab6e05
6
+ metadata.gz: 43c4b93e93306ccb4d59a6386576fe7277aa24306fa74a36b2661ca78671f0355a9b85ab085c32619117cfb3f1e57e8663e2873064bbfc8a6c12b7a9fa2aeb0a
7
+ data.tar.gz: e843ff147fe2e66ebbcba2436ed372cbcce3b902282e8b71ce0b087ff8c16084cb2c74edd09fc9334040f6b0070c763479ba0f3dc38c0df177373fb2343441ed
data/.travis.yml CHANGED
@@ -17,9 +17,6 @@ matrix:
17
17
  - rvm: ruby-head
18
18
  - rvm: jruby-head
19
19
 
20
- before_install:
21
- - gem update --system -N
22
-
23
20
  before_script:
24
21
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
25
22
  - chmod +x ./cc-test-reporter
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fast_serializer_ruby (0.2.0)
4
+ fast_serializer_ruby (0.4.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -123,6 +123,3 @@ DEPENDENCIES
123
123
  rspec (~> 3.0)
124
124
  rspec-benchmark
125
125
  simplecov
126
-
127
- BUNDLED WITH
128
- 2.0.2
@@ -24,7 +24,16 @@ module FastSerializer
24
24
 
25
25
  cond = @opts[:if] || @opts[:unless]
26
26
 
27
- res = context.instance_exec(resource, params, &cond)
27
+ res = if cond.is_a?(Proc)
28
+ if cond.arity.abs == 1
29
+ context.instance_exec(resource, &cond)
30
+ else
31
+ context.instance_exec(resource, params, &cond)
32
+ end
33
+ else
34
+ context.public_send(cond)
35
+ end
36
+
28
37
  res = !res unless @opts[:unless].nil?
29
38
 
30
39
  res
@@ -6,7 +6,7 @@ module FastSerializer
6
6
  attr_accessor :key, :method, :context
7
7
 
8
8
  def initialize(key: nil, method: nil, opts: {}, **_)
9
- @key = key
9
+ @key = key&.to_sym
10
10
  @method = method || key
11
11
  @opts = opts || {}
12
12
  end
@@ -14,13 +14,23 @@ module FastSerializer
14
14
  end
15
15
 
16
16
  def included?(resource, params, context)
17
- super(resource, params) && include_relation?(params)
17
+ super && include_relation?(params)
18
18
  end
19
19
 
20
20
  def include_relation?(params)
21
- return true if params[:include].nil?
21
+ include?(params) && !exclude?(params)
22
+ end
22
23
 
23
- params[:include].include?(key)
24
+ def exclude?(params)
25
+ return false if params[:exclude].nil?
26
+ return false if params[:exclude].empty?
27
+ params[:exclude_index].key?(key)
28
+ end
29
+
30
+ def include?(params)
31
+ return true if params[:include].nil?
32
+ return false if params[:include].empty?
33
+ params[:include_index].has_key?(key)
24
34
  end
25
35
  end
26
36
  end
@@ -12,11 +12,29 @@ module FastSerializer
12
12
  @serialization_schema = JsonModel::Object.new
13
13
  @params = (params || {}).symbolize_keys
14
14
  @params[:self] = self
15
+ @params[:include_index] = {}
16
+ @params[:exclude_index] = {}
15
17
 
16
- if @params[:include]
17
- if @params[:include].any?
18
- @params[:include] = @params[:include].map(&:to_sym)
19
- end
18
+ self.include = @params.delete(:include)
19
+ self.exclude = @params.delete(:exclude)
20
+ end
21
+
22
+ def include=(list)
23
+ return if !list
24
+
25
+
26
+ if list.any?
27
+ @params[:include] = list.map(&:to_sym)
28
+ @params[:include_index] = @params[:include].map { |key| [key, nil] }.to_h
29
+ end
30
+ end
31
+
32
+ def exclude=(list)
33
+ return if !list
34
+
35
+ if list.any?
36
+ @params[:exclude] = list.map(&:to_sym)
37
+ @params[:exclude_index] = @params[:exclude].map { |key| [key, nil] }.to_h
20
38
  end
21
39
  end
22
40
 
@@ -101,6 +119,7 @@ module FastSerializer
101
119
  meta = _params_dup.delete(:meta)
102
120
 
103
121
  is_collection = resource.respond_to?(:size) && !resource.respond_to?(:each_pair)
122
+ is_collection = params.delete(:is_collection) if params.has_key?(:is_collection)
104
123
 
105
124
  _serialization_schema = if is_collection
106
125
  JsonModel::Array.new(schema: serialization_schema)
@@ -118,7 +137,7 @@ module FastSerializer
118
137
  end
119
138
 
120
139
  def serialize_resource_to_json(resource, params = {}, context = self)
121
- FastSerializer.config.coder.dump(serialize_resource(resource, params))
140
+ FastSerializer.config.coder.dump(serialize_resource(resource, params, context))
122
141
  end
123
142
 
124
143
  module Mixin
@@ -147,11 +166,13 @@ module FastSerializer
147
166
  self.params = params || {}
148
167
  end
149
168
 
150
- def serializable_hash
169
+ def serializable_hash(opts = {})
170
+ self.params = params.merge(opts)
151
171
  self.class.__schema__.serialize_resource(resource, params, self)
152
172
  end
153
173
 
154
- def serialized_json
174
+ def serialized_json(opts = {})
175
+ self.params = params.merge(opts)
155
176
  self.class.__schema__.serialize_resource_to_json(resource, params, self)
156
177
  end
157
178
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FastSerializer
4
- VERSION = '0.2.1'
4
+ VERSION = '0.4.1'
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.2.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeny Stepanov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-16 00:00:00.000000000 Z
11
+ date: 2019-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers