lib/couch_potato.rb in couch_potato-1.7.0 vs lib/couch_potato.rb in couch_potato-1.7.1
- old
+ new
@@ -10,11 +10,11 @@
Config = Struct.new(:database_host, :database_name, :digest_view_names,
:split_design_documents_per_view, :default_language).new
Config.split_design_documents_per_view = false
Config.digest_view_names = false
Config.default_language = :javascript
- Config.database_host = "http://127.0.0.1:5984"
+ Config.database_host = 'http://127.0.0.1:5984'
class NotFound < StandardError; end
class Conflict < StandardError; end
# returns all the classes that include the CouchPotato::Persistence module
@@ -23,36 +23,36 @@
@models
end
# Returns a database instance which you can then use to create objects and query views. You have to set the CouchPotato::Config.database_name before this works.
def self.database
- @@__database ||= Database.new(self.couchrest_database)
+ Thread.current[:__couch_potato_database] ||= Database.new(couchrest_database)
end
# Returns the underlying CouchRest database object if you want low level access to your CouchDB. You have to set the CouchPotato::Config.database_name before this works.
def self.couchrest_database
- @@__couchrest_database ||= CouchRest.database(full_url_to_database(CouchPotato::Config.database_name, CouchPotato::Config.database_host))
+ Thread.current[:__couchrest_database] ||= CouchRest.database(full_url_to_database(CouchPotato::Config.database_name, CouchPotato::Config.database_host))
end
# Returns a specific database instance
def self.use(database_name)
- @@__databases ||= {}
- @@__databases["#{database_name}"] = Database.new(couchrest_database_for_name!(database_name)) unless @@__databases["#{database_name}"]
- @@__databases["#{database_name}"]
+ Thread.current[:__couch_potato_databases] ||= {}
+ Thread.current[:__couch_potato_databases][database_name] = Database.new(couchrest_database_for_name!(database_name)) unless Thread.current[:__couch_potato_databases][database_name]
+ Thread.current[:__couch_potato_databases][database_name]
end
# Executes a block of code and yields a datbase with the given name.
#
# example:
# CouchPotato.with_database('couch_customer') do |couch|
# couch.save @customer
# end
#
def self.with_database(database_name)
- @@__databases ||= {}
- @@__databases["#{database_name}"] = Database.new(couchrest_database_for_name(database_name)) unless @@__databases["#{database_name}"]
- yield(@@__databases["#{database_name}"])
+ Thread.current[:__couch_potato_databases] ||= {}
+ Thread.current[:__couch_potato_databases][database_name] = Database.new(couchrest_database_for_name(database_name)) unless Thread.current[:__couch_potato_databases][database_name]
+ yield Thread.current[:__couch_potato_databases][database_name]
end
# Returns a CouchRest-Database for directly accessing that functionality.
def self.couchrest_database_for_name(database_name)
CouchRest.database(full_url_to_database(database_name, CouchPotato::Config.database_host))
@@ -61,12 +61,10 @@
# Creates a CouchRest-Database for directly accessing that functionality.
def self.couchrest_database_for_name!(database_name)
CouchRest.database!(full_url_to_database(database_name))
end
- private
-
- def self.full_url_to_database(database_name=CouchPotato::Config.database_name, database_host = CouchPotato::Config.database_host)
+ def self.full_url_to_database(database_name = CouchPotato::Config.database_name, database_host = CouchPotato::Config.database_host)
raise('No Database configured. Set CouchPotato::Config.database_name') unless database_name
if database_name.match(%r{https?://})
database_name
else
"#{database_host}/#{database_name}"