Class: ActiveModel::Serializer

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Includes:
Associations, Configuration
Defined in:
lib/active_model/serializer.rb,
lib/active_model/serializer/adapter.rb,
lib/active_model/serializer/version.rb,
lib/active_model/serializer/fieldset.rb,
lib/active_model/serializer/reflection.rb,
lib/active_model/serializer/association.rb,
lib/active_model/serializer/associations.rb,
lib/active_model/serializer/configuration.rb,
lib/active_model/serializer/array_serializer.rb,
lib/active_model/serializer/has_one_reflection.rb,
lib/active_model/serializer/has_many_reflection.rb,
lib/active_model/serializer/singular_reflection.rb,
lib/active_model/serializer/belongs_to_reflection.rb,
lib/active_model/serializer/collection_reflection.rb

Defined Under Namespace

Modules: Associations, Configuration, Lint, Utils Classes: Adapter, ArraySerializer, Association, BelongsToReflection, CollectionReflection, Fieldset, HasManyReflection, HasOneReflection, Reflection, SingularReflection

Constant Summary collapse

CALLER_FILE =

Matches

"c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
AND
"/c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
AS
c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb
/
  \A       # start of string
  \S+      # one or more non-spaces
  (?=      # stop previous match when
    :\d+     # a colon is followed by one or more digits
    :in      # followed by a colon followed by in
  )
/x
VERSION =
"0.10.11"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations

#associations

Constructor Details

#initialize(object, options = {}) ⇒ Serializer

Returns a new instance of Serializer.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/active_model/serializer.rb', line 109

def initialize(object, options = {})
  @object = object
  @options = options
  @root = options[:root]
  @meta = options[:meta]
  @meta_key = options[:meta_key]
  @scope = options[:scope]

  scope_name = options[:scope_name]
  if scope_name && !respond_to?(scope_name)
    self.class.class_eval do
      define_method scope_name, lambda { scope }
    end
  end
end

Class Attribute Details

._attributesObject

Returns the value of attribute _attributes.



33
34
35
# File 'lib/active_model/serializer.rb', line 33

def _attributes
  @_attributes
end

._attributes_keysObject

Returns the value of attribute _attributes_keys.



34
35
36
# File 'lib/active_model/serializer.rb', line 34

def _attributes_keys
  @_attributes_keys
end

._cacheObject

Returns the value of attribute _cache.



35
36
37
# File 'lib/active_model/serializer.rb', line 35

def _cache
  @_cache
end

._cache_digestObject

Returns the value of attribute _cache_digest.



41
42
43
# File 'lib/active_model/serializer.rb', line 41

def _cache_digest
  @_cache_digest
end

._cache_exceptObject

Returns the value of attribute _cache_except.



39
40
41
# File 'lib/active_model/serializer.rb', line 39

def _cache_except
  @_cache_except
end

._cache_keyObject

Returns the value of attribute _cache_key.



37
38
39
# File 'lib/active_model/serializer.rb', line 37

def _cache_key
  @_cache_key
end

._cache_onlyObject

Returns the value of attribute _cache_only.



38
39
40
# File 'lib/active_model/serializer.rb', line 38

def _cache_only
  @_cache_only
end

._cache_optionsObject

Returns the value of attribute _cache_options.



40
41
42
# File 'lib/active_model/serializer.rb', line 40

def _cache_options
  @_cache_options
end

._fragmentedObject

Returns the value of attribute _fragmented.



36
37
38
# File 'lib/active_model/serializer.rb', line 36

def _fragmented
  @_fragmented
end

Instance Attribute Details

#metaObject

Returns the value of attribute meta.



107
108
109
# File 'lib/active_model/serializer.rb', line 107

def meta
  @meta
end

#meta_keyObject

Returns the value of attribute meta_key.



107
108
109
# File 'lib/active_model/serializer.rb', line 107

def meta_key
  @meta_key
end

#objectObject

Returns the value of attribute object.



107
108
109
# File 'lib/active_model/serializer.rb', line 107

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



156
157
158
# File 'lib/active_model/serializer.rb', line 156

def options
  @options
end

#rootObject

Returns the value of attribute root.



107
108
109
# File 'lib/active_model/serializer.rb', line 107

def root
  @root
end

#scopeObject

