Module: CouchPotato

Extended by:
RSpec::StubDb
Defined in:
lib/couch_potato.rb,
lib/couch_potato/railtie.rb,
lib/couch_potato/version.rb,
lib/couch_potato/database.rb,
lib/couch_potato/validation.rb,
lib/couch_potato/view/lists.rb,
lib/couch_potato/persistence.rb,
lib/couch_potato/view/view_query.rb,
lib/couch_potato/persistence/json.rb,
lib/couch_potato/view/custom_views.rb,
lib/couch_potato/view/raw_view_spec.rb,
lib/couch_potato/view/base_view_spec.rb,
lib/couch_potato/view/model_view_spec.rb,
lib/couch_potato/persistence/callbacks.rb,
lib/couch_potato/persistence/revisions.rb,
lib/couch_potato/view/custom_view_spec.rb,
lib/couch_potato/persistence/properties.rb,
lib/couch_potato/persistence/attachments.rb,
lib/couch_potato/persistence/type_caster.rb,
lib/couch_potato/view/properties_view_spec.rb,
lib/couch_potato/persistence/simple_property.rb,
lib/couch_potato/persistence/dirty_attributes.rb,
lib/couch_potato/persistence/magic_timestamps.rb,
lib/couch_potato/rspec/matchers/map_to_matcher.rb,
lib/couch_potato/rspec/matchers/list_as_matcher.rb,
lib/couch_potato/forbidden_attributes_protection.rb,
lib/couch_potato/rspec/matchers/reduce_to_matcher.rb,
lib/couch_potato/persistence/deep_dirty_attributes.rb,
lib/couch_potato/persistence/deep_tracked_property.rb,
lib/couch_potato/persistence/active_model_compliance.rb,
lib/couch_potato/rspec/matchers/map_reduce_to_matcher.rb

Defined Under Namespace

Modules: Attachments, ForbiddenAttributesProtection, MagicTimestamps, Persistence, RSpec, Validation, View Classes: Conflict, Database, NotFound, Railtie

Constant Summary collapse

Config =
Struct.new(:database_host, :database_name, :digest_view_names,
:split_design_documents_per_view, :default_language, :additional_databases).new
VERSION =
'1.7.1'.freeze
RSPEC_VERSION =
'3.4.0'.freeze

Class Method Summary collapse

Methods included from RSpec::StubDb

stub_db

Class Method Details

.configure(config) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/couch_potato.rb', line 24

def self.configure(config)
  if config.is_a?(String)
    Config.database_name = config
  else
    config = config.stringify_keys
    Config.database_name = config['database']
    Config.database_host = config['database_host'] if config['database_host']
    Config.additional_databases = config['additional_databases'].stringify_keys if config['additional_databases']
    Config.split_design_documents_per_view = config['split_design_documents_per_view'] if config['split_design_documents_per_view']
    Config.digest_view_names = config['digest_view_names'] if config['digest_view_names']
    Config.default_language = config['default_language'] if config['default_language']
  end
end

.couchrest_databaseObject

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.



50
51
52
# File 'lib/couch_potato.rb', line 50

def self.couchrest_database
  Thread.current[:__couchrest_database] ||= CouchRest.database(full_url_to_database(CouchPotato::Config.database_name, CouchPotato::Config.database_host))
end

.couchrest_database_for_name(database_name) ⇒ Object

Returns a CouchRest-Database for directly accessing that functionality.



78
79
80
81
# File 'lib/couch_potato.rb', line 78

def self.couchrest_database_for_name(database_name)
  Thread.current[:__couchrest_databases] ||= {}
  Thread.current[:__couchrest_databases][database_name] ||= CouchRest.database(full_url_to_database(database_name, CouchPotato::Config.database_host))
end

.couchrest_database_for_name!(database_name) ⇒ Object

Creates a CouchRest-Database for directly accessing that functionality.



84
85
86
87
# File 'lib/couch_potato.rb', line 84

def self.couchrest_database_for_name!(database_name)
  Thread.current[:__couchrest_databases] ||= {}
  Thread.current[:__couchrest_databases][database_name] ||= CouchRest.database!(full_url_to_database(database_name))
end

.databaseObject

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.



45
46
47
# File 'lib/couch_potato.rb', line 45

def self.database
  Thread.current[:__couch_potato_database] ||= Database.new(couchrest_database)
end

.full_url_to_database(database_name = CouchPotato::Config.database_name, database_host = CouchPotato::Config.database_host) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/couch_potato.rb', line 89

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}"
  end
end

.modelsObject

returns all the classes that include the CouchPotato::Persistence module



39
40
41
42
# File 'lib/couch_potato.rb', line 39

def self.models
  @models ||= []
  @models
end

.rails_initObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/couch_potato/railtie.rb', line 6

def self.rails_init
  require File.expand_path(File.dirname(__FILE__) + '/../../rails/reload_classes') if Rails.env.development?
  path = Rails.root.join('config/couchdb.yml')
  if File.exist?(path)
    require 'yaml'
    config = YAML.safe_load(ERB.new(File.read(path)).result, [Symbol], [], ['default'])[Rails.env]
    CouchPotato.configure(config)
  else
    Rails.logger.warn 'Rails.root/config/couchdb.yml does not exist. Not configuring a database.'
  end
end

.resolve_database_name(database_name) ⇒ Object

resolves a name to a database name/full url configured under additional databases



62
63
64
# File 'lib/couch_potato.rb', line 62

def self.resolve_database_name(database_name)
  Config.additional_databases[database_name] || database_name
end

.use(database_name) ⇒ Object

Returns a specific database instance



55
56
57
58
59
# File 'lib/couch_potato.rb', line 55

def self.use(database_name)
  resolved_database_name = resolve_database_name(database_name)
  Thread.current[:__couch_potato_databases] ||= {}
  Thread.current[:__couch_potato_databases][resolved_database_name] ||= Database.new(couchrest_database_for_name!(resolved_database_name), name: database_name)
end

.with_database(database_name) {|use(database_name)| ... } ⇒ Object

Executes a block of code and yields a database with the given name.

example:

CouchPotato.with_database('couch_customer') do |couch|
  couch.save @customer
end

Yields:

  • (use(database_name))


73
74
75
# File 'lib/couch_potato.rb', line 73

def self.with_database(database_name)
  yield use(database_name)
end