Sha256: 082604bc9b00a60b4933761a3b021b64473ea7f6fec8249525f39c6d52db4623

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

module Modis
  module Finder
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def find(*ids)
        models = find_all(ids)
        ids.count == 1 ? models.first : models
      end

      def all
        records = Modis.with_connection do |redis|
          ids = redis.smembers(key_for(:all))
          redis.pipelined do
            ids.map { |id| record_for(redis, id) }
          end
        end

        records_to_models(records)
      end

      def attributes_for(redis, id)
        raise RecordNotFound, "Couldn't find #{name} without an ID" if id.nil?

        attributes = deserialize(record_for(redis, id))

        unless attributes['id'].present?
          raise RecordNotFound, "Couldn't find #{name} with id=#{id}"
        end

        attributes
      end

      def find_all(ids)
        raise RecordNotFound, "Couldn't find #{name} without an ID" if ids.empty?

        records = Modis.with_connection do |redis|
          blk = proc { |id| record_for(redis, id) }
          ids.count == 1 ? ids.map(&blk) : redis.pipelined { ids.map(&blk) }
        end

        models = records_to_models(records)

        if models.count < ids.count
          missing = ids - models.map(&:id)
          raise RecordNotFound, "Couldn't find #{name} with id=#{missing.first}"
        end

        models
      end

      private

      def records_to_models(records)
        records.map do |record|
          model_for(deserialize(record)) unless record.blank?
        end.compact
      end

      def model_for(attributes)
        cls = model_class(attributes)
        return unless self == cls || cls < self
        cls.new(attributes, new_record: false)
      end

      def record_for(redis, id)
        key = sti_child? ? sti_base_key_for(id) : key_for(id)
        redis.hgetall(key)
      end

      def model_class(record)
        return self if record["type"].blank?
        record["type"].constantize
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
modis-1.4.2 lib/modis/finder.rb