Class: CouchTomato::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_tomato/database.rb

Defined Under Namespace

Classes: ValidationsFailedError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.couchrest_dbObject

Returns the value of attribute couchrest_db.



14
15
16
# File 'lib/couch_tomato/database.rb', line 14

def couchrest_db
  @couchrest_db
end

.nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/couch_tomato/database.rb', line 11

def name
  @name
end

.prefixObject

Returns the value of attribute prefix.



9
10
11
# File 'lib/couch_tomato/database.rb', line 9

def prefix
  @prefix
end

.suffixObject

Returns the value of attribute suffix.



10
11
12
# File 'lib/couch_tomato/database.rb', line 10

def suffix
  @suffix
end

.urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/couch_tomato/database.rb', line 8

def url
  @url
end

.viewsObject

Returns the value of attribute views.



12
13
14
# File 'lib/couch_tomato/database.rb', line 12

def views
  @views
end

Class Method Details

.bulk_save(documents) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/couch_tomato/database.rb', line 70

def self.bulk_save(documents)
  doc_hashes = []

  documents.each do |document|
    document.run_callbacks :before_validation_on_save
    document.run_callbacks(document.new? ? :before_validation_on_create : :before_validation_on_update)
    return unless document.valid?
    document.run_callbacks :before_save
    document.run_callbacks(document.new? ? :before_create : :before_update)

    doc_hashes << document.to_hash
  end

  res = self.couchrest_db.bulk_save(doc_hashes)

  documents.each_with_index do |document, index|
    is_new = document.new?
    document._id = res[index]['id'] if is_new
    document._rev = res[index]['rev']
    document.run_callbacks :after_save
    document.run_callbacks(is_new ? :after_create : :after_update)
  end

  true
end

.databaseObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/couch_tomato/database.rb', line 25

def self.database
  return self.couchrest_db if self.couchrest_db

  path = "#{self.url.gsub(/\/\s*$/, "")}/#{([self.prefix.to_s, self.name, self.suffix.to_s] - [""]).join("_")}"

  self.couchrest_db = CouchRest.database(path)

  begin
    self.couchrest_db.info
  rescue RestClient::ResourceNotFound
    raise "Database '#{path}' does not exist."
  end

  return self.couchrest_db
end

.destroy_document(document) ⇒ Object Also known as: destroy, destroy_doc



96
97
98
99
100
101
102
103
# File 'lib/couch_tomato/database.rb', line 96

def self.destroy_document(document)
  document.run_callbacks :before_destroy
  document._deleted = true
  self.database.delete_doc document.to_hash
  document.run_callbacks :after_destroy
  document._id = nil
  document._rev = nil
end

.inherited(c) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/couch_tomato/database.rb', line 17

def self.inherited(c)
  c.url    = CouchTomato::Config.url
  c.prefix = CouchTomato::Config.prefix
  c.suffix = CouchTomato::Config.suffix
  c.name   = c.to_s.underscore
  c.views  = {}
end

.inspectObject



126
127
128
129
130
# File 'lib/couch_tomato/database.rb', line 126

def self.inspect
  puts "Database server: #{self.url || "nil"}"
  puts "Database name:   #{self.name || "nil"}"
  puts "Views: #{self.views.inspect}"
end

.load_document(id, options = {}) ⇒ Object Also known as: load, load_doc



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/couch_tomato/database.rb', line 105

def self.load_document(id, options ={})
  raise "Can't load a document without an id (got nil)" if id.nil?

  begin
    # json = self.couchrest_db.get(id)
    # instance = Class.const_get(json['ruby_class']).json_create json
    # # instance.database = self
    # instance
    json = self.database.get(id)
    if options[:model] == :raw || !json['ruby_class']
      {}.merge(json)
    else
      klass = class_from_string(json['ruby_class'])
      instance = klass.json_create json
      instance
    end
  rescue(RestClient::ResourceNotFound) #'Document not found'
    nil
  end
end

.query_view!(name, options = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/couch_tomato/database.rb', line 132

def self.query_view!(name, options={})
  view = self.views[name]
  raise 'View does not exist' unless view

  begin
    tmp_couch_opts = view[:couch_options] || {}
    pr_options = options.merge(tmp_couch_opts)
    results = self.query_view(name, pr_options)
    self.process_results(name, results, pr_options)
  rescue RestClient::ResourceNotFound# => e
    raise
  end
end

.save_document(document) ⇒ Object Also known as: save, save_doc



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/couch_tomato/database.rb', line 51

def self.save_document(document)
  # TODO: Need to place some protected block here to respond to an exception in case trying to save
  #       a :raw document with an old _rev number

  # return self.couchrest_db.save_doc(document) unless document.respond_to?(:dirty?)

  # return true if document.respond_to?(:dirty?) && !document.dirty?

  if document.new?
    self.create_document document
  else
    self.update_document document
  end
end

.save_document!(document) ⇒ Object Also known as: save!, save_doc!



66
67
68
# File 'lib/couch_tomato/database.rb', line 66

def self.save_document!(document)
  save_document(document) || raise(ValidationsFailedError.new(document.errors.full_messages))
end

.view(name, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/couch_tomato/database.rb', line 41

def self.view(name, options={})
  raise "A View nemonic must be specified" if name.nil?

  self.views[name] = {}
  self.views[name][:design_doc] = !options[:design_doc] ? self.name.to_sym : options.delete(:design_doc).to_sym
  self.views[name][:view_name] = options.delete(:view_name) || name.to_s
  self.views[name][:model] = options.delete(:model)
  self.views[name][:couch_options] = options
end