Module: Modis::Finders::ClassMethods

Defined in:
lib/modis/finders.rb

Instance Method Summary collapse

Instance Method Details

#allObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/modis/finders.rb', line 13

def all
  records = []

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

  records.map do |record|
    klass = model_class(record)
    klass.new(record, new_record: false)
  end
end

#attributes_for(id) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/modis/finders.rb', line 29

def attributes_for(id)
  if id.nil?
    raise RecordNotFound, "Couldn't find #{name} without an ID"
  end
  values = Modis.with_connection { |redis| redis.hgetall(key_for(id)) }
  unless values['id'].present?
    raise RecordNotFound, "Couldn't find #{name} with id=#{id}"
  end
  values
end

#find(id) ⇒ Object



8
9
10
11
# File 'lib/modis/finders.rb', line 8

def find(id)
  record = attributes_for(id)
  model_class(record).new(record, new_record: false)
end