Module: ActiveModelSerializers::Adapter
- Extended by:
- ActiveSupport::Autoload
- Defined in:
- lib/active_model_serializers/adapter.rb,
lib/active_model_serializers/adapter/base.rb,
lib/active_model_serializers/adapter/json.rb,
lib/active_model_serializers/adapter/null.rb,
lib/active_model_serializers/adapter/json_api.rb,
lib/active_model_serializers/adapter/attributes.rb,
lib/active_model_serializers/adapter/json_api/link.rb,
lib/active_model_serializers/adapter/json_api/meta.rb,
lib/active_model_serializers/adapter/json_api/error.rb,
lib/active_model_serializers/adapter/json_api/jsonapi.rb,
lib/active_model_serializers/adapter/json_api/relationship.rb,
lib/active_model_serializers/adapter/json_api/deserialization.rb,
lib/active_model_serializers/adapter/json_api/pagination_links.rb,
lib/active_model_serializers/adapter/json_api/resource_identifier.rb
Defined Under Namespace
Classes: Attributes, Base, Json, JsonApi, Null
Constant Summary collapse
- UnknownAdapterError =
Class.new(ArgumentError)
Class Method Summary collapse
- .adapter_class(adapter) ⇒ Object
- .adapter_map ⇒ Hash<adapter_name, adapter_class>
-
.adapters ⇒ Array<Symbol>
List of adapter names.
-
.configured_adapter ⇒ Object
:nocov:.
- .create(resource, options = {}) ⇒ Object
- .find_by_name(adapter_name) ⇒ Object private
-
.lookup(adapter) ⇒ ActiveModelSerializers::Adapter
Subclass of Adapter.
-
.new(*args) ⇒ Object
:nocov:.
-
.register(name, klass = name) ⇒ Object
Adds an adapter ‘klass’ with ‘name’ to the ‘adapter_map’ Names are stringified and underscored.
- .registered_name(adapter_class) ⇒ Object
Class Method Details
.adapter_class(adapter) ⇒ Object
29 30 31 |
# File 'lib/active_model_serializers/adapter.rb', line 29 def adapter_class(adapter) ActiveModelSerializers::Adapter.lookup(adapter) end |
.adapter_map ⇒ Hash<adapter_name, adapter_class>
34 35 36 |
# File 'lib/active_model_serializers/adapter.rb', line 34 def adapter_map ADAPTER_MAP end |
.adapters ⇒ Array<Symbol>
Returns list of adapter names.
39 40 41 |
# File 'lib/active_model_serializers/adapter.rb', line 39 def adapters adapter_map.keys.sort! end |
.configured_adapter ⇒ Object
:nocov:
18 19 20 |
# File 'lib/active_model_serializers/adapter.rb', line 18 def configured_adapter lookup(ActiveModelSerializers.config.adapter) end |
.create(resource, options = {}) ⇒ Object
22 23 24 25 26 |
# File 'lib/active_model_serializers/adapter.rb', line 22 def create(resource, = {}) override = .delete(:adapter) klass = override ? adapter_class(override) : configured_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.
83 84 85 86 87 88 |
# File 'lib/active_model_serializers/adapter.rb', line 83 def find_by_name(adapter_name) adapter_name = adapter_name.to_s.classify.tr('API', 'Api') "ActiveModelSerializers::Adapter::#{adapter_name}".safe_constantize || "ActiveModelSerializers::Adapter::#{adapter_name.pluralize}".safe_constantize or # rubocop:disable Style/AndOr fail UnknownAdapterError end |
.lookup(adapter) ⇒ ActiveModelSerializers::Adapter
Returns subclass of Adapter.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/active_model_serializers/adapter.rb', line 65 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) do # 3. try to find adapter class from environment adapter_class = find_by_name(adapter_name) register(adapter_name, adapter_class) adapter_class end rescue NameError, ArgumentError => e = "NameError: #{e.}. Unknown adapter: #{adapter.inspect}. Valid adapters are: #{adapters}" raise UnknownAdapterError, , e.backtrace end |
.new(*args) ⇒ Object
:nocov:
11 12 13 14 15 |
# File 'lib/active_model_serializers/adapter.rb', line 11 def new(*args) fail ArgumentError, 'Adapters inherit from Adapter::Base.' \ "Adapter.new called with args: '#{args.inspect}', from" \ "'caller[0]'." end |
.register(name, klass = name) ⇒ Object
Note:
The registered name strips out ‘ActiveModelSerializers::Adapter::’ so that registering ‘ActiveModelSerializers::Adapter::Json’ and ‘Json’ will both register as ‘json’.
Adds an adapter ‘klass’ with ‘name’ to the ‘adapter_map’ Names are stringified and underscored
52 53 54 55 56 |
# File 'lib/active_model_serializers/adapter.rb', line 52 def register(name, klass = name) name = name.to_s.gsub(/\AActiveModelSerializers::Adapter::/, ''.freeze) adapter_map[name.underscore] = klass self end |
.registered_name(adapter_class) ⇒ Object
58 59 60 |
# File 'lib/active_model_serializers/adapter.rb', line 58 def registered_name(adapter_class) ADAPTER_MAP.key adapter_class end |