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

Class Method Details

.adapter_class(adapter) ⇒ Object

See Also:



27
28
29
# File 'lib/active_model_serializers/adapter.rb', line 27

def adapter_class(adapter)
  ActiveModelSerializers::Adapter.lookup(adapter)
end

.adapter_mapHash<adapter_name, adapter_class>

Returns:



32
33
34
# File 'lib/active_model_serializers/adapter.rb', line 32

def adapter_map
  ADAPTER_MAP
end

.adaptersArray<Symbol>

Returns list of adapter names.

Returns:

  • (Array<Symbol>)

    list of adapter names



37
38
39
# File 'lib/active_model_serializers/adapter.rb', line 37

def adapters
  adapter_map.keys.sort
end

.configured_adapterObject

:nocov:



16
17
18
# File 'lib/active_model_serializers/adapter.rb', line 16

def configured_adapter
  lookup(ActiveModelSerializers.config.adapter)
end

.create(resource, options = {}) ⇒ Object



20
21
22
23
24
# File 'lib/active_model_serializers/adapter.rb', line 20

def create(resource, options = {})
  override = options.delete(:adapter)
  klass = override ? adapter_class(override) : configured_adapter
  klass.new(resource, options)
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.



81
82
83
84
85
86
# File 'lib/active_model_serializers/adapter.rb', line 81

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.

Parameters:

  • adapter (String, Symbol, Class)

    name to fetch adapter by

Returns:

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/active_model_serializers/adapter.rb', line 63

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
  failure_message =
    "NameError: #{e.message}. Unknown adapter: #{adapter.inspect}. Valid adapters are: #{adapters}"
  raise UnknownAdapterError, failure_message, e.backtrace
end

.new(*args) ⇒ Object

:nocov:



9
10
11
12
13
# File 'lib/active_model_serializers/adapter.rb', line 9

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

Examples:

AMS::Adapter.register(:my_adapter, MyAdapter)

Parameters:

  • name (Symbol, String, Class)

    name of the registered adapter

  • klass (Class) (defaults to: name)

    adapter class itself, optional if name is the class



50
51
52
53
54
# File 'lib/active_model_serializers/adapter.rb', line 50

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



56
57
58
# File 'lib/active_model_serializers/adapter.rb', line 56

def registered_name(adapter_class)
  ADAPTER_MAP.key adapter_class
end