53 lines
964 B
Ruby
53 lines
964 B
Ruby
module TesterConnector
|
|
class Base
|
|
attr_reader :collection
|
|
|
|
def initialize
|
|
collection_name = self.class.name.demodulize.underscore.pluralize
|
|
@collection = SpecSupport.db.collection(collection_name)
|
|
@next_id = 1
|
|
end
|
|
|
|
def create(hash={})
|
|
collection.insert(create_params(hash))
|
|
if block_given?
|
|
yield
|
|
cleanup
|
|
end
|
|
end
|
|
|
|
def create_list(size=2, hash={})
|
|
size.times { create(hash) }
|
|
if block_given?
|
|
yield
|
|
cleanup
|
|
end
|
|
end
|
|
|
|
def list
|
|
collection.find().to_a
|
|
end
|
|
|
|
def show(id)
|
|
collection.find({'_id' => id}).to_a.first
|
|
end
|
|
|
|
def cleanup
|
|
collection.remove
|
|
end
|
|
|
|
private
|
|
|
|
def create_params(hash)
|
|
params = hash.dup
|
|
if params[:id]
|
|
params['_id'] = params.delete(:id)
|
|
end
|
|
unless params['_id']
|
|
params['_id'] = @next_id
|
|
@next_id += 1
|
|
end
|
|
params
|
|
end
|
|
end
|
|
end |