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

Allow hash surrealization #106

Merged
merged 4 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion lib/surrealist/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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) || ar_relation?(object)
(object.is_a?(Enumerable) && !object.instance_of?(Hash)) || ar_relation?(object)
end

def self.ar_relation?(object)
Expand Down
6 changes: 5 additions & 1 deletion lib/surrealist/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def serializer_context(*array)
# e.g `CatSerializer.new(Cat.new(3), food: CatFood.new)`
# And then food will be available inside serializer via `context[:food]`
def initialize(object, **context)
@object = object
@object = wrap_hash_into_struct(object)
@context = context
end

Expand Down Expand Up @@ -94,5 +94,9 @@ def method_missing(method, *args, &block)
def respond_to_missing?(method, include_private = false)
object.respond_to?(method) || super
end

def wrap_hash_into_struct(object)
object.instance_of?(Hash) ? OpenStruct.new(object) : object
end
end
end
48 changes: 48 additions & 0 deletions spec/build_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,56 @@ def name
# expecting: Surrealist::UnknownSchemaError
end

class ComplexNumber < Surrealist::Serializer
json_schema do
{
real: Integer,
imaginary: Integer,
}
end
end

class DeepHash < Surrealist::Serializer
json_schema do
{
list: Array,
nested: {
left: String,
right: Integer,
},
}
end
end

class HashRoot < Surrealist::Serializer
json_schema do
{ nested: ComplexNumber.defined_schema }
end
end

RSpec.describe Surrealist do
describe '#build_schema' do
context 'with hash arg' do
specify do
expect(ComplexNumber.new(real: 1, imaginary: 2).build_schema)
.to eq(real: 1, imaginary: 2)
end
end

context 'deep hash arg' do
specify do
expect(DeepHash.new(list: [1, 2], nested: { right: 4, left: 'three' }).build_schema)
.to eq(list: [1, 2], nested: { right: 4, left: 'three' })
end
end

context 'root hash with object inside' do
specify do
expect(HashRoot.new(nested: OpenStruct.new(real: 1, imaginary: -1)).build_schema)
.to eq(nested: { real: 1, imaginary: -1 })
end
end

context 'with defined schema' do
context 'with correct types' do
it 'works' do
Expand Down
29 changes: 29 additions & 0 deletions spec/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,35 @@ class FooSerializer < Surrealist::Serializer
it { is_expected.to eq expectation }
end
end

describe 'serializing hash' do
let(:milk) { Struct.new(:amount).new(20) }
let(:flour) { Struct.new(:amount).new(40) }
let(:instance) { { color: 'yellow', amount: 60 } }

describe "#surrealize" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

subject(:json) { PancakeSerializer.new(instance, flour: flour).surrealize }
let(:expectation) { { amount: 60, color: 'yellow' }.to_json }

it { is_expected.to eq expectation }
end

describe "#surrealize with include root" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

subject(:json) { PancakeSerializer.new(instance, flour: flour).surrealize(include_root: true) }
# NOTE: include root doesn't work as well when we call serializer in such way
let(:expectation) { { pancake_serializer: { amount: 60, color: 'yellow' } }.to_json }

it { is_expected.to eq expectation }
end

describe "#surrealize with camelize" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

let(:instance) { { name: 'Pepe' } }
subject(:json) { DogeSerializer.new(instance, flour: flour).surrealize(camelize: true) }
let(:expectation) { { name: 'Pepe', nameLength: 4 }.to_json }

it { is_expected.to eq expectation }
end
end
end

describe 'collection' do
Expand Down