Sha256: 4a87518690a8c22cb1d519b2592d2b0623a48617a36f60947813b52c5a1cf3b0

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

module Quebert
  module AsyncSender
    # I'm not sure if I want to do this or build serializers per type of object...
    module ActiveRecord
      # Reference an AR with the pk
      class PersistedRecordJob < Job
        def perform(model_name, pk, meth, args)
          Support.constantize(model_name).find(pk).send(meth, *args)
        end
      end
      
      # Serialize an unpersisted AR with the attributes on the thing.
      class UnpersistedRecordJob < Job
        def perform(model_name, attrs, meth, args)
          self.class.deserialize(Support.constantize(model_name), attrs).send(meth, *args)
        end
        
        # Deal with converting an AR to/from a hash that we can send over the wire.
        def self.serialize(record)
          record.attributes.inject({}) do |hash, (attr, val)|
            hash[attr] = val
            hash
          end
        end
        
        def self.deserialize(model, attrs)
          record = model.new
          record.attributes.each do |attr, val|
            record.send("#{attr}=", attrs[attr])
          end
          record
        end
      end
      
      def self.included(base)
        base.send(:include, InstanceMethods)
        base.send(:extend, ClassMethods)
      end
      
      module InstanceMethods
        # The meat of dealing with ActiveRecord instances.
        def async_send(meth, *args)
          if self.new_record?
            UnpersistedRecordJob.enqueue(self.class.model_name, UnpersistedRecordJob.serialize(self), meth, args)
          else
            PersistedRecordJob.enqueue(self.class.model_name, id, meth, args)
          end
        end
      end
      
      # Get the model name of the Model. Can't just do class.name on this...
      module ClassMethods
        def async_send(meth, *args)
          Object::ObjectJob.enqueue(self.model_name, meth, args)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
quebert-0.0.4 lib/quebert/async_sender/active_record.rb