lib/ohm.rb in ohm-1.3.1 vs lib/ohm.rb in ohm-1.3.2
- old
+ new
@@ -143,11 +143,13 @@
module Collection
include Enumerable
def each
if block_given?
- to_a.each { |element| yield element }
+ ids.each_slice(1000) do |slice|
+ fetch(slice).each { |e| yield(e) }
+ end
else
Enumerator.new(self, :each)
end
end
@@ -501,10 +503,12 @@
#
def add(model)
db.sadd(key, model.id)
end
+ alias_method :<<, :add
+
# Remove a model directly from the set.
#
# Example:
#
# user = User.create
@@ -781,11 +785,11 @@
#
def self.with(att, val)
raise IndexNotFound unless uniques.include?(att)
id = db.hget(key[:uniques][att], val)
- id && self[id]
+ new(:id => id).load! if id
end
# Find values in indexed fields.
#
# Example:
@@ -1341,27 +1345,30 @@
#
def delete
transaction do |t|
_uniques = nil
_indices = nil
- existing = nil
+ existing_indices = nil
+ existing_uniques = nil
t.watch(*_unique_keys)
t.watch(key)
t.watch(key[:_indices]) if model.indices.any?
t.watch(key[:_uniques]) if model.uniques.any?
t.read do
- existing = _read_attributes(model.indices) if model.indices.any?
+ existing_indices = _read_attributes(model.indices) if model.indices.any?
+ existing_uniques = _read_attributes(model.uniques) if model.uniques.any?
_uniques = db.hgetall(key[:_uniques])
_indices = db.smembers(key[:_indices])
end
t.write do
_delete_uniques(_uniques)
_delete_indices(_indices)
- _delete_existing_indices(existing)
+ _delete_existing_uniques(existing_uniques)
+ _delete_existing_indices(existing_indices)
model.collections.each { |e| db.del(key[e]) }
db.srem(model.key[:all], id)
db.del(key[:counters])
db.del(key)
end