Sha256: 61708b4e3f05e92ebdea1df4a08e50c5a79b4358a08fcad0588dcbfa73b3bc22

Contents?: true

Size: 850 Bytes

Versions: 3

Compression:

Stored size: 850 Bytes

Contents

class MockDB
  def collection(name)
    MockCollection.new(:name => name)
  end
end

class MockCursor
  include Enumerable
  attr_accessor :objs
  include FromHash
  def each(&b)
    objs.each(&b)
  end
  def count
    objs.size
  end
end

class MockCollection
  include MongoPersistCollection
  attr_accessor :name
  include FromHash
  fattr(:objs) { {} }
  def remove 
    objs!
  end
  def save(doc)
    doc['_id'] = rand(10000000000)
    objs[doc['_id']] = doc
    doc['_id']
  end
  def update(ops,new_doc)
    find_raw(ops).each do |doc|
      objs[doc['_id']] = new_doc.merge('_id' => doc['_id'])
    end
  end
  def find_raw(ops={})
    objs.values.select do |h|
      ops.all? { |k,v| v === h[k.to_s] }
    end
  end
  def find(ops={})
    MockCursor.new(:objs => find_raw(ops))
  end
  def find_one(ops={})
    find_raw(ops).first
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongo_persist-0.2.1 spec/mocks.rb
mongo_persist-0.2.0 spec/mocks.rb
mongo_persist-0.1.0 spec/mocks.rb