-
Notifications
You must be signed in to change notification settings - Fork 35
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
Respect namespacing in SerializerResolver #93
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_support/core_ext/string/inflections' | ||
require 'active_support/core_ext/module/introspection' | ||
|
||
class Panko::SerializerResolver | ||
def self.resolve(name) | ||
serializer_name = "#{name.singularize.camelize}Serializer" | ||
serializer_const = safe_const_get(serializer_name) | ||
class << self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took the liverty to convert to |
||
def resolve(name, from) | ||
serializer_const = nil | ||
|
||
return nil if serializer_const.nil? | ||
return nil unless is_serializer(serializer_const) | ||
if namespace = namespace_for(from) | ||
serializer_const = safe_serializer_get("#{namespace}::#{name.singularize.camelize}Serializer") | ||
end | ||
|
||
serializer_const | ||
end | ||
serializer_const ||= safe_serializer_get("#{name.singularize.camelize}Serializer") | ||
serializer_const | ||
end | ||
|
||
private | ||
private | ||
|
||
def self.is_serializer(const) | ||
const < Panko::Serializer | ||
end | ||
if Module.method_defined?(:module_parent_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rails 6.0 renamed |
||
def namespace_for(from) | ||
from.module_parent_name | ||
end | ||
else | ||
def namespace_for(from) | ||
from.parent_name | ||
end | ||
end | ||
|
||
def self.safe_const_get(name) | ||
Object.const_get(name) | ||
rescue NameError | ||
nil | ||
def safe_serializer_get(name) | ||
const = Object.const_get(name) | ||
const < Panko::Serializer ? const : nil | ||
rescue NameError | ||
nil | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,5 @@ Gem::Specification.new do |spec| | |
spec.extensions << "ext/panko_serializer/extconf.rb" | ||
|
||
spec.add_dependency "oj", "~> 3.10.0" | ||
spec.add_dependency "activesupport" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the resolver uses a bunch of ActiveSupport core_ext, it's better to add it to the dependencies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was always a question for me, whether I want a direct dependency on rails for Panko since it can serialize plain ruby objects. but to be honest with me, it's 90% of the project, so ok. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Up to you, we can also try to eliminate the dependency.
But maybe there's a way to have this code only called in Rails context? I don't know. |
||
end |
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.
This is the two AS features used by this file.