Class: ActiveModelSerializers::Adapter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model_serializers/adapter/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer, options = {}) ⇒ Base

Returns a new instance of Base.


47
48
49
50
# File 'lib/active_model_serializers/adapter/base.rb', line 47

def initialize(serializer, options = {})
  @serializer = serializer
  @instance_options = options
end

Instance Attribute Details

#instance_optionsObject (readonly)

Returns the value of attribute instance_options


45
46
47
# File 'lib/active_model_serializers/adapter/base.rb', line 45

def instance_options
  @instance_options
end

#serializerObject (readonly)

Returns the value of attribute serializer


45
46
47
# File 'lib/active_model_serializers/adapter/base.rb', line 45

def serializer
  @serializer
end

Class Method Details

.cache_keyObject


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

def self.cache_key
  @cache_key ||= ActiveModelSerializers::Adapter.registered_name(self)
end

.default_key_transformSymbol

Sets the default transform for the adapter.

Returns:

  • (Symbol)

    the default transform for the adapter


14
15
16
# File 'lib/active_model_serializers/adapter/base.rb', line 14

def self.default_key_transform
  :unaltered
end

.fragment_cache(cached_hash, non_cached_hash) ⇒ Object


41
42
43
# File 'lib/active_model_serializers/adapter/base.rb', line 41

def self.fragment_cache(cached_hash, non_cached_hash)
  non_cached_hash.merge cached_hash
end

.inherited(subclass) ⇒ Object

Automatically register adapters when subclassing


7
8
9
# File 'lib/active_model_serializers/adapter/base.rb', line 7

def self.inherited(subclass)
  ActiveModelSerializers::Adapter.register(subclass)
end

.transform(options) ⇒ Symbol

Determines the transform to use in order of precedence:

adapter option, global config, adapter default.

Parameters:

  • options (Object)

Returns:

  • (Symbol)

    the transform to use


23
24
25
26
# File 'lib/active_model_serializers/adapter/base.rb', line 23

def self.transform(options)
  return options[:key_transform] if options && options[:key_transform]
  ActiveModelSerializers.config.key_transform || default_key_transform
end

.transform_key_casing!(value, options) ⇒ Symbol

Transforms the casing of the supplied value.

Parameters:

  • value (Object)

    the value to be transformed

  • options (Object)

    serializable resource options

Returns:

  • (Symbol)

    the default transform for the adapter


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

def self.transform_key_casing!(value, options)
  CaseTransform.send(transform(options), value)
end

Instance Method Details

#as_json(options = nil) ⇒ Object


58
59
60
# File 'lib/active_model_serializers/adapter/base.rb', line 58

def as_json(options = nil)
  serializable_hash(options)
end

#cache_keyObject


62
63
64
# File 'lib/active_model_serializers/adapter/base.rb', line 62

def cache_key
  self.class.cache_key
end

#fragment_cache(cached_hash, non_cached_hash) ⇒ Object


66
67
68
# File 'lib/active_model_serializers/adapter/base.rb', line 66

def fragment_cache(cached_hash, non_cached_hash)
  self.class.fragment_cache(cached_hash, non_cached_hash)
end

#serializable_hash(_options = nil) ⇒ Object

Subclasses that implement this method must first call

options = serialization_options(options)

54
55
56
# File 'lib/active_model_serializers/adapter/base.rb', line 54

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