Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix serialize Struct, now Struct is not a collection #135

Merged
merged 6 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/surrealist/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def self.collection?(object)
# 4.2 AR relation object did not include Enumerable (it defined
# all necessary method through ActiveRecord::Delegation module),
# so we need to explicitly check for this
(object.is_a?(Enumerable) && !object.instance_of?(Hash)) || ar_relation?(object)
return false if object.is_a?(Struct)

object.is_a?(Enumerable) && !object.instance_of?(Hash) || ar_relation?(object)
end

def self.ar_relation?(object)
Expand Down
13 changes: 13 additions & 0 deletions spec/helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

TestStruct = Struct.new(:name, :other_param)

RSpec.describe Surrealist::Helper do
describe 'serializing a single struct' do
let(:person) { TestStruct.new('John', 'Dow') }

specify 'a struct is not treated as a collection' do
expect(Surrealist::Helper.collection?(person)).to eq false
end
end
end
32 changes: 32 additions & 0 deletions spec/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ class FooChildSerializer < FooSerializer
end
end

TestStruct = Struct.new(:name, :other_param)

class TestStructSerializer < Surrealist::Serializer
json_schema { { name: String, other_param: String } }
end

RSpec.describe Surrealist::Serializer do
describe 'Explicit surrealization through `Serializer.new`' do
describe 'instance' do
Expand Down Expand Up @@ -210,6 +216,14 @@ class FooChildSerializer < FooSerializer
it { is_expected.to eq expectation }
end
end

describe 'serializing a single struct' do
let(:person) { TestStruct.new('John', 'Dow') }
let(:expectation) { { name: 'John', other_param: 'Dow' } }
subject(:hash) { TestStructSerializer.new(person).build_schema }

it { is_expected.to eq expectation }
end
end

describe 'collection' do
Expand Down Expand Up @@ -250,6 +264,24 @@ class FooChildSerializer < FooSerializer
it { is_expected.to eq expectation }
end
end

describe 'collection of structs' do
let(:person1) { TestStruct.new('John', 'Dow') }
let(:person2) { TestStruct.new('Mountain', 'Dew') }
let(:person3) { TestStruct.new('Harley', 'Queen') }
let(:struct_collection) { [person1, person2, person3] }
let(:expectation) do
[
{ name: 'John', other_param: 'Dow' },
{ name: 'Mountain', other_param: 'Dew' },
{ name: 'Harley', other_param: 'Queen' },
]
end

subject(:json) { TestStructSerializer.new(struct_collection).build_schema }

it { is_expected.to eq expectation }
end
end
end

Expand Down