fast_serializer 1.1.2 → 1.1.4

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.
metadata CHANGED
@@ -1,32 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
- - We Heart It
8
7
  - Brian Durand
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2020-01-25 00:00:00.000000000 Z
11
+ date: 2023-11-12 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
- requirement: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - "~>"
19
- - !ruby/object:Gem::Version
20
- version: '2.0'
21
- type: :development
22
- prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - "~>"
26
- - !ruby/object:Gem::Version
27
- version: '2.0'
28
- - !ruby/object:Gem::Dependency
29
- name: rake
30
15
  requirement: !ruby/object:Gem::Requirement
31
16
  requirements:
32
17
  - - ">="
@@ -39,51 +24,16 @@ dependencies:
39
24
  - - ">="
40
25
  - !ruby/object:Gem::Version
41
26
  version: '0'
42
- - !ruby/object:Gem::Dependency
43
- name: rspec
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - "~>"
47
- - !ruby/object:Gem::Version
48
- version: '3.0'
49
- type: :development
50
- prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - "~>"
54
- - !ruby/object:Gem::Version
55
- version: '3.0'
56
- - !ruby/object:Gem::Dependency
57
- name: activesupport
58
- requirement: !ruby/object:Gem::Requirement
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: '4.0'
63
- type: :development
64
- prerelease: false
65
- version_requirements: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- version: '4.0'
70
- description: Super fast object serialization for API's combining a simple DSL with
71
- many optimizations under the hood.
27
+ description:
72
28
  email:
73
- - dev@weheartit.com
74
29
  - bbdurand@gmail.com
75
30
  executables: []
76
31
  extensions: []
77
32
  extra_rdoc_files: []
78
33
  files:
79
- - ".gitignore"
80
- - ".travis.yml"
81
- - Gemfile
82
- - Gemfile.lock
83
- - HISTORY.md
84
- - MIT_LICENSE
34
+ - CHANGELOG.md
35
+ - MIT_LICENSE.txt
85
36
  - README.md
86
- - Rakefile
87
37
  - VERSION
88
38
  - fast_serializer.gemspec
89
39
  - lib/fast_serializer.rb
@@ -93,19 +43,11 @@ files:
93
43
  - lib/fast_serializer/serialization_context.rb
94
44
  - lib/fast_serializer/serialized_field.rb
95
45
  - lib/fast_serializer/serializer.rb
96
- - spec/array_serializer_spec.rb
97
- - spec/cache/active_support_cache_spec.rb
98
- - spec/fast_serializer_spec.rb
99
- - spec/serialization_context_spec.rb
100
- - spec/serialized_field_spec.rb
101
- - spec/serializer_spec.rb
102
- - spec/spec_helper.rb
103
- - spec/support/test_models.rb
104
- homepage: https://github.com/weheartit/fast_serializer
46
+ homepage: https://github.com/bdurand/fast_serializer
105
47
  licenses:
106
48
  - MIT
107
49
  metadata: {}
108
- post_install_message:
50
+ post_install_message:
109
51
  rdoc_options: []
110
52
  require_paths:
111
53
  - lib
@@ -113,23 +55,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
55
  requirements:
114
56
  - - ">="
115
57
  - !ruby/object:Gem::Version
116
- version: '0'
58
+ version: '2.5'
117
59
  required_rubygems_version: !ruby/object:Gem::Requirement
118
60
  requirements:
119
61
  - - ">="
120
62
  - !ruby/object:Gem::Version
121
63
  version: '0'
122
64
  requirements: []
123
- rubygems_version: 3.0.3
124
- signing_key:
65
+ rubygems_version: 3.4.12
66
+ signing_key:
125
67
  specification_version: 4
