Class: ActiveRecord::MessagePack::Decoder
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | activerecord/lib/active_record/message_pack.rb |
Class Method Summary
- .new(entries) ⇒ Decoder constructor
Instance Method Summary
Constructor Details
.new(entries) ⇒ Decoder
# File 'activerecord/lib/active_record/message_pack.rb', line 104
def initialize(entries) @records = entries.map { |entry| build_record(entry) } @records.zip(entries) { |record, entry| resolve_cached_associations(record, entry) } end
Instance Method Details
#build_record(entry)
: (Array) -> ::ActiveRecord::Base
# File 'activerecord/lib/active_record/message_pack.rb', line 118
def build_record(entry) class_name, attributes_hash, is_new_record, * = entry klass = ActiveSupport::MessagePack::Extensions.load_class(class_name) builder = klass.attributes_builder attributes = builder.build_from_indexed_row(builder.row_from_hash(attributes_hash)) klass.allocate.init_with_attributes(attributes, is_new_record) end
#decode(ref)
[ GitHub ]# File 'activerecord/lib/active_record/message_pack.rb', line 109
def decode(ref) if ref.is_a?(Array) ref.map { |r| @records[r] } elsif ref @records[ref] end end
#resolve_cached_associations(record, entry)
[ GitHub ]# File 'activerecord/lib/active_record/message_pack.rb', line 126
def resolve_cached_associations(record, entry) i = 3 # entry == [class_name, attributes_hash, is_new_record, *associations] while i < entry.length begin record.association(entry[i]).target = decode(entry[i + 1]) rescue ActiveRecord::AssociationNotFoundError # The association no longer exists, so just skip it. end i += 2 end end