Module: FastJsonapi::ObjectSerializer
- Extended by:
- ActiveSupport::Concern
- Includes:
- SerializationCore
- Defined in:
- lib/fast_jsonapi/object_serializer.rb
Instance Method Summary collapse
- #hash_for_multiple_records(serializable_hash) ⇒ Object
- #hash_for_one_record(serializable_hash) ⇒ Object
- #initialize(resource, options = {}) ⇒ Object
- #serializable_hash ⇒ Object
- #serialized_json ⇒ Object
Instance Method Details
#hash_for_multiple_records(serializable_hash) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/fast_jsonapi/object_serializer.rb', line 65 def hash_for_multiple_records(serializable_hash) data = [] included = [] @records.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 end |
#hash_for_one_record(serializable_hash) ⇒ Object
59 60 61 62 63 |
# File 'lib/fast_jsonapi/object_serializer.rb', line 59 def hash_for_one_record(serializable_hash) serializable_hash[:data] = self.class.record_hash(@record) serializable_hash[:included] = self.class.get_included_records(@record, @includes, @known_included_objects) if @includes.present? serializable_hash end |
#initialize(resource, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/fast_jsonapi/object_serializer.rb', line 36 def initialize(resource, = {}) if .present? @meta_tags = [:meta] @includes = [:include].delete_if(&:blank?) if [:include].present? self.class.has_permitted_includes(@includes) if @includes.present? @known_included_objects = {} # keep track of inc objects that have already been serialized end # @records if enumerables like Array, ActiveRecord::Relation but if Struct just make it a @record if resource.respond_to?(:each) && !resource.respond_to?(:each_pair) @records = resource else @record = resource end end |
#serializable_hash ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/fast_jsonapi/object_serializer.rb', line 51 def serializable_hash serializable_hash = { data: nil } serializable_hash[:meta] = @meta_tags if @meta_tags.present? return hash_for_one_record(serializable_hash) if @record return hash_for_multiple_records(serializable_hash) if @records serializable_hash end |
#serialized_json ⇒ Object
77 78 79 |
# File 'lib/fast_jsonapi/object_serializer.rb', line 77 def serialized_json self.class.to_json(serializable_hash) end |