fast_serializer 1.1.1 → 1.1.2

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: 9c038967d22d51bae0cd7f9993f625dbc3d1b747590ec3db6826cf4ac3c4c4ba
4
- data.tar.gz: aeffd7e6274da8733f26556e6be03799fc182e05e8888245b4fe3a62df5c881c
3
+ metadata.gz: 612f91c858c466052eb82d76560f4acb6bf55f416a2da7d104b0d3c97d40cddb
4
+ data.tar.gz: 7a515141fcad3e9ed626abd98bb67ce049b00a520a8a1d7e87e99245cfd397e1
5
5
  SHA512:
6
- metadata.gz: 1d21207437c1ea463715ee99cd354de62c27a455df3f58b3ec56ed8b282276cdd1c6b632870750dbe2946d00aecd17e9457a12e1008e49d0d35cf500a05c973e
7
- data.tar.gz: bf2888c605c80312f7fdfb457a258b86ac4c3b4810527e21b5e959c16aaec42933414fefb9121e15bde5a00a941c9cdb63b3d650070788f5712a6a73fac4976f
6
+ metadata.gz: b4dc18b54f002e692ed98c5977f14df1233bd10f71093222098c127412e0455a714f5182d1c8666a8d30915020849edaf61596305724d1437b5e96baf21b7eb4
7
+ data.tar.gz: 4636276312fcb0654cec728e93f35f717dc58c23960588716713c8589a52c5bc59e0b4b143a441199740c26d40d51a665f58ce6ccef599f87f31fd03efb94857
data/.gitignore CHANGED
@@ -1,17 +1,6 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
1
  pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
2
  tmp
3
+ rdoc
4
+ *.rbc
5
+ .tm_properties
6
+ .DS_Store
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - "2.6"
5
+
6
+ gemfile:
7
+ - Gemfile
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fast_serializer (1.1.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (6.0.2.1)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 0.7, < 2)
12
+ minitest (~> 5.1)
13
+ tzinfo (~> 1.1)
14
+ zeitwerk (~> 2.2)
15
+ concurrent-ruby (1.1.5)
16
+ diff-lcs (1.3)
17
+ i18n (1.8.2)
18
+ concurrent-ruby (~> 1.0)
19
+ minitest (5.14.0)
20
+ rake (13.0.1)
21
+ rspec (3.9.0)
22
+ rspec-core (~> 3.9.0)
23
+ rspec-expectations (~> 3.9.0)
24
+ rspec-mocks (~> 3.9.0)
25
+ rspec-core (3.9.1)
26
+ rspec-support (~> 3.9.1)
27
+ rspec-expectations (3.9.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.9.0)
30
+ rspec-mocks (3.9.1)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.9.0)
33
+ rspec-support (3.9.2)
34
+ thread_safe (0.3.6)
35
+ tzinfo (1.2.6)
36
+ thread_safe (~> 0.1)
37
+ zeitwerk (2.2.2)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ activesupport (>= 4.0)
44
+ bundler (~> 2.0)
45
+ fast_serializer!
46
+ rake
47
+ rspec (~> 3.0)
48
+
49
+ BUNDLED WITH
50
+ 2.1.4
data/HISTORY.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### 1.1.2
2
+
3
+ * Handle converting ActiveSupport::TimeWithZone to a Time so it can be better dumped to non-JSON formats.
4
+ * Sanity check for unsupported options
5
+
1
6
  ### 1.1.1
2
7
 
