Class: SimplyCouch::Model::DatabaseInstance

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

Overview

Renamed from CompatibilityNote — this is the actual Database class (separate from the Database module above to avoid naming conflict)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(couchrest_database) ⇒ DatabaseInstance

Returns a new instance of DatabaseInstance.



70
71
72
73
74
75
76
77
78
79
# File 'lib/simply_couch/model/database.rb', line 70

def initialize(couchrest_database)
  if couchrest_database.is_a?(String)
    # URL string — create CouchRest database
    @couchrest_database = CouchRest.database(couchrest_database)
  elsif couchrest_database.nil?
    @couchrest_database = CouchRest.database('http://127.0.0.1:5984')
  else
    @couchrest_database = couchrest_database
  end
end

Instance Attribute Details

#couchrest_databaseObject (readonly)

Returns the value of attribute couchrest_database.



68
69
70
# File 'lib/simply_couch/model/database.rb', line 68

def couchrest_database
  @couchrest_database
end

Instance Method Details

#bulk_load(ids) ⇒ Object



142
143
144
145
146
147
# File 'lib/simply_couch/model/database.rb', line 142

def bulk_load(ids)
  response = couchrest_database.bulk_load ids
  docs = response['rows'].map{|row| row["doc"]}.compact
  docs.each{|doc| doc.database = self if doc.respond_to?(:database=) }
  docs
end

#delete_document(document) ⇒ Object



149
150
151
# File 'lib/simply_couch/model/database.rb', line 149

def delete_document(document)
  couchrest_database.delete_doc document.to_hash
end

#destroy_document(document, run_callbacks = true) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/simply_couch/model/database.rb', line 128

def destroy_document(document, run_callbacks = true)
  if run_callbacks
    document.run_callbacks :destroy do
      document._deleted = true
      couchrest_database.delete_doc document.to_hash
    end
  else
    document._deleted = true
    couchrest_database.delete_doc document.to_hash
  end
  document._id = nil
  document._rev = nil
end

#first(spec) ⇒ Object



100
101
102
103
# File 'lib/simply_couch/model/database.rb', line 100

def first(spec)
  spec.view_parameters = spec.view_parameters.merge({:limit => 1})
  view(spec).first
end

#load_document(id) ⇒ Object



121
122
123
124
125
126
# File 'lib/simply_couch/model/database.rb', line 121

def load_document(id)
  raise "Can't load a document without an id (got nil)" if id.nil?
  instance = couchrest_database.get(id)
  instance.database = self if instance.respond_to?(:database=)
  instance
end

#save_document(document, validate = true) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/simply_couch/model/database.rb', line 105

def save_document(document, validate = true)
  begin
    if document.new?
      create_document(document, validate)
    else
      update_document(document, validate)
    end
  rescue CouchRest::Conflict
    raise SimplyCouch::Conflict.new
  end
end

#save_document!(document) ⇒ Object



117
118
119
# File 'lib/simply_couch/model/database.rb', line 117

def save_document!(document)
  save_document(document) || raise("Validations failed: #{document.errors.full_messages}")
end

#view(spec) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/simply_couch/model/database.rb', line 81

def view(spec)
  results = View::ViewQuery.new(
    couchrest_database,
    spec.design_document,
    {spec.view_name => {
      :map => spec.map_function,
      :reduce => spec.reduce_function
    }},
    (spec.list_name ? {spec.list_name => spec.list_function} : nil),
    spec.lib,
    spec.language
  ).query_view!(spec.view_parameters)
  processed_results = spec.process_results results
  processed_results.each do |document|
    document.database = self if document.respond_to?(:database=)
  end if processed_results.respond_to?(:each)
  processed_results
end