Module: FastJsonapi::ObjectSerializer
- Extended by:
- ActiveSupport::Concern
- Includes:
- SerializationCore
- Defined in:
- lib/fast_jsonapi/object_serializer.rb,
lib/fast_jsonapi/instrumentation/serialized_json.rb,
lib/fast_jsonapi/instrumentation/serializable_hash.rb
Constant Summary collapse
- SERIALIZABLE_HASH_NOTIFICATION =
'render.fast_jsonapi.serializable_hash'.freeze
- SERIALIZED_JSON_NOTIFICATION =
'render.fast_jsonapi.serialized_json'.freeze
Instance Method Summary collapse
- #hash_for_collection ⇒ Object
- #hash_for_one_record ⇒ Object
- #initialize(resource, options = {}) ⇒ Object
- #serializable_hash ⇒ Object
- #serializable_hash_without_instrumentation ⇒ Object
- #serialized_json ⇒ Object
- #serialized_json_without_instrumentation ⇒ Object
- #to_hash ⇒ Object
Instance Method Details
#hash_for_collection ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fast_jsonapi/object_serializer.rb', line 45 def hash_for_collection serializable_hash = {} data = [] included = [] @resource.each do |record| data << self.class.record_hash(record) included.concat self.class.get_included_records(record, @includes, @known_included_objects) if @includes.present? end serializable_hash[:data] = data serializable_hash[:included] = included if @includes.present? serializable_hash[:meta] = @meta if @meta.present? serializable_hash end |
#hash_for_one_record ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/fast_jsonapi/object_serializer.rb', line 34 def hash_for_one_record serializable_hash = { data: nil } serializable_hash[:meta] = @meta if @meta.present? return serializable_hash unless @resource serializable_hash[:data] = self.class.record_hash(@resource) serializable_hash[:included] = self.class.get_included_records(@resource, @includes, @known_included_objects) if @includes.present? serializable_hash end |
#initialize(resource, options = {}) ⇒ Object
21 22 23 24 25 |
# File 'lib/fast_jsonapi/object_serializer.rb', line 21 def initialize(resource, = {}) () @resource = resource end |
#serializable_hash ⇒ Object
27 28 29 30 31 |
# File 'lib/fast_jsonapi/object_serializer.rb', line 27 def serializable_hash return hash_for_collection if is_collection?(@resource) hash_for_one_record end |
#serializable_hash_without_instrumentation ⇒ Object
6 7 8 9 10 |
# File 'lib/fast_jsonapi/instrumentation/serializable_hash.rb', line 6 def serializable_hash return hash_for_collection if is_collection?(@resource) hash_for_one_record end |
#serialized_json ⇒ Object
61 62 63 |
# File 'lib/fast_jsonapi/object_serializer.rb', line 61 def serialized_json self.class.to_json(serializable_hash) end |
#serialized_json_without_instrumentation ⇒ Object
6 7 8 |
# File 'lib/fast_jsonapi/instrumentation/serialized_json.rb', line 6 def serialized_json self.class.to_json(serializable_hash) end |
#to_hash ⇒ Object
32 33 34 35 36 |
# File 'lib/fast_jsonapi/object_serializer.rb', line 32 def serializable_hash return hash_for_collection if is_collection?(@resource) hash_for_one_record end |