Sha256: 35462d3cb1bf30ff713ab892392ae8aa572b5a440af95795fd8db37cbb83b536
Contents?: true
Size: 1.83 KB
Versions: 3
Compression:
Stored size: 1.83 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) model_class(attributes).new(attributes, new_record: false) end def record_for(redis, id) redis.hgetall(key_for(id)) end def model_class(record) return self if record["type"].blank? record["type"].constantize end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
modis-1.4.1-java | lib/modis/finder.rb |
modis-1.4.1 | lib/modis/finder.rb |
modis-1.4.0 | lib/modis/finder.rb |