Skip to content

Commit

Permalink
Fix serializing when direct instance method call returns nil (#141)
Browse files Browse the repository at this point in the history
* Fix serializing when direct instance method call returns nil

* Fix renaming anonymous class after freeze in ruby 2.7.0
  • Loading branch information
past-one authored and nesaulov committed May 23, 2019
1 parent a011a5f commit 35f31c0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/surrealist/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Surrealist
# A class that builds a hash from the schema and type-checks the values.
class Builder
# Struct to carry schema along
Schema = Struct.new(:key, :value).freeze
Schema = Struct.new(:key, :value)
Schema.freeze

# @param [Carrier] carrier instance of Surrealist::Carrier
# @param [Hash] schema the schema defined in the object's class.
Expand Down
4 changes: 3 additions & 1 deletion lib/surrealist/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def build_collection_schema(**args)

# Methods not found inside serializer will be invoked on the object
def method_missing(method, *args, &block)
object.public_send(method, *args, &block) || super
return super unless object.respond_to?(method)

object.public_send(method, *args, &block)
end

# Methods not found inside serializer will be invoked on the object
Expand Down
18 changes: 17 additions & 1 deletion spec/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ class TestStructSerializer < Surrealist::Serializer
json_schema { { name: String, other_param: String } }
end

class AliasSerializer < Surrealist::Serializer
json_schema { { param: String } }

def param
other_param
end
end

RSpec.describe Surrealist::Serializer do
describe 'Explicit surrealization through `Serializer.new`' do
describe 'instance' do
Expand Down Expand Up @@ -217,13 +225,21 @@ class TestStructSerializer < Surrealist::Serializer
end
end

describe 'serializing a single struct' do
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

describe 'serializing when direct instance method call returns nil' do
let(:person) { TestStruct.new('John', nil) }
let(:expectation) { { param: nil } }
subject(:hash) { AliasSerializer.new(person).build_schema }

it { is_expected.to eq expectation }
end
end

describe 'collection' do
Expand Down

0 comments on commit 35f31c0

Please sign in to comment.