126
- summary: Super fast object serialization for API's.
127
- test_files:
128
- - spec/array_serializer_spec.rb
129
- - spec/cache/active_support_cache_spec.rb
130
- - spec/fast_serializer_spec.rb
131
- - spec/serialization_context_spec.rb
132
- - spec/serialized_field_spec.rb
133
- - spec/serializer_spec.rb
134
- - spec/spec_helper.rb
135
- - spec/support/test_models.rb
68
+ summary: Super fast object serialization for API's combining a simple DSL with many
69
+ optimizations under the hood.
70
+ test_files: []
data/.gitignore DELETED
@@ -1,6 +0,0 @@
1
- pkg
2
- tmp
3
- rdoc
4
- *.rbc
5
- .tm_properties
6
- .DS_Store
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- language: ruby
2
-
3
- rvm:
4
- - "2.6"
5
-
6
- gemfile:
7
- - Gemfile
data/Gemfile DELETED
@@ -1,2 +0,0 @@
1
- source 'https://rubygems.org'
2
- gemspec
data/Gemfile.lock DELETED
@@ -1,50 +0,0 @@
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 DELETED
@@ -1,30 +0,0 @@
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
-
6
- ### 1.1.1
7
-
8
- * Add `array` class method to serializers.
9
-
10
- ### 1.1.0
11
-
12
- * Add helper method for scope option.
13
-
14
- * Pass serialization options to child serializers.
15
-
16
- * Add `if` option to conditionally include fields.
17
-
18
- * Better cache keys handling for more complex objects.
19
-
20
- ### 1.0.2
21
-
22
- * Better integration with ActiveSupport caching.
23
-
24
- ### 1.0.1
25
-
26
- * Compatibility with change to fetch_multi in ActiveSupport 4.2.
27
-
28
- ### 1.0.0
29
-
30
- * Initial release
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task default: :spec
@@ -1,67 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FastSerializer::ArraySerializer do
4
-
5
- it "should serialize an array of regular objects" do
6
- array = [1, 2, 3]
7
- serializer = FastSerializer::ArraySerializer.new(array)
8
- expect(serializer.as_json).to eq array
9
- end
10
-
11
- it "should serialize any Enumerable" do
12
- hash = {:a => 1, :b => 2}
13
- serializer = FastSerializer::ArraySerializer.new(hash)
14
- expect(serializer.as_json).to eq hash.to_a
15
- end
16
-
17
- it "should serializer an array of objects using a specific serializer" do
18
- model_1 = SimpleModel.new(:id => 1, :name => "foo")
19
- model_2 = SimpleModel.new(:id => 2, :name => "bar")
20
- serializer = FastSerializer::ArraySerializer.new([model_1, model_2], :serializer => SimpleSerializer)
21
- expect(JSON.load(serializer.to_json)).to eq [
22
- {"id" => 1, "name" => "foo", "validated" => false},
23
- {"id" => 2, "name" => "bar", "validated" => false}
24
- ]
25
- end
26
-
27
- it "should serializer an array of objects using a specific serializer with options" do
28
- model_1 = SimpleModel.new(:id => 1, :name => "foo")
29
- model_2 = SimpleModel.new(:id => 2, :name => "bar")
30
- serializer = FastSerializer::ArraySerializer.new([model_1, model_2], :serializer => SimpleSerializer, :serializer_options => {:include => :description})
31
- expect(JSON.load(serializer.to_json)).to eq [
32
- {"id" => 1, "name" => "foo", "validated" => false, "description" => nil},
33
- {"id" => 2, "name" => "bar", "validated" => false, "description" => nil}
34
- ]
35
- end
36
-
37
- it "should be able to use the array helper method on a serializer to serialize an array of objects" do
38
- model_1 = SimpleModel.new(:id => 1, :name => "foo")
39
- model_2 = SimpleModel.new(:id => 2, :name => "bar")
40
- array_serializer = FastSerializer::ArraySerializer.new([model_1, model_2], :serializer => SimpleSerializer, :serializer_options => {:include => :description})
41
- helper_serializer = SimpleSerializer.array([model_1, model_2], :serializer_options => {:include => :description})
42
- expect(array_serializer.to_json).to eq helper_serializer.to_json
43
- end
44
-
45
- it "should not respond to_hash methods" do
46
- array = [1, 2, 3]
47
- serializer = FastSerializer::ArraySerializer.new(array)
48
- expect(serializer.respond_to?(:to_hash)).to eq false
49
- expect(serializer.respond_to?(:to_h)).to eq false
50
- end
51
-
52
- it "should respond to to_a" do
53
- array = [1, 2, 3]
54
- serializer = FastSerializer::ArraySerializer.new(array)
55
- expect(serializer.to_a).to eq array
56
- end
57
-
58
- it "should pull cacheable serializers from a cache" do
59
- model_1 = SimpleModel.new(:id => 1, :name => "foo")
60
- model_2 = SimpleModel.new(:id => 2, :name => "bar")
61
- serializer = FastSerializer::ArraySerializer.new([model_1, model_2], serializer: CachedSerializer)
62
- expect(serializer.cacheable?).to eq true
63
- already_cached_json = CachedSerializer.new(model_1).as_json
64
- expect(serializer.as_json.collect(&:object_id)).to eq [already_cached_json.object_id, CachedSerializer.new(model_2).as_json.object_id]
65
- end
66
-
67
- end
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FastSerializer::Cache::ActiveSupportCache do
4
-
5
- it "should fetch from an ActiveSupport cache store" do
6
- cache_store = ActiveSupport::Cache::MemoryStore.new
7
- cache = FastSerializer::Cache::ActiveSupportCache.new(cache_store)
8
- serializer = SimpleSerializer.new(SimpleModel.new(:id => 1))
9
-
10
- expect(cache.fetch(serializer, 60){|s| s.as_json} ).to eq serializer.as_json
11
- expect(cache.fetch(serializer, 60){ raise "boom" }).to eq serializer.as_json
12
- end
13
-
14
- it "should fetch multiple from an ActiveSupport cache store" do
15
- cache_store = ActiveSupport::Cache::MemoryStore.new
16
- cache = FastSerializer::Cache::ActiveSupportCache.new(cache_store)
17
- s1 = SimpleSerializer.new(SimpleModel.new(:id => 1))
18
- s2 = SimpleSerializer.new(SimpleModel.new(:id => 2))
19
-
20
- expect(cache.fetch_all([s1, s2], 60){|s| s.as_json} ).to eq [s1.as_json, s2.as_json]
21
- expect(cache.fetch_all([s1, s2], 60){ raise "boom" }).to eq [s1.as_json, s2.as_json]
22
- end
23
-
24
- end
@@ -1,40 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FastSerializer do
4
-
5
- it "should be able to set and get a global cache" do
6
- expect(FastSerializer.cache).to eq nil
7
- begin
8
- cache = TestCache.new
9
- FastSerializer.cache = cache
10
- expect(FastSerializer.cache).to eq cache
11
- ensure
12
- FastSerializer.cache = nil
13
- end
14
- expect(FastSerializer.cache).to eq nil
15
- end
16
-
17
- it "should set the cache to Rails.cache with the value :rails" do
18
- begin
19
- rails = double(:cache => :rails_cache)
20
- stub_const("Rails", rails)
21
- FastSerializer.cache = :rails
22
- expect(FastSerializer.cache).to be_a FastSerializer::Cache::ActiveSupportCache
23
- expect(FastSerializer.cache.cache).to eq :rails_cache
24
- ensure
25
- FastSerializer.cache = nil
26
- end
27
- end
28
-
29
- it "should set the cache with an ActiveSupport cache" do
30
- begin
31
- cache_store = ActiveSupport::Cache::MemoryStore.new
32
- FastSerializer.cache = cache_store
33
- expect(FastSerializer.cache).to be_a FastSerializer::Cache::ActiveSupportCache
34
- expect(FastSerializer.cache.cache).to eq cache_store
35
- ensure
36
- FastSerializer.cache = nil
37
- end
38
- end
39
-
40
- end
@@ -1,32 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FastSerializer::SerializationContext do
4
-
5
- it "should get a single context only within a block" do
6
- expect(FastSerializer::SerializationContext.current).to eq nil
7
- FastSerializer::SerializationContext.use do
8
- context = FastSerializer::SerializationContext.current
9
- expect(FastSerializer::SerializationContext.current).to_not eq nil
10
- FastSerializer::SerializationContext.use do
11
- expect(FastSerializer::SerializationContext.current).to eq context
12
- end
13
- expect(FastSerializer::SerializationContext.current).to eq context
14
- end
15
- expect(FastSerializer::SerializationContext.current).to eq nil
16
- end
17
-
18
- it "should create serializers and reload them from cache with the same object and options" do
19
- context = FastSerializer::SerializationContext.new
20
- object = SimpleModel.new(:id => 1, :name => "foo")
21
-
22
- serializer = context.load(SimpleSerializer, object, :count => 1)
23
- expect(serializer).to be_a SimpleSerializer
24
- expect(serializer.object).to eq object
25
- expect(serializer.options).to eq(:count => 1)
26
-
27
- expect(context.load(SimpleSerializer, object, :count => 1).object_id).to eq serializer.object_id
28
- expect(context.load(SimpleSerializer, SimpleModel.new(:id => 2, :name => "bar"), :count => 1).object_id).to_not eq serializer.object_id
29
- expect(context.load(SimpleSerializer, object, :count => 2).object_id).to_not eq serializer.object_id
30
- end
31
-
32
- end
@@ -1,82 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FastSerializer::SerializedField do
4
-
5
- let(:field){ FastSerializer::SerializedField.new(:test) }
6
- let(:model){ SimpleModel.new(:id => 1, :name => "foo") }
7
-
8
- it "should integers" do
9
- expect(field.serialize(1)).to eq 1
10
- end
11
-
12
- it "should serialize floats" do
13
- expect(field.serialize(1.5)).to eq 1.5
14
- end
15
-
16
- it "should serialize strings" do
17
- expect(field.serialize("foo")).to eq "foo"
18
- end
19
-
20
- it "should serialize symbols" do
21
- expect(field.serialize(:foo)).to eq :foo
22
- end
23
-
24
- it "should serialize nil" do
25
- expect(field.serialize(nil)).to eq nil
26
- end
27
-
28
- it "should serialize booleans" do
29
- expect(field.serialize(true)).to eq true
30
- expect(field.serialize(false)).to eq false
31
- end
32
-
33
- it "should serialize times" do
34
- time = Time.now
35
- expect(field.serialize(time)).to eq time
36
- end
37
-
38
- it "should serialize dates" do
39
- date = Date.today
40
- expect(field.serialize(date)).to eq date
41
- end
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
-
54
- it "should serialize a field using a specified serializer" do
55
- field = FastSerializer::SerializedField.new(:test, serializer: SimpleSerializer)
56
- expect(field.serialize(model)).to eq({:id => 1, :name => "foo", :validated => false})
57
- end
58
-
59
- it "should serialize an enumerable field using a specified serializer" do
60
- field = FastSerializer::SerializedField.new(:test, serializer: SimpleSerializer, enumerable: true)
61
- expect(field.serialize(model)).to eq([{:id => 1, :name => "foo", :validated => false}])
62
- end
63
-
64
- it "should serialize a field value by calling as_json on the field" do
65
- expect(field.serialize(model)).to eq({:id => 1, :name => "foo", :description => nil, :number => nil})
66
- end
67
-
68
- it "should serialize a hash of objects" do
69
- expect(field.serialize(:name => "Test", :object => model)).to eq({
70
- :name => "Test",
71
- :object => {:id => 1, :name => "foo", :description => nil, :number => nil}
72
- })
73
- end
74
-
75
- it "should serialize an array of objects" do
76
- expect(field.serialize([model, model])).to eq([
77
- {:id => 1, :name => "foo", :description => nil, :number => nil},
78
- {:id => 1, :name => "foo", :description => nil, :number => nil}
79
- ])
80
- end
81
-
82
- end
@@ -1,177 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FastSerializer::Serializer do
4
-
5
- let(:model){ SimpleModel.new(:id => 1, :name => "foo", :description => "foobar", :number => 12) }
6
-
7
- it "should serialize object to JSON compatible format" do
8
- serializer = SimpleSerializer.new(model)
9
- expect(serializer.as_json).to eq({:id => 1, :name => "foo", :validated => false})
10
- expect(serializer.to_hash).to eq({:id => 1, :name => "foo", :validated => false})
11
- expect(serializer.to_h).to eq({:id => 1, :name => "foo", :validated => false})
12
- end
13
-
14
- it "should serialize nil as nil" do
15
- expect(SimpleSerializer.new(nil).as_json).to eq nil
16
- end
17
-
18
- it "should include optional fields only if included in the options" do
19
- expect(SimpleSerializer.new(model).as_json).to eq({:id => 1, :name => "foo", :validated => false})
20
- expect(SimpleSerializer.new(model, :include => :nothing).as_json).to eq({:id => 1, :name => "foo", :validated => false})
21
- expect(SimpleSerializer.new(model, :include => :description).as_json).to eq({:id => 1, :name => "foo", :validated => false, :description => "foobar"})
22
- expect(SimpleSerializer.new(model, :include => ["description"]).as_json).to eq({:id => 1, :name => "foo", :validated => false, :description => "foobar"})
23
- end
24
-
25
- it "should exclude specified fields" do
26
- expect(SimpleSerializer.new(model, :exclude => :name).as_json).to eq({:id => 1, :validated => false})
27
- expect(SimpleSerializer.new(model, :exclude => ["id", "validated"]).as_json).to eq({:name => "foo"})
28
- end
29
-
30
- it "should allow aliasing fields" do
31
- number_model = SimpleModel.new(:number => 50.5)
32
- expect(SimpleSerializer.new(number_model, :include => :amount).as_json).to eq({:id => nil, :name => nil, :validated => false, :amount => 50.5})
33
- end
34
-
35
- it "should pull cached serializers from a cache" do
36
- serializer = SimpleSerializer.new(model)
37
- cached_serializer = CachedSerializer.new(model)
38
-
39
- expect(serializer.cacheable?).to eq false
40
- expect(cached_serializer.cacheable?).to eq true
41
-
42
- expect(serializer.cache_ttl).to eq nil
43
- expect(cached_serializer.cache_ttl).to eq 2
44
-
45
- expect(cached_serializer.as_json).to eq serializer.as_json
46
- expect(cached_serializer.as_json.object_id).to eq cached_serializer.as_json.object_id
47
- end
48
-
49
- it "should allow setting cache and ttl on parent serializers" do
50
- class SubCacheSerializer1 < CachedSerializer
51
- self.cache_ttl = 5
52
- self.cache = :mock
53
- end
54
-
55
- class SubCacheSerializer2 < CachedSerializer
56
- end
57
-
58
- expect(SubCacheSerializer1.cacheable?).to eq true
59
- expect(SubCacheSerializer2.cacheable?).to eq true
60
-
61
- expect(SubCacheSerializer1.cache).to eq :mock
62
- expect(SubCacheSerializer2.cache).to eq CachedSerializer.cache
63
-
64
- expect(SubCacheSerializer1.cache_ttl).to eq 5
65
- expect(SubCacheSerializer2.cache_ttl).to eq 2
66
- end
67
-
68
- it "should allow setting cache and ttl on instances" do
69
- serializer = SimpleSerializer.new(model, :cacheable => true, :cache => :mock, :cache_ttl => 10)
70
- expect(serializer.cacheable?).to eq true
71
- expect(serializer.cache).to eq :mock
72
- expect(serializer.cache_ttl).to eq 10
73
- end
74
-
75
- it "should get the cache from the global setting by default" do
76
- class SubCacheSerializerGlobalInheritTest
77
- include FastSerializer::Serializer
78
- self.cacheable
79
- end
80
-
81
- expect(SubCacheSerializerGlobalInheritTest.cache).to eq nil
82
- allow(FastSerializer).to receive_messages(:cache => :mock)
83
- expect(SubCacheSerializerGlobalInheritTest.cache).to eq :mock
84
- end
85
-
86
- it "should not break on cached serializers if no cache is set" do
87
- serializer = CachedSerializer.new(model, :cache => nil)
88
- expect(serializer.as_json).to eq({:id => 1, :name => "foo", :validated => false})
89
- end
90
-
91
- it "should serialize complex objects" do
92
- other_model = SimpleModel.new(:id => 3, :name => "other")
93
- complex = SimpleModel.new(:id => 2, :name => :complex, :parent => model, :associations => [model, other_model])
94
- serializer = ComplexSerializer.new(complex, :serial_number => 15)
95
- expect(serializer.as_json).to eq({
96
- :id => 2, :name => :complex, :validated => false, :serial_number => 15,
97
- :associations => [
98
- {:id => 1, :name => "foo", :validated => false},
99
- {:id => 3, :name => "other", :validated => false}
100
- ],
101
- :parent => {:id => 1, :name => "foo", :validated => false}
102
- })
103
- end
104
-
105
- it "should pass include and exclude options to child serializers" do
106
- other_model = SimpleModel.new(:id => 3, :name => "other")
107
- complex = SimpleModel.new(:id => 2, :name => :complex, :parent => model, :associations => [model, other_model])
108
- serializer = ComplexSerializer.new(complex, :serial_number => 15, :exclude => {:associations => [:validated]}, :include => {:parent => :amount})
109
- expect(serializer.as_json).to eq({
110
- :id => 2, :name => :complex, :validated => false, :serial_number => 15,
111
- :associations => [
112
- {:id => 1, :name => "foo"},
113
- {:id => 3, :name => "other"}
114
- ],
115
- :parent => {:id => 1, :name => "foo", :validated => false, :amount => 12}
116
- })
117
- end
118
-
119
- it "should have a scope option" do
120
- serializer = CachedSerializer.new(model, :scope => :foo)
121
- expect(serializer.scope).to eq :foo
122
- end
123
-
124
- it "should return identical serialized values for serializers on the same object and options inside the same context" do
125
- other_model = SimpleModel.new(:id => 3, :name => "other")
126
- complex = SimpleModel.new(:id => 2, :name => "complex", :associations => [model, other_model, model])
127
- serializer = ComplexSerializer.new(complex)
128
- json = serializer.as_json
129
- expect(json[:associations][0].object_id).to_not eq json[:associations][1].object_id
130
- expect(json[:associations][0].object_id).to eq json[:associations][2].object_id
131
- end
132
-
133
- it "should dump the object to JSON" do
134
- complex = SimpleModel.new(:id => 2, :name => "complex", :parent => model, :associations => [model])
135
- serializer = ComplexSerializer.new(complex)
136
- expect(JSON.load(serializer.to_json)).to eq({
137
- "id" => 2,
138
- "name" => "complex",
139
- "validated" => false,
140
- "serial_number" => nil,
141
- "associations" => [
142
- {"id" => 1, "name" => "foo", "validated" => false}
143
- ],
144
- "parent" => {"id" => 1, "name" => "foo", "validated" => false, "description"=>"foobar"}
145
- })
146
- end
147
-
148
- it "should allow passing options for JSON dumping" do
149
- complex = SimpleModel.new(:id => 2, :name => "complex", :parent => model, :associations => [model])
150
- serializer = ComplexSerializer.new(complex)
151
- expect(serializer.to_json(:pretty => true)).to be_a(String)
152
- end
153
-
154
- it "should expose options from the options hash by name" do
155
- serializer = CachedSerializer.new(model, :foo => "bar")
156
- expect(serializer.option(:foo)).to eq "bar"
157
- expect(serializer.option(:other)).to eq nil
158
- end
159
-
160
- it "should return nil value for options if none are set" do
161
- serializer = CachedSerializer.new(model, nil)
162
- expect(serializer.option(:foo)).to eq nil
163
- end
164
-
165
- it "should not get into infinite loops" do
166
- model = SimpleModel.new(:id => 1)
167
- model.parent = model
168
- serializer = CircularSerializer.new(model)
169
- expect{ serializer.as_json }.to raise_error(FastSerializer::CircularReferenceError)
170
- end
171
-
172
- it "should allow conditionals on serialized fields" do
173
- expect(ConditionalSerializer.new(model).as_json).to eq({:id => 1})
174
- expect(ConditionalSerializer.new(model, :scope => :description).as_json).to eq({:id => 1, :description => "foobar"})
175
- expect(ConditionalSerializer.new(model, :scope => :name).as_json).to eq({:id => 1, :name => "foo"})
176
- end
177
- end