Class: ActiveRecord::MessagePack::Decoder
- Inherits:
-
Object
- Object
- ActiveRecord::MessagePack::Decoder
- Defined in:
- lib/active_record/message_pack.rb
Instance Method Summary collapse
- #build_record(entry) ⇒ Object
- #decode(ref) ⇒ Object
-
#initialize(entries) ⇒ Decoder
constructor
A new instance of Decoder.
- #resolve_cached_associations(record, entry) ⇒ Object
Constructor Details
#initialize(entries) ⇒ Decoder
Returns a new instance of Decoder.
104 105 106 107 |
# File '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) ⇒ Object
117 118 119 120 121 122 |
# File 'lib/active_record/message_pack.rb', line 117 def build_record(entry) class_name, attributes_hash, is_new_record, * = entry klass = ActiveSupport::MessagePack::Extensions.load_class(class_name) attributes = klass.attributes_builder.build_from_database(attributes_hash) klass.allocate.init_with_attributes(attributes, is_new_record) end |
#decode(ref) ⇒ Object
109 110 111 112 113 114 115 |
# File '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) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/active_record/message_pack.rb', line 124 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 |