Class: ActiveModel::Serializer
- Inherits:
-
Object
- Object
- ActiveModel::Serializer
- 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
-
._attributes ⇒ Object
Returns the value of attribute _attributes.
-
._attributes_keys ⇒ Object
Returns the value of attribute _attributes_keys.
-
._cache ⇒ Object
Returns the value of attribute _cache.
-
._cache_digest ⇒ Object
Returns the value of attribute _cache_digest.
-
._cache_except ⇒ Object
Returns the value of attribute _cache_except.
-
._cache_key ⇒ Object
Returns the value of attribute _cache_key.
-
._cache_only ⇒ Object
Returns the value of attribute _cache_only.
-
._cache_options ⇒ Object
Returns the value of attribute _cache_options.
-
._fragmented ⇒ Object
Returns the value of attribute _fragmented.
Instance Attribute Summary collapse
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#meta_key ⇒ Object
Returns the value of attribute meta_key.
-
#object ⇒ Object
Returns the value of attribute object.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#root ⇒ Object
Returns the value of attribute root.
-
#scope ⇒ Object
Returns the value of attribute scope.
Class Method Summary collapse
- .adapter ⇒ Object
- .attribute(attr, options = {}) ⇒ Object
- .attributes(*attrs) ⇒ Object
-
.cache(options = {}) ⇒ Object
Enables a serializer to be automatically cached.
- .digest_caller_file(caller_line) ⇒ Object
- .fragmented(serializer) ⇒ Object
- .get_serializer_for(klass) ⇒ Object
- .inherited(base) ⇒ Object
- .root_name ⇒ Object
- .serializer_for(resource, options = {}) ⇒ Object
- .serializers_cache ⇒ Object
Instance Method Summary collapse
- #attributes(options = {}) ⇒ Object
-
#initialize(object, options = {}) ⇒ Serializer
constructor
A new instance of Serializer.
- #json_key ⇒ Object
Methods included from 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, = {}) @object = object @options = @root = [:root] @meta = [:meta] @meta_key = [:meta_key] @scope = [:scope] scope_name = [: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
._attributes ⇒ Object
Returns the value of attribute _attributes.
33 34 35 |
# File 'lib/active_model/serializer.rb', line 33 def _attributes @_attributes end |
._attributes_keys ⇒ Object
Returns the value of attribute _attributes_keys.
34 35 36 |
# File 'lib/active_model/serializer.rb', line 34 def _attributes_keys @_attributes_keys end |
._cache ⇒ Object
Returns the value of attribute _cache.
35 36 37 |
# File 'lib/active_model/serializer.rb', line 35 def _cache @_cache end |
._cache_digest ⇒ Object
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_except ⇒ Object
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_key ⇒ Object
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_only ⇒ Object
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_options ⇒ Object
Returns the value of attribute _cache_options.
40 41 42 |
# File 'lib/active_model/serializer.rb', line 40 def @_cache_options end |
._fragmented ⇒ Object
Returns the value of attribute _fragmented.
36 37 38 |
# File 'lib/active_model/serializer.rb', line 36 def _fragmented @_fragmented end |
Instance Attribute Details
#meta ⇒ Object
Returns the value of attribute meta.
107 108 109 |
# File 'lib/active_model/serializer.rb', line 107 def @meta end |
#meta_key ⇒ Object
Returns the value of attribute meta_key.
107 108 109 |
# File 'lib/active_model/serializer.rb', line 107 def @meta_key end |
#object ⇒ Object
Returns the value of attribute object.
107 108 109 |
# File 'lib/active_model/serializer.rb', line 107 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
156 157 158 |
# File 'lib/active_model/serializer.rb', line 156 def @options end |
#root ⇒ Object
Returns the value of attribute root.
107 108 109 |
# File 'lib/active_model/serializer.rb', line 107 def root @root end |
#scope ⇒ Object
Returns the value of attribute scope.
107 108 109 |
# File 'lib/active_model/serializer.rb', line 107 def scope @scope end |
Class Method Details
.adapter ⇒ Object
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, = {}) key = .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( = {}) @_cache = ActionController::Base.cache_store if Rails.configuration.action_controller.perform_caching @_cache_key = .delete(:key) @_cache_only = .delete(:only) @_cache_except = .delete(:except) @_cache_options = (.empty?) ? nil : 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_name ⇒ Object
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, = {}) if resource.respond_to?(:serializer_class) resource.serializer_class elsif resource.respond_to?(:to_ary) config.array_serializer else .fetch(:serializer, get_serializer_for(resource.class)) end end |
.serializers_cache ⇒ Object
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( = {}) attributes = if [:fields] self.class._attributes & [: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_key ⇒ Object
125 126 127 |
# File 'lib/active_model/serializer.rb', line 125 def json_key @root || object.class.model_name.to_s.underscore end |