Class: ActiveModelSerializers::SerializableResource
- Inherits:
-
Object
- Object
- ActiveModelSerializers::SerializableResource
show all
- Includes:
- Logging
- Defined in:
- lib/active_model_serializers/serializable_resource.rb
Constant Summary
collapse
- ADAPTER_OPTION_KEYS =
Set.new([:include, :fields, :adapter, :meta, :meta_key, :links, :serialization_context, :key_transform])
Constants included
from Logging
Logging::RENDER_EVENT
Instance Method Summary
collapse
Methods included from Logging
#notify_render, #notify_render_payload
Constructor Details
#initialize(resource, options = {}) ⇒ Object
Primary interface to composing a resource with a serializer and adapter.
17
18
19
20
21
|
# File 'lib/active_model_serializers/serializable_resource.rb', line 17
def initialize(resource, options = {})
@resource = resource
@adapter_opts = options.select { |k, _| ADAPTER_OPTION_KEYS.include? k }
@serializer_opts = options.reject { |k, _| ADAPTER_OPTION_KEYS.include? k }
end
|
Instance Method Details
#adapter ⇒ Object
Also known as:
adapter_instance
NOTE: if no adapter is available, returns the resource itself. (i.e. adapter is a no-op)
36
37
38
|
# File 'lib/active_model_serializers/serializable_resource.rb', line 36
def adapter
@adapter ||= find_adapter
end
|
#find_adapter ⇒ Object
41
42
43
44
45
46
47
|
# File 'lib/active_model_serializers/serializable_resource.rb', line 41
def find_adapter
return resource unless serializer?
adapter = catch :no_serializer do
ActiveModelSerializers::Adapter.create(serializer_instance, adapter_opts)
end
adapter || resource
end
|
#serialization_scope ⇒ Object
27
28
29
|
# File 'lib/active_model_serializers/serializable_resource.rb', line 27
def serialization_scope
serializer_opts[:scope]
end
|
#serialization_scope=(scope) ⇒ Object
23
24
25
|
# File 'lib/active_model_serializers/serializable_resource.rb', line 23
def serialization_scope=(scope)
serializer_opts[:scope] = scope
end
|
#serialization_scope_name=(scope_name) ⇒ Object
31
32
33
|
# File 'lib/active_model_serializers/serializable_resource.rb', line 31
def serialization_scope_name=(scope_name)
serializer_opts[:scope_name] = scope_name
end
|
#serializer ⇒ Object
Also known as:
serializer_class
Get serializer either explicitly :serializer or implicitly from resource Remove :serializer key from serializer_opts Remove :each_serializer if present and set as :serializer key
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/active_model_serializers/serializable_resource.rb', line 56
def serializer
@serializer ||=
begin
@serializer = serializer_opts.delete(:serializer)
@serializer ||= ActiveModel::Serializer.serializer_for(resource, serializer_opts)
if serializer_opts.key?(:each_serializer)
serializer_opts[:serializer] = serializer_opts.delete(:each_serializer)
end
@serializer
end
end
|
#serializer? ⇒ Boolean
76
77
78
|
# File 'lib/active_model_serializers/serializable_resource.rb', line 76
def serializer?
use_adapter? && !serializer.nil?
end
|
#serializer_instance ⇒ Object
49
50
51
|
# File 'lib/active_model_serializers/serializable_resource.rb', line 49
def serializer_instance
@serializer_instance ||= serializer.new(resource, serializer_opts)
end
|
#use_adapter? ⇒ Boolean
True when no explicit adapter given, or explicit appear is truthy (non-nil) False when explicit adapter is falsy (nil or false)
72
73
74
|
# File 'lib/active_model_serializers/serializable_resource.rb', line 72
def use_adapter?
!(adapter_opts.key?(:adapter) && !adapter_opts[:adapter])
end
|