-
Notifications
You must be signed in to change notification settings - Fork 20
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
Conversation
lib/surrealist/vars_helper.rb
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use self-assignment shorthand ||=.
lib/surrealist/vars_helper.rb
Outdated
def find_serializer(klass) | ||
klass.instance_variable_get(SERIALIZER_CLASS) | ||
def find_serializer(klass, tag: nil) | ||
tag = tag || DEFAULT_TAG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use self-assignment shorthand ||=.
lib/surrealist/instance_methods.rb
Outdated
@@ -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 |
There was a problem hiding this comment.
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 &&/||.
lib/surrealist/vars_helper.rb
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use self-assignment shorthand ||=.
lib/surrealist/vars_helper.rb
Outdated
def find_serializer(klass) | ||
klass.instance_variable_get(SERIALIZER_CLASS) | ||
def find_serializer(klass, tag: nil) | ||
tag = tag || DEFAULT_TAG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use self-assignment shorthand ||=.
lib/surrealist/instance_methods.rb
Outdated
@@ -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 |
There was a problem hiding this comment.
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 &&/||.
lib/surrealist/vars_helper.rb
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use self-assignment shorthand ||=.
lib/surrealist/vars_helper.rb
Outdated
def find_serializer(klass) | ||
klass.instance_variable_get(SERIALIZER_CLASS) | ||
def find_serializer(klass, tag: nil) | ||
tag = tag || DEFAULT_TAG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use self-assignment shorthand ||=.
lib/surrealist/instance_methods.rb
Outdated
@@ -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 |
There was a problem hiding this comment.
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 &&/||.
There was a problem hiding this 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 @@ | |||
[](https://rubygems.org/gems/surrealist) | |||
|
|||
 | |||
|
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
lib/surrealist/class_methods.rb
Outdated
@@ -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) |
There was a problem hiding this comment.
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?
lib/surrealist/vars_helper.rb
Outdated
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) |
There was a problem hiding this comment.
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 🙃
There was a problem hiding this comment.
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)
923c13c
to
1bcfc17
Compare
@@ -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 |
There was a problem hiding this comment.
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.
lib/surrealist/vars_helper.rb
Outdated
def find_serializer(klass, tag: nil) | ||
tag ||= DEFAULT_TAG | ||
hash = klass.instance_variable_get(SERIALIZER_CLASSES) | ||
return if hash.nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe hash && hash.fetch()
?
spec/multiple_serializers_spec.rb
Outdated
end | ||
|
||
it do | ||
json = Surrealist.surrealize_collection(collection, tag: :short) |
There was a problem hiding this comment.
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
@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". |
Thank you, @nulldef! 🎉 |
Resolve #65