Class: ActiveModel::Serializer::Adapter
- Inherits:
-
Object
- Object
- ActiveModel::Serializer::Adapter
- Extended by:
- ActiveSupport::Autoload
- Defined in:
- lib/active_model/serializer/adapter.rb
Defined Under Namespace
Classes: FlattenJson, FragmentCache, Json, JsonApi, Null
Constant Summary collapse
- UnknownAdapterError =
Class.new(ArgumentError)
Instance Attribute Summary collapse
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
Class Method Summary collapse
- .adapter_class(adapter) ⇒ Object
- .adapter_map ⇒ Object
-
.adapters ⇒ Array<Symbol>
List of adapter names.
- .create(resource, options = {}) ⇒ Object
- .find_by_name(adapter_name) ⇒ Object private
-
.inherited(subclass) ⇒ Object
Automatically register adapters when subclassing.
-
.lookup(adapter) ⇒ ActiveModel::Serializer::Adapter
Subclass of Adapter.
-
.register(name, klass) ⇒ Object
Adds an adapter ‘klass’ with ‘name’ to the ‘adapter_map’ Names are stringified and underscored.
Instance Method Summary collapse
- #as_json(options = nil) ⇒ Object
- #fragment_cache(*args) ⇒ Object
-
#initialize(serializer, options = {}) ⇒ Adapter
constructor
A new instance of Adapter.
- #serializable_hash(options = nil) ⇒ Object
Constructor Details
#initialize(serializer, options = {}) ⇒ Adapter
Returns a new instance of Adapter.
85 86 87 88 |
# File 'lib/active_model/serializer/adapter.rb', line 85 def initialize(serializer, = {}) @serializer = serializer @options = end |
Instance Attribute Details
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
83 84 85 |
# File 'lib/active_model/serializer/adapter.rb', line 83 def serializer @serializer end |
Class Method Details
.adapter_class(adapter) ⇒ Object
21 22 23 |
# File 'lib/active_model/serializer/adapter.rb', line 21 def self.adapter_class(adapter) ActiveModel::Serializer::Adapter.lookup(adapter) end |
.adapter_map ⇒ Object
29 30 31 |
# File 'lib/active_model/serializer/adapter.rb', line 29 def adapter_map ADAPTER_MAP end |
.adapters ⇒ Array<Symbol>
Returns list of adapter names.
34 35 36 |
# File 'lib/active_model/serializer/adapter.rb', line 34 def adapters adapter_map.keys.sort end |
.create(resource, options = {}) ⇒ Object
14 15 16 17 18 |
# File 'lib/active_model/serializer/adapter.rb', line 14 def self.create(resource, = {}) override = .delete(:adapter) klass = override ? adapter_class(override) : ActiveModel::Serializer.adapter klass.new(resource, ) end |
.find_by_name(adapter_name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
70 71 72 73 74 |
# File 'lib/active_model/serializer/adapter.rb', line 70 def find_by_name(adapter_name) adapter_name = adapter_name.to_s.classify.tr('API', 'Api') ActiveModel::Serializer::Adapter.const_get(adapter_name.to_sym) or # rubocop:disable Style/AndOr fail UnknownAdapterError end |
.inherited(subclass) ⇒ Object
Automatically register adapters when subclassing
79 80 81 |
# File 'lib/active_model/serializer/adapter.rb', line 79 def self.inherited(subclass) ActiveModel::Serializer::Adapter.register(subclass.to_s.demodulize, subclass) end |
.lookup(adapter) ⇒ ActiveModel::Serializer::Adapter
Returns subclass of Adapter.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/active_model/serializer/adapter.rb', line 52 def lookup(adapter) # 1. return if is a class return adapter if adapter.is_a?(Class) adapter_name = adapter.to_s.underscore # 2. return if registered adapter_map.fetch(adapter_name) { # 3. try to find adapter class from environment adapter_class = find_by_name(adapter_name) register(adapter_name, adapter_class) adapter_class } rescue NameError, ArgumentError => e = "NameError: #{e.}. Unknown adapter: #{adapter.inspect}. Valid adapters are: #{adapters}" raise UnknownAdapterError, , e.backtrace end |
.register(name, klass) ⇒ Object
Adds an adapter ‘klass’ with ‘name’ to the ‘adapter_map’ Names are stringified and underscored
44 45 46 47 |
# File 'lib/active_model/serializer/adapter.rb', line 44 def register(name, klass) adapter_map.update(name.to_s.underscore => klass) self end |
Instance Method Details
#as_json(options = nil) ⇒ Object
94 95 96 97 98 |
# File 'lib/active_model/serializer/adapter.rb', line 94 def as_json( = nil) hash = serializable_hash() (hash) hash end |
#fragment_cache(*args) ⇒ Object
100 101 102 |
# File 'lib/active_model/serializer/adapter.rb', line 100 def fragment_cache(*args) raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.' end |
#serializable_hash(options = nil) ⇒ Object
90 91 92 |
# File 'lib/active_model/serializer/adapter.rb', line 90 def serializable_hash( = nil) raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.' end |