3
8
  * Add `array` class method to serializers.
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![Build Status](https://travis-ci.org/weheartit/fast_serializer.svg?branch=master)](https://travis-ci.org/weheartit/fast_serializer)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/02fd32c734e667432df6/maintainability)](https://codeclimate.com/github/bdurand/fast_serializer/maintainability)
3
+
1
4
  This gem provides a highly optimized framework for serializing Ruby objects into hashes suitable for serialization to some other format (i.e. JSON). It provides many of the same features as other serialization frameworks like active_model_serializers, but it is designed to emphasize code efficiency over feature set and syntactic surgar.
2
5
 
3
6
  ## Examples
data/Rakefile CHANGED
@@ -1,18 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
2
3
 
3
- desc 'Default: run unit tests.'
4
- task :default => :test
4
+ RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- desc 'RVM likes to call it tests'
7
- task :tests => :test
8
-
9
- begin
10
- require 'rspec'
11
- require 'rspec/core/rake_task'
12
- desc 'Run the unit tests'
13
- RSpec::Core::RakeTask.new(:test)
14
- rescue LoadError
15
- task :test do
16
- STDERR.puts "You must have rspec >= 2.0 installed to run the tests"
17
- end
18
- end
6
+ task default: :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.1
1
+ 1.1.2
@@ -17,8 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_development_dependency "bundler", "~>1.3"
20
+ spec.add_development_dependency "bundler", "~>2.0"
21
21
  spec.add_development_dependency "rake"
22
22
  spec.add_development_dependency "rspec", "~>3.0"
23
- spec.add_development_dependency "active_support", ">=4.0"
23
+ spec.add_development_dependency "activesupport", ">=4.0"
24
24
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json'
2
4
  require 'time'
3
5
  require 'date'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module FastSerializer
2
4
  # Serializer implementation for serializing an array of objects.
3
5
  # This class allows taking advantage of a single SerializationContext
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module FastSerializer
2
4
  # Base class for cache implementations for storing cacheable serializers.
3
5
  # Implementations must implement the +fetch+ method.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module FastSerializer
2
4
  # ActiveSupport compatible cache implementation.
3
5
  class Cache::ActiveSupportCache < Cache
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module FastSerializer
2
4
  # This class provides a context for creating serializers that allows
3
5
  # duplicate serializers to be re-used within the context. This then
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module FastSerializer
2
4
  # Data structure used internally for maintaining a field to be serialized.
3
5
  class SerializedField
@@ -67,8 +69,14 @@ module FastSerializer
67
69
 
68
70
  # Convert the value to primitive data types: string, number, boolean, symbol, time, date, array, hash.
69
71
  def serialize_value(value)
70
- if value.is_a?(String) || value.is_a?(Numeric) || value == nil || value == true || value == false || value.is_a?(Time) || value.is_a?(Date) || value.is_a?(Symbol)
72
+ if value.is_a?(String) || value.is_a?(Numeric) || value == nil || value == true || value == false || value.is_a?(Symbol)
71
73
  value
74
+ elsif value.is_a?(Time) || value.is_a?(Date)
75
+ if defined?(ActiveSupport::TimeWithZone) && value.is_a?(ActiveSupport::TimeWithZone)
76
+ value.to_time
77
+ else
78
+ value
79
+ end
72
80
  elsif value.is_a?(Hash)
73
81
  serialize_hash(value)
74
82
  elsif value.is_a?(Enumerable)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module FastSerializer
2
4
  # Models can include this module to define themselves as serializers. A serializer is used to wrap
3
5
  # an object and output a hash version of that object suitable for serialization to JSON or other formats.
@@ -103,16 +105,22 @@ module FastSerializer
103
105
  def serialize(*fields)
104
106
  options = {}
105
107
  if fields.size > 1 && fields.last.is_a?(Hash)
106
- options = fields.last
108
+ fields.last.each do |key, value|
109
+ options[key.to_sym] = value
110
+ end
107
111
  fields = fields[0, fields.size - 1]
108
112
  end
109
- as = options[:as]
110
- optional = options.fetch(:optional, false)
111
- delegate = options.fetch(:delegate, true)
112
- enumerable = options.fetch(:enumerable, false)
113
- serializer = options[:serializer]
114
- serializer_options = options[:serializer_options]
115
- condition = options[:if]
113
+ as = options.delete(:as)
114
+ optional = options.delete(:optional) || false
115
+ delegate = options.delete(:delegate) || true
116
+ enumerable = options.delete(:enumerable) || false
117
+ serializer = options.delete(:serializer)
118
+ serializer_options = options.delete(:serializer_options)
119
+ condition = options.delete(:if)
120
+
121
+ unless options.empty?
122
+ raise ArgumentError.new("Unsupported serialize options: #{options.keys.join(', ')}")
123
+ end
116
124
 
117
125
  if as && fields.size > 1
118
126
  raise ArgumentError.new("Cannot specify :as argument with multiple fields to serialize")
@@ -234,7 +242,7 @@ module FastSerializer
234
242
  private include_method_name
235
243
  condition = include_method_name
236
244
  end
237
-
245
+
238
246
  field = SerializedField.new(name, optional: optional, serializer: serializer, serializer_options: serializer_options, enumerable: enumerable, condition: condition)
239
247
 
240
248
  # Add the field to the frozen list of fields.
@@ -256,7 +264,7 @@ module FastSerializer
256
264
  define_method(attribute){ object.send(field) }
257
265
  end
258
266
  end
259
-
267
+
260
268
  module ArrayHelper
261
269
  # Helper method to serialize an array of values using this serializer.
262
270
  def array(values, options = nil)
@@ -353,14 +361,14 @@ module FastSerializer
353
361
  SerializationContext.use do
354
362
  self.class.serializable_fields.each do |field|
355
363
  name = field.name
356
-
364
+
357
365
  if field.optional?
358
366
  next unless include_fields && include_fields.include?(name)
359
367
  end
360
368
  next if excluded_fields && excluded_fields[name] == true
361
369
  condition = field.condition
362
370
  next if condition && !send(condition)
363
-
371
+
364
372
  value = field.serialize(send(name), serializer_options(name))
365
373
  hash[name] = value
366
374
  end
@@ -40,6 +40,17 @@ describe FastSerializer::SerializedField do
40
40
  expect(field.serialize(date)).to eq date
41
41
  end
42
42
 
43
+ it "should serialize datetimes" do
44
+ datetime = DateTime.now
45
+ expect(field.serialize(datetime)).to eq datetime
46
+ end
47
+
48
+ it "should serialize ActiveSupport::TimeWithZone as a DateTime" do
49
+ time = ActiveSupport::TimeZone.new("UTC").now
50
+ expect(field.serialize(time)).to eq time
51
+ expect(field.serialize(time).class).to eq Time
52
+ end
53
+
43
54
  it "should serialize a field using a specified serializer" do
44
55
  field = FastSerializer::SerializedField.new(:test, serializer: SimpleSerializer)
45
56
  expect(field.serialize(model)).to eq({:id => 1, :name => "foo", :validated => false})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - We Heart It
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-01-12 00:00:00.000000000 Z
12
+ date: 2020-01-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '1.3'
20
+ version: '2.0'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '1.3'
27
+ version: '2.0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -54,7 +54,7 @@ dependencies:
54
54
  - !ruby/object:Gem::Version
55
55
  version: '3.0'
56
56
  - !ruby/object:Gem::Dependency
57
- name: active_support
57
+ name: activesupport
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - ">="
@@ -77,6 +77,9 @@ extensions: []
77
77
  extra_rdoc_files: []
78
78
  files:
79
79
  - ".gitignore"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - Gemfile.lock
80
83
  - HISTORY.md
81
84
  - MIT_LICENSE
82
85
  - README.md
@@ -117,8 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
120
  - !ruby/object:Gem::Version
118
121
  version: '0'
119
122
  requirements: []
120
- rubyforge_project:
121
- rubygems_version: 2.7.6
123
+ rubygems_version: 3.0.3
122
124
  signing_key:
123
125
  specification_version: 4
124
126
  summary: Super fast object serialization for API's.