simply_couch 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/simply_couch/database_config.rb +69 -0
- data/lib/simply_couch/model/database.rb +7 -1
- data/lib/simply_couch.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9f91e1cc95b626be0fc90071cd9874ad9f0ad8d45aa086d9c2c74914db6fdd80
|
|
4
|
+
data.tar.gz: d0344763634933ddd8ddf0a3cf09edbe368c9daa7126b793f814deb59e17da02
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d364dc979b21f271d5b3acada4dacb98ba22d810462d7e698b9a4485a342eebf57ae1329058e6794c82a71bc344d40d35b973b74353ae3093baa10c711573f8a
|
|
7
|
+
data.tar.gz: '09bf0b17499c88514010940652174e99758e76fb183a852abd1d20efcdc10384b3ec168ea8063cf5fc022a82b63fff0d84f219e05dadcfc9af7b49ec34f2a094'
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
|
4
|
+
|
|
5
|
+
module SimplyCouch
|
|
6
|
+
# Global CouchDB configuration.
|
|
7
|
+
#
|
|
8
|
+
# Set once at boot:
|
|
9
|
+
# SimplyCouch.database_url = "http://admin:pass@localhost:5984/mozo_development"
|
|
10
|
+
#
|
|
11
|
+
# Per-request override (multi-tenant):
|
|
12
|
+
# Define a top-level Current class in your app:
|
|
13
|
+
#
|
|
14
|
+
# class Current < ActiveSupport::CurrentAttributes
|
|
15
|
+
# attribute :couch_database
|
|
16
|
+
# end
|
|
17
|
+
#
|
|
18
|
+
# Then set it per-request (e.g. in ApplicationController):
|
|
19
|
+
# Current.couch_database = SimplyCouch.database_for("http://.../company_x")
|
|
20
|
+
#
|
|
21
|
+
# Block-scoped:
|
|
22
|
+
# SimplyCouch.with_database(db) { ... }
|
|
23
|
+
#
|
|
24
|
+
# Fallback chain for Model#database:
|
|
25
|
+
# 1. Model's own use_database / couchrest_database_url
|
|
26
|
+
# 2. Current.couch_database (app-defined, request-scoped)
|
|
27
|
+
# 3. SimplyCouch.database_url (global default)
|
|
28
|
+
|
|
29
|
+
mattr_accessor :database_url
|
|
30
|
+
|
|
31
|
+
# Returns a DatabaseInstance for the given URL, caching by URL.
|
|
32
|
+
def self.database_for(url)
|
|
33
|
+
return nil unless url
|
|
34
|
+
databases[url] ||= Model::DatabaseInstance.new(url)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Returns the effective default database (request-scoped or global).
|
|
38
|
+
def self.database
|
|
39
|
+
current_database || database_for(database_url) || fallback_database
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Temporarily switch the current database for a block.
|
|
43
|
+
def self.with_database(database)
|
|
44
|
+
previous = current_database
|
|
45
|
+
self.current_database = database
|
|
46
|
+
yield
|
|
47
|
+
ensure
|
|
48
|
+
self.current_database = previous
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Get/set the request-scoped database (delegates to app's Current class).
|
|
52
|
+
def self.current_database
|
|
53
|
+
defined?(::Current) && ::Current.respond_to?(:couch_database) ? ::Current.couch_database : nil
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.current_database=(db)
|
|
57
|
+
::Current.couch_database = db if defined?(::Current) && ::Current.respond_to?(:couch_database=)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def self.databases
|
|
63
|
+
@_databases ||= {}
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.fallback_database
|
|
67
|
+
database_for(ENV['COUCHDB_URL'] || 'http://127.0.0.1:5984/mozo_development')
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -2,7 +2,13 @@ module SimplyCouch
|
|
|
2
2
|
module Model
|
|
3
3
|
module Database
|
|
4
4
|
def database
|
|
5
|
-
@_simply_couch_database ||=
|
|
5
|
+
@_simply_couch_database ||= begin
|
|
6
|
+
if @_couchrest_database_url
|
|
7
|
+
DatabaseInstance.new(full_database_url)
|
|
8
|
+
else
|
|
9
|
+
SimplyCouch.database
|
|
10
|
+
end
|
|
11
|
+
end
|
|
6
12
|
end
|
|
7
13
|
|
|
8
14
|
# Override this to provide a custom database URL.
|
data/lib/simply_couch.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: simply_couch
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Benjamin ter Kuile
|
|
@@ -80,6 +80,7 @@ files:
|
|
|
80
80
|
- lib/core_ext/time.rb
|
|
81
81
|
- lib/simply_couch.rb
|
|
82
82
|
- lib/simply_couch/class_methods_base.rb
|
|
83
|
+
- lib/simply_couch/database_config.rb
|
|
83
84
|
- lib/simply_couch/has_attachment.rb
|
|
84
85
|
- lib/simply_couch/include_relation.rb
|
|
85
86
|
- lib/simply_couch/instance_methods.rb
|