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/adapter/json.rb,
lib/active_model/serializer/adapter/null.rb,
lib/active_model/serializer/associations.rb,
lib/active_model/serializer/configuration.rb,
lib/active_model/serializer/adapter/json_api.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/adapter/flatten_json.rb,
lib/active_model/serializer/belongs_to_reflection.rb,
lib/active_model/serializer/collection_reflection.rb,
lib/active_model/serializer/adapter/fragment_cache.rb,
lib/active_model/serializer/adapter/json/fragment_cache.rb,
lib/active_model/serializer/adapter/json_api/fragment_cache.rb,
lib/active_model/serializer/adapter/json_api/pagination_links.rb
Defined Under Namespace
Modules: Associations, Configuration, Lint 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.8"
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.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/active_model/serializer.rb', line 118 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.
32 33 34 |
# File 'lib/active_model/serializer.rb', line 32 def _attributes @_attributes end |
._attributes_keys ⇒ Object
Returns the value of attribute _attributes_keys.
33 34 35 |
# File 'lib/active_model/serializer.rb', line 33 def _attributes_keys @_attributes_keys end |
._cache ⇒ Object
Returns the value of attribute _cache.
34 35 36 |
# File 'lib/active_model/serializer.rb', line 34 def _cache @_cache end |
._cache_digest ⇒ Object
Returns the value of attribute _cache_digest.
40 41 42 |
# File 'lib/active_model/serializer.rb', line 40 def _cache_digest @_cache_digest end |
._cache_except ⇒ Object
Returns the value of attribute _cache_except.
38 39 40 |
# File 'lib/active_model/serializer.rb', line 38 def _cache_except @_cache_except end |
._cache_key ⇒ Object
Returns the value of attribute _cache_key.
36 37 38 |
# File 'lib/active_model/serializer.rb', line 36 def _cache_key @_cache_key end |
._cache_only ⇒ Object
Returns the value of attribute _cache_only.
37 38 39 |
# File 'lib/active_model/serializer.rb', line 37 def _cache_only @_cache_only end |
._cache_options ⇒ Object
Returns the value of attribute _cache_options.
39 40 41 |
# File 'lib/active_model/serializer.rb', line 39 def @_cache_options end |
._fragmented ⇒ Object
Returns the value of attribute _fragmented.
35 36 37 |
# File 'lib/active_model/serializer.rb', line 35 def _fragmented @_fragmented end |
Instance Attribute Details
#meta ⇒ Object
Returns the value of attribute meta.
116 117 118 |
# File 'lib/active_model/serializer.rb', line 116 def @meta end |
#meta_key ⇒ Object
Returns the value of attribute meta_key.
116 117 118 |
# File 'lib/active_model/serializer.rb', line 116 def @meta_key end |
#object ⇒ Object
Returns the value of attribute object.
116 117 118 |
# File 'lib/active_model/serializer.rb', line 116 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
165 166 167 |
# File 'lib/active_model/serializer.rb', line 165 def @options end |
#root ⇒ Object
Returns the value of attribute root.
116 117 118 |
# File 'lib/active_model/serializer.rb', line 116 def root @root end |
#scope ⇒ Object
Returns the value of attribute scope.
116 117 118 |
# File 'lib/active_model/serializer.rb', line 116 def scope @scope end |
Class Method Details
.adapter ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/active_model/serializer.rb', line 97 def self.adapter adapter_class = case config.adapter when Symbol ActiveModel::Serializer::Adapter.adapter_class(config.adapter) when Class config.adapter end unless adapter_class valid_adapters = Adapter.constants.map { |klass| ":#{klass.to_s.downcase}" } raise ArgumentError, "Unknown adapter: #{config.adapter}. Valid adapters are: #{valid_adapters}" end adapter_class end |
.attribute(attr, options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/active_model/serializer.rb', line 62 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
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/active_model/serializer.rb', line 50 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
79 80 81 82 83 84 85 |
# File 'lib/active_model/serializer.rb', line 79 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
159 160 161 162 163 |
# File 'lib/active_model/serializer.rb', line 159 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
74 75 76 |
# File 'lib/active_model/serializer.rb', line 74 def self.fragmented(serializer) @_fragmented = serializer end |
.get_serializer_for(klass) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/active_model/serializer.rb', line 167 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
43 44 45 46 47 48 |
# File 'lib/active_model/serializer.rb', line 43 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
112 113 114 |
# File 'lib/active_model/serializer.rb', line 112 def self.root_name name.demodulize.underscore.sub(/_serializer$/, '') if name end |
.serializer_for(resource, options = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/active_model/serializer.rb', line 87 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
155 156 157 |
# File 'lib/active_model/serializer.rb', line 155 def self.serializers_cache @serializers_cache ||= ThreadSafe::Cache.new end |
Instance Method Details
#attributes(options = {}) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/active_model/serializer.rb', line 138 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
134 135 136 |
# File 'lib/active_model/serializer.rb', line 134 def json_key @root || object.class.model_name.to_s.underscore end |