Class: ActiveModel::Serializer::Adapter

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/active_model/serializer/adapter.rb

Direct Known Subclasses

Json, JsonApi, Null

Defined Under Namespace

Classes: FlattenJson, FragmentCache, Json, JsonApi, Null

Constant Summary collapse

UnknownAdapterError =
Class.new(ArgumentError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @serializer = serializer
  @options = options
end

Instance Attribute Details

#serializerObject (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

See Also:



21
22
23
# File 'lib/active_model/serializer/adapter.rb', line 21

def self.adapter_class(adapter)
  ActiveModel::Serializer::Adapter.lookup(adapter)
end

.adapter_mapObject



29
30
31
# File 'lib/active_model/serializer/adapter.rb', line 29

def adapter_map
  ADAPTER_MAP
end

.adaptersArray<Symbol>

Returns list of adapter names.

Returns:

  • (Array<Symbol>)

    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, options = {})
  override = options.delete(:adapter)
  klass = override ? adapter_class(override) : ActiveModel::Serializer.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.



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.

Parameters:

  • adapter (String, Symbol, Class)

    name to fetch adapter by

Returns:

Raises:



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

.register(name, klass) ⇒ Object

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)

    of the registered adapter

  • klass (Class)
    • adapter class itself



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(options = nil)
  hash = serializable_hash(options)
  include_meta(hash)
  hash
end

#fragment_cache(*args) ⇒ Object

Raises:

  • (NotImplementedError)


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

Raises:

  • (NotImplementedError)


90
91
92
# File 'lib/active_model/serializer/adapter.rb', line 90

def serializable_hash(options = nil)
  raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
end