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.



49
50
51
52
# File 'lib/active_model_serializers/adapter/base.rb', line 49

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

Instance Attribute Details

#instance_optionsObject (readonly)

Returns the value of attribute instance_options.



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

def instance_options
  @instance_options
end

#serializerObject (readonly)

Returns the value of attribute serializer.



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

def serializer
  @serializer
end

Class Method Details

.cache_keyObject



39
40
41
# File 'lib/active_model_serializers/adapter/base.rb', line 39

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



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

def self.default_key_transform
  :unaltered
end

.fragment_cache(cached_hash, non_cached_hash) ⇒ Object



43
44
45
# File 'lib/active_model_serializers/adapter/base.rb', line 43

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

.inherited(subclass) ⇒ Object

Automatically register adapters when subclassing



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

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



25
26
27
28
# File 'lib/active_model_serializers/adapter/base.rb', line 25

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



35
36
37
# File 'lib/active_model_serializers/adapter/base.rb', line 35

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

Instance Method Details

#as_json(options = nil) ⇒ Object



60
61
62
# File 'lib/active_model_serializers/adapter/base.rb', line 60

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

#cache_keyObject



64
65
66
# File 'lib/active_model_serializers/adapter/base.rb', line 64

def cache_key
  self.class.cache_key
end

#fragment_cache(cached_hash, non_cached_hash) ⇒ Object



68
69
70
# File 'lib/active_model_serializers/adapter/base.rb', line 68

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)


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

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