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

Multiple serializers #66

Merged
merged 7 commits into from
Feb 4, 2018
Merged

Multiple serializers #66

merged 7 commits into from
Feb 4, 2018

Conversation

nulldef
Copy link
Collaborator

@nulldef nulldef commented Feb 1, 2018

Resolve #65

self_class.instance_variable_set(SERIALIZER_CLASS, serializer_class)
# @param [Symbol] tag a tag associated with serializer
def add_serializer(self_class, serializer_class, tag: nil)
tag = tag || DEFAULT_TAG

Choose a reason for hiding this comment

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

Use self-assignment shorthand ||=.

def find_serializer(klass)
klass.instance_variable_get(SERIALIZER_CLASS)
def find_serializer(klass, tag: nil)
tag = tag || DEFAULT_TAG

Choose a reason for hiding this comment

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

Use self-assignment shorthand ||=.

@@ -49,7 +49,8 @@ module InstanceMethods
# # => "{\"name\":\"Nikita\",\"age\":23}"
# # For more examples see README
def surrealize(**args)
if (serializer = Surrealist::VarsHelper.find_serializer(self.class))
serializer = Surrealist::VarsHelper.find_serializer(self.class, tag: args[:tag])
if serializer

Choose a reason for hiding this comment

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

Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.

self_class.instance_variable_set(SERIALIZER_CLASS, serializer_class)
# @param [Symbol] tag a tag associated with serializer
def add_serializer(self_class, serializer_class, tag: nil)
tag = tag || DEFAULT_TAG

Choose a reason for hiding this comment

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

Use self-assignment shorthand ||=.

def find_serializer(klass)
klass.instance_variable_get(SERIALIZER_CLASS)
def find_serializer(klass, tag: nil)
tag = tag || DEFAULT_TAG

Choose a reason for hiding this comment

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

Use self-assignment shorthand ||=.

@@ -49,7 +49,8 @@ module InstanceMethods
# # => "{\"name\":\"Nikita\",\"age\":23}"
# # For more examples see README
def surrealize(**args)
if (serializer = Surrealist::VarsHelper.find_serializer(self.class))
serializer = Surrealist::VarsHelper.find_serializer(self.class, tag: args[:tag])
if serializer

Choose a reason for hiding this comment

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

Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.

self_class.instance_variable_set(SERIALIZER_CLASS, serializer_class)
# @param [Symbol] tag a tag associated with serializer
def add_serializer(self_class, serializer_class, tag: nil)
tag = tag || DEFAULT_TAG

Choose a reason for hiding this comment

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

Use self-assignment shorthand ||=.

def find_serializer(klass)
klass.instance_variable_get(SERIALIZER_CLASS)
def find_serializer(klass, tag: nil)
tag = tag || DEFAULT_TAG

Choose a reason for hiding this comment

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

Use self-assignment shorthand ||=.

@@ -49,7 +49,8 @@ module InstanceMethods
# # => "{\"name\":\"Nikita\",\"age\":23}"
# # For more examples see README
def surrealize(**args)
if (serializer = Surrealist::VarsHelper.find_serializer(self.class))
serializer = Surrealist::VarsHelper.find_serializer(self.class, tag: args[:tag])
if serializer

Choose a reason for hiding this comment

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

Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.

Copy link
Owner

@nesaulov nesaulov left a comment

Choose a reason for hiding this comment

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

Could you please also add some specs on this new feature? (covering both #surrealize & #build_schema)

@@ -5,7 +5,7 @@
[![Gem Version](https://badge.fury.io/rb/surrealist.svg)](https://rubygems.org/gems/surrealist)

![Surrealist](surrealist-icon.png)

Copy link
Owner

Choose a reason for hiding this comment

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

Could you please rollback changes concerning whitespaces here? They are necessary for proper code displaying.

@@ -148,7 +148,8 @@ def build_schema(instance:, **args)
# provided in the object's class. Values will be taken from the return values
# of appropriate methods from the object.
def __build_schema(object, **args)
if (serializer = Surrealist::VarsHelper.find_serializer(object.class))
serializer = Surrealist::VarsHelper.find_serializer(object.class, tag: args[:tag])
if serializer
Copy link
Owner

@nesaulov nesaulov Feb 3, 2018

Choose a reason for hiding this comment

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

I think you forgot to apply the new feature to the #build_schema method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is no external serializer support for this method

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, I guess I missed it in #61.

@@ -98,9 +98,9 @@ def delegate_surrealization_to(klass)
# @param [Class] klass a class that should inherit form Surrealist::Serializer
#
# @raise ArgumentError if Surrealist::Serializer is not found in the ancestors chain
def surrealize_with(klass)
def surrealize_with(klass, tag: :default)
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe we should use DEFAULT_TAG here also?

klass.instance_variable_get(SERIALIZER_CLASS)
def find_serializer(klass, tag: nil)
tag ||= DEFAULT_TAG
klass.instance_variable_get(SERIALIZER_CLASSES).try(:[], tag.to_sym)
Copy link
Owner

Choose a reason for hiding this comment

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

We don't have ActiveSupport in here 🙃

Copy link
Owner

Choose a reason for hiding this comment

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

You can use return + fetch(tag.to_sym, nil) here I guess (safe navigation would be better, but it's not available in ruby 2.2)

@@ -148,7 +148,8 @@ def build_schema(instance:, **args)
# provided in the object's class. Values will be taken from the return values
# of appropriate methods from the object.
def __build_schema(object, **args)
if (serializer = Surrealist::VarsHelper.find_serializer(object.class))
serializer = Surrealist::VarsHelper.find_serializer(object.class, tag: args[:tag])
if serializer
Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, I guess I missed it in #61.

def find_serializer(klass, tag: nil)
tag ||= DEFAULT_TAG
hash = klass.instance_variable_get(SERIALIZER_CLASSES)
return if hash.nil?
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe hash && hash.fetch() ?

end

it do
json = Surrealist.surrealize_collection(collection, tag: :short)
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe extract this to rspec's let, so we can make it a one-liner?
Or use specify instead of it

@nesaulov
Copy link
Owner

nesaulov commented Feb 3, 2018

@nulldef one more thing: we need an error message for the case when the wrong tag is passed. From your spec right now:

Surrealist.surrealize_collection([post1, post2], tag: :kek)
  Surrealist::UnknownSchemaError: Can't serialize Post - no schema was provided.
from /Users/nesaulov/Code/ruby/opensource/surrealist/lib/surrealist/exception_raiser.rb:49:in `raise_unknown_schema!'

The error message is wrong and misleading. I suggest something like "The tag specified (#{tag}) has no corresponding serializer".

@nesaulov nesaulov merged commit 70dcb5f into nesaulov:master Feb 4, 2018
@nesaulov
Copy link
Owner

nesaulov commented Feb 4, 2018

Thank you, @nulldef! 🎉

@nulldef nulldef deleted the multiserializer branch February 27, 2018 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants