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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae31eaff1df5b1b5f92f31fc03ed41f757e8d790d5ed6a288bc57f8f15b41f80
4
- data.tar.gz: 337be26593098ec144a9f89ea94f21e9614dea96ad44f0b6933191fb9ee9a72f
3
+ metadata.gz: 9f91e1cc95b626be0fc90071cd9874ad9f0ad8d45aa086d9c2c74914db6fdd80
4
+ data.tar.gz: d0344763634933ddd8ddf0a3cf09edbe368c9daa7126b793f814deb59e17da02
5
5
  SHA512:
6
- metadata.gz: 60edce6ad7801d418943af609c9bef4b09cc5ffeec88acca6ea9e111030f2f9981981c4b26f862d2b013dd260573ea2096e1df890803a03b07ffc6546fefc14e
7
- data.tar.gz: 21aca11008af8b4ed59fdb5adaf366cf9bdbc7e66db7cee4582879cf64411f44f9faf54658c5e143fc8dd5db5eed7ffa8c6a1d013b795927e5fbd927c80c794b
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 ||= DatabaseInstance.new(full_database_url)
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
@@ -19,6 +19,7 @@ unless defined?(SimplyCouch)
19
19
  class ModelNotInstantiatedError < RuntimeError; end
20
20
  end
21
21
 
22
+ require 'simply_couch/database_config'
22
23
  require 'simply_couch/model'
23
24
  require 'core_ext/time'
24
25
  require 'core_ext/date'
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.0
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