Skip to content

Commit

Permalink
Build schema for collections from Surrealist::Serializer (#74)
Browse files Browse the repository at this point in the history
* [#73] Fix `build_schema` issue for collections

* Remove comments
  • Loading branch information
nesaulov authored Feb 6, 2018
1 parent b3482f4 commit b4bf6b1
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/surrealist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def included(base)
# # => "[{\"name\":\"Nikita\",\"age\":23}, {\"name\":\"Alessandro\",\"age\":24}]"
# # For more examples see README
def surrealize_collection(collection, **args)
raise Surrealist::ExceptionRaiser.raise_invalid_collection! unless collection.respond_to?(:each)
Surrealist::ExceptionRaiser.raise_invalid_collection! unless collection.respond_to?(:each)

result = collection.map do |object|
Helper.surrealist?(object.class) ? __build_schema(object, args) : object
Expand Down
5 changes: 4 additions & 1 deletion lib/surrealist/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ def surrealize(**args)
serializer = Surrealist::VarsHelper.find_serializer(self.class, tag: args[:tag])
return serializer.new(self).surrealize(args) if serializer

Oj.dump(build_schema(args), mode: :compat)
Oj.dump(Surrealist.build_schema(instance: self, **args), mode: :compat)
end

# Invokes +Surrealist+'s class method +build_schema+
def build_schema(**args)
serializer = Surrealist::VarsHelper.find_serializer(self.class, tag: args[:tag])
return serializer.new(self).build_schema(args) if serializer

Surrealist.build_schema(instance: self, **args)
end
end
Expand Down
11 changes: 10 additions & 1 deletion lib/surrealist/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,22 @@ def surrealize(**args)

# Passes build_schema to Surrealist
def build_schema(**args)
Surrealist.build_schema(instance: self, **args)
if object.respond_to?(:each)
build_collection_schema(args)
else
Surrealist.build_schema(instance: self, **args)
end
end

private

attr_reader :object, :context

# Maps collection and builds schema for each instance.
def build_collection_schema(**args)
object.map { |object| self.class.new(object, context).build_schema(args) }
end

# Methods not found inside serializer will be invoked on the object
def method_missing(method, *args, &block)
object.public_send(method, *args, &block)
Expand Down
101 changes: 77 additions & 24 deletions spec/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,62 +57,115 @@ class WrongSerializer
describe 'Explicit surrealization through `Serializer.new`' do
describe 'instance' do
let(:instance) { Doge.new('John') }
let(:expectation) { { name: 'John', name_length: 4 }.to_json }
subject(:json) { DogeSerializer.new(instance).surrealize }

it { is_expected.to eq expectation }
it_behaves_like 'error is raised for invalid params: instance'
it_behaves_like 'error is not raised for valid params: instance'
describe '#surrealize' do
let(:expectation) { { name: 'John', name_length: 4 }.to_json }
subject(:json) { DogeSerializer.new(instance).surrealize }

it { is_expected.to eq expectation }
it_behaves_like 'error is raised for invalid params: instance'
it_behaves_like 'error is not raised for valid params: instance'
end

describe '#build_schema' do
let(:expectation) { { name: 'John', name_length: 4 } }
subject(:hash) { DogeSerializer.new(instance).build_schema }

it { is_expected.to eq expectation }
end

describe 'passing context to serializer' do
let(:milk) { Struct.new(:amount).new(20) }
let(:flour) { Struct.new(:amount).new(40) }
let(:instance) { Pancake.new(milk) }
let(:expectation) { { amount: 60, color: 'yellow' }.to_json }
subject(:json) { PancakeSerializer.new(instance, flour: flour).surrealize }

it { is_expected.to eq expectation }
describe '#surrealize' do
let(:expectation) { { amount: 60, color: 'yellow' }.to_json }
subject(:json) { PancakeSerializer.new(instance, flour: flour).surrealize }

it { is_expected.to eq expectation }
end

describe '#build_schema' do
let(:expectation) { { amount: 60, color: 'yellow' } }
subject(:hash) { PancakeSerializer.new(instance, flour: flour).build_schema }

it { is_expected.to eq expectation }
end
end
end

describe 'collection' do
let(:collection) { [Doge.new('John'), Doge.new('Josh')] }
let(:expectation) { [{ name: 'John', name_length: 4 }, { name: 'Josh', name_length: 4 }].to_json }
subject(:json) { DogeSerializer.new(collection).surrealize }

it { is_expected.to eq expectation }
it_behaves_like 'error is raised for invalid params: collection'
it_behaves_like 'error is not raised for valid params: collection'
describe '#surrealize' do
let(:expectation) { [{ name: 'John', name_length: 4 }, { name: 'Josh', name_length: 4 }].to_json }
subject(:json) { DogeSerializer.new(collection).surrealize }

it { is_expected.to eq expectation }
it_behaves_like 'error is raised for invalid params: collection'
it_behaves_like 'error is not raised for valid params: collection'
end

describe '#build_schema' do
let(:expectation) { [{ name: 'John', name_length: 4 }, { name: 'Josh', name_length: 4 }] }
subject(:hash) { DogeSerializer.new(collection).build_schema }

it { is_expected.to eq expectation }
end

describe 'passing context to serializer' do
let(:milk) { Struct.new(:amount).new(20) }
let(:flour) { Struct.new(:amount).new(40) }
let(:collection) { [Pancake.new(milk), Pancake.new(milk)] }
let(:expectation) { [{ amount: 60, color: 'yellow' }, { amount: 60, color: 'yellow' }].to_json }
subject(:json) { PancakeSerializer.new(collection, flour: flour).surrealize }

it { is_expected.to eq expectation }
describe '#surrealize' do
let(:expectation) { [{ amount: 60, color: 'yellow' }, { amount: 60, color: 'yellow' }].to_json }
subject(:json) { PancakeSerializer.new(collection, flour: flour).surrealize }

it { is_expected.to eq expectation }
end

describe '#build_schema' do
let(:expectation) { [{ amount: 60, color: 'yellow' }, { amount: 60, color: 'yellow' }] }
subject(:hash) { PancakeSerializer.new(collection, flour: flour).build_schema }

it { is_expected.to eq expectation }
end
end
end
end

describe 'Implicit surrealization using .surrealize_with' do
describe 'instance' do
let(:instance) { Doge.new('George') }
let(:expectation) { { name: 'George', name_length: 6 }.to_json }
subject(:json) { instance.surrealize }

it { is_expected.to eq expectation }
it_behaves_like 'error is raised for invalid params: instance'
it_behaves_like 'error is not raised for valid params: instance'
describe '#surrealize' do
let(:expectation) { { name: 'George', name_length: 6 }.to_json }
subject(:json) { instance.surrealize }

it { is_expected.to eq expectation }
it_behaves_like 'error is raised for invalid params: instance'
it_behaves_like 'error is not raised for valid params: instance'
end

describe '#build_schema' do
let(:expectation) { { name: 'George', name_length: 6 } }
subject(:hash) { instance.build_schema }

it { is_expected.to eq expectation }
end
end

describe 'collection' do
let(:collection) { [Doge.new('Wow'), Doge.new('Doge')] }
let(:expectation) { [{ name: 'Wow', name_length: 3 }, { name: 'Doge', name_length: 4 }].to_json }
subject(:json) { Surrealist.surrealize_collection(collection) }

it { is_expected.to eq expectation }
describe '#surrealize' do
let(:expectation) { [{ name: 'Wow', name_length: 3 }, { name: 'Doge', name_length: 4 }].to_json }
subject(:json) { Surrealist.surrealize_collection(collection) }

it { is_expected.to eq expectation }
end
end
end

Expand Down

0 comments on commit b4bf6b1

Please sign in to comment.