Returns the value of attribute scope.



107
108
109
# File 'lib/active_model/serializer.rb', line 107

def scope
  @scope
end

Class Method Details

.adapterObject



99
100
101
# File 'lib/active_model/serializer.rb', line 99

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

.attribute(attr, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/active_model/serializer.rb', line 63

def self.attribute(attr, options = {})
  key = options.fetch(:key, attr)
  @_attributes_keys[attr] = { key: key } if key != attr
  @_attributes << key unless @_attributes.include?(key)

  ActiveModelSerializers.silence_warnings do
    define_method key do
      object.read_attribute_for_serialization(attr)
    end unless (key != :id && method_defined?(key)) || _fragmented.respond_to?(attr)
  end
end

.attributes(*attrs) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_model/serializer.rb', line 51

def self.attributes(*attrs)
  attrs = attrs.first if attrs.first.class == Array
  @_attributes.concat attrs
  @_attributes.uniq!

  attrs.each do |attr|
    define_method attr do
      object && object.read_attribute_for_serialization(attr)
    end unless method_defined?(attr) || _fragmented.respond_to?(attr)
  end
end

.cache(options = {}) ⇒ Object

Enables a serializer to be automatically cached



80
81
82
83
84
85
86
# File 'lib/active_model/serializer.rb', line 80

def self.cache(options = {})
  @_cache = ActionController::Base.cache_store if Rails.configuration.action_controller.perform_caching
  @_cache_key = options.delete(:key)
  @_cache_only = options.delete(:only)
  @_cache_except = options.delete(:except)
  @_cache_options = (options.empty?) ? nil : options
end

.digest_caller_file(caller_line) ⇒ Object



150
151
152
153
154
# File 'lib/active_model/serializer.rb', line 150

def self.digest_caller_file(caller_line)
  serializer_file_path = caller_line[CALLER_FILE]
  serializer_file_contents = IO.read(serializer_file_path)
  Digest::MD5.hexdigest(serializer_file_contents)
end

.fragmented(serializer) ⇒ Object



75
76
77
# File 'lib/active_model/serializer.rb', line 75

def self.fragmented(serializer)
  @_fragmented = serializer
end

.get_serializer_for(klass) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/active_model/serializer.rb', line 158

def self.get_serializer_for(klass)
  serializers_cache.fetch_or_store(klass) do
    serializer_class_name = "#{klass.name}Serializer"
    serializer_class = serializer_class_name.safe_constantize

    if serializer_class
      serializer_class
    elsif klass.superclass
      get_serializer_for(klass.superclass)
    end
  end
end

.inherited(base) ⇒ Object



44
45
46
47
48
49
# File 'lib/active_model/serializer.rb', line 44

def self.inherited(base)
  base._attributes = self._attributes.try(:dup) || []
  base._attributes_keys = self._attributes_keys.try(:dup) || {}
  base._cache_digest = digest_caller_file(caller.first)
  super
end

.root_nameObject



103
104
105
# File 'lib/active_model/serializer.rb', line 103

def self.root_name
  name.demodulize.underscore.sub(/_serializer$/, '') if name
end

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



88
89
90
91
92
93
94
95
96
# File 'lib/active_model/serializer.rb', line 88

def self.serializer_for(resource, options = {})
  if resource.respond_to?(:serializer_class)
    resource.serializer_class
  elsif resource.respond_to?(:to_ary)
    config.array_serializer
  else
    options.fetch(:serializer, get_serializer_for(resource.class))
  end
end

.serializers_cacheObject



146
147
148
# File 'lib/active_model/serializer.rb', line 146

def self.serializers_cache
  @serializers_cache ||= ThreadSafe::Cache.new
end

Instance Method Details

#attributes(options = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/active_model/serializer.rb', line 129

def attributes(options = {})
  attributes =
    if options[:fields]
      self.class._attributes & options[:fields]
    else
      self.class._attributes.dup
    end

  attributes.each_with_object({}) do |name, hash|
    unless self.class._fragmented
      hash[name] = send(name)
    else
      hash[name] = self.class._fragmented.public_send(name)
    end
  end
end

#json_keyObject



125
126
127
# File 'lib/active_model/serializer.rb', line 125

def json_key
  @root || object.class.model_name.to_s.underscore
end