Module: Modis::Finder::ClassMethods
- Defined in:
- lib/modis/finder.rb
Instance Method Summary collapse
Instance Method Details
#all ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/modis/finder.rb', line 13 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 |
#attributes_for(redis, id) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/modis/finder.rb', line 24 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 |
#find(*ids) ⇒ Object
8 9 10 11 |
# File 'lib/modis/finder.rb', line 8 def find(*ids) models = find_all(ids) ids.count == 1 ? models.first : models end |
#find_all(ids) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/modis/finder.rb', line 36 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 |