-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Introduce Adapter::Base #1138
Introduce Adapter::Base #1138
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,26 +1,30 @@ | ||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
module Adapter | ||
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. possible source of error 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. fixed |
||
UnknownAdapterError = Class.new(ArgumentError) | ||
ADAPTER_MAP = {} | ||
private_constant :ADAPTER_MAP if defined?(private_constant) | ||
require 'active_model/serializer/adapter/fragment_cache' | ||
require 'active_model/serializer/adapter/cached_serializer' | ||
|
||
def self.create(resource, options = {}) | ||
override = options.delete(:adapter) | ||
klass = override ? adapter_class(override) : ActiveModel::Serializer.adapter | ||
klass.new(resource, options) | ||
end | ||
class << self # All methods are class functions | ||
def new(*args) | ||
fail ArgumentError, 'Adapters inherit from Adapter::Base.' \ | ||
"Adapter.new called with args: '#{args.inspect}', from" \ | ||
"'caller[0]'." | ||
end | ||
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. Just in case.. but perhaps I could write a better message.. is really meant to be a helpful deprecation fast failure |
||
|
||
# @see ActiveModel::Serializer::Adapter.lookup | ||
def self.adapter_class(adapter) | ||
ActiveModel::Serializer::Adapter.lookup(adapter) | ||
end | ||
def create(resource, options = {}) | ||
override = options.delete(:adapter) | ||
klass = override ? adapter_class(override) : ActiveModel::Serializer.adapter | ||
klass.new(resource, options) | ||
end | ||
|
||
# @see ActiveModel::Serializer::Adapter.lookup | ||
def adapter_class(adapter) | ||
ActiveModel::Serializer::Adapter.lookup(adapter) | ||
end | ||
|
||
# Only the Adapter class has these methods. | ||
# None of the sublasses have them. | ||
class << ActiveModel::Serializer::Adapter | ||
# @return Hash<adapter_name, adapter_class> | ||
def adapter_map | ||
ADAPTER_MAP | ||
|
@@ -76,58 +80,8 @@ def find_by_name(adapter_name) | |
private :find_by_name | ||
end | ||
|
||
# Automatically register adapters when subclassing | ||
def self.inherited(subclass) | ||
ActiveModel::Serializer::Adapter.register(subclass) | ||
end | ||
|
||
attr_reader :serializer, :instance_options | ||
|
||
def initialize(serializer, options = {}) | ||
@serializer = serializer | ||
@instance_options = options | ||
end | ||
|
||
def serializable_hash(options = nil) | ||
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.' | ||
end | ||
|
||
def as_json(options = nil) | ||
hash = serializable_hash(options) | ||
include_meta(hash) | ||
hash | ||
end | ||
|
||
def fragment_cache(*args) | ||
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.' | ||
end | ||
|
||
def cache_check(serializer) | ||
CachedSerializer.new(serializer).cache_check(self) do | ||
yield | ||
end | ||
end | ||
|
||
private | ||
|
||
def meta | ||
serializer.meta if serializer.respond_to?(:meta) | ||
end | ||
|
||
def meta_key | ||
serializer.meta_key || 'meta'.freeze | ||
end | ||
|
||
def root | ||
serializer.json_key.to_sym if serializer.json_key | ||
end | ||
|
||
def include_meta(json) | ||
json[meta_key] = meta if meta | ||
json | ||
end | ||
|
||
# Gotta be at the bottom to use the code above it :( | ||
require 'active_model/serializer/adapter/base' | ||
require 'active_model/serializer/adapter/null' | ||
require 'active_model/serializer/adapter/attributes' | ||
require 'active_model/serializer/adapter/json' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class Base | ||
# Automatically register adapters when subclassing | ||
def self.inherited(subclass) | ||
ActiveModel::Serializer::Adapter.register(subclass) | ||
end | ||
|
||
attr_reader :serializer, :instance_options | ||
|
||
def initialize(serializer, options = {}) | ||
@serializer = serializer | ||
@instance_options = options | ||
end | ||
|
||
def serializable_hash(_options = nil) | ||
fail NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.' | ||
end | ||
|
||
def as_json(options = nil) | ||
hash = serializable_hash(options) | ||
include_meta(hash) | ||
hash | ||
end | ||
|
||
def fragment_cache(*_args) | ||
fail NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.' | ||
end | ||
|
||
def cache_check(serializer) | ||
CachedSerializer.new(serializer).cache_check(self) do | ||
yield | ||
end | ||
end | ||
|
||
private | ||
|
||
def meta | ||
serializer.meta if serializer.respond_to?(:meta) | ||
end | ||
|
||
def meta_key | ||
serializer.meta_key || 'meta'.freeze | ||
end | ||
|
||
def root | ||
serializer.json_key.to_sym if serializer.json_key | ||
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. What's the rationale behind calling it 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 think just evolution... |
||
end | ||
|
||
def include_meta(json) | ||
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's probably not within the realm of this PR, but I think we should stay away from side effects as much as possible when unnecessary. 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. yup |
||
json[meta_key] = meta if meta | ||
json | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
module Adapter | ||
class FragmentCache | ||
attr_reader :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.
There should be a note somewhere stating that the FlattenJSON adapter changed name.
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, that's why I wanted a label for 'add to changelog' for all the times we haven't done it