Class: ShyCouch::CouchDatabase

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

Defined Under Namespace

Classes: CouchServerConnection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ CouchDatabase

Returns a new instance of CouchDatabase.



41
42
43
# File 'lib/ShyCouch.rb', line 41

def initialize(settings)
init(settings)
end

Instance Attribute Details

#design_documentsObject

Returns the value of attribute design_documents.



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

def design_documents
  @design_documents
end

#designsObject

Returns the value of attribute designs.



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

def designs
  @designs
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#serverObject

Returns the value of attribute server.



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

def server
  @server
end

Instance Method Details

#add_design(design) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/ShyCouch.rb', line 155

def add_design(design)
  throw TypeError unless design.kind_of?(ShyCouch::Data::Design)
  # if the db has already stored the design, update it
  if design_by_name(design.name) != nil
		design_by_name(design.name) == design
  end
  # otherwise, add it
  @designs << design
end

#add_design_and_push!(doc) ⇒ Object



172
173
174
175
# File 'lib/ShyCouch.rb', line 172

def add_design_and_push!(doc)
  add_design(doc)
  push_designs!
end

#add_designs_and_push!(*docs) ⇒ Object



165
166
167
168
169
170
# File 'lib/ShyCouch.rb', line 165

def add_designs_and_push!(*docs)
  docs.each do |doc|
		add_design(doc)
  end
  push_designs!
end

#all_docsObject



92
93
94
95
96
# File 'lib/ShyCouch.rb', line 92

def all_docs
  getDocumentById('_all_docs').rows.map { |doc|
		getDocumentById(doc["id"])
  }
end

#connectObject



47
48
49
50
51
52
53
54
# File 'lib/ShyCouch.rb', line 47

def connect
	@server = CouchServerConnection.new({"host"=>@host, "port"=>@port, "user"=>@user, "password"=>@password, "database" => @name})
	if @server.responds?
		return {"ok"=>true, "message"=>"Successfully connected to the couch database at #{@host}:#{@port}"}
	else
		return {"ok"=>false, "message"=>"Could not connect to the couch database."}
	end
end

#create!Object



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

def create!
	@server.create_db(@name)
end

#delete!Object



55
56
57
# File 'lib/ShyCouch.rb', line 55

def delete!
  @server.delete_db(self.name)
end

#delete_document!(doc, opts = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ShyCouch.rb', line 113

def delete_document!(doc, opts = {})
	if !doc._id
		if !doc._rev
			raise ShyCouch::DocumentValidationError, "Cannot delete document without id and rev" unless doc._id
		else
			raise ShyCouch::DocumentValidationError, "Cannot delete document without id" unless doc._id
		end
	elsif !doc._rev
		raise ShyCouch::DocumentValidationError, "Cannot delete document without id" unless doc._id
	end
	@server.delete_document(self.name, doc)
end

#design(design) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/ShyCouch.rb', line 63

def design(design)
  if design.kind_of?(ShyCouch::Data::Design)
		return design_by_name(design.name)
  else
		return design_by_name(design)
  end
end

#design_by_name(name) ⇒ Object



71
72
73
74
# File 'lib/ShyCouch.rb', line 71

def design_by_name(name)
  @designs.each { |design| return design if design.name == name.to_s }
  return nil
end

#exists?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/ShyCouch.rb', line 76

def exists?
  @server.has_database?(@name)
end

#getDocumentById(id) ⇒ Object



82
83
84
85
86
# File 'lib/ShyCouch.rb', line 82

def getDocumentById(id)
  @server.getDocumentById @name, id
rescue RestClient::ResourceNotFound
	raise ShyCouch::ResourceNotFound
end

#init!Object



60
61
62
# File 'lib/ShyCouch.rb', line 60

def init!
  create! unless exists?
end

#pull_design(designName) ⇒ Object



88
89
90
91
# File 'lib/ShyCouch.rb', line 88

def pull_design(designName)
  doc = @server.getDocumentById(@name, designName)
  return ShyCouch::Data::Design.new(designName).merge! doc
end

#pull_document(document) ⇒ Object



101
102
103
104
105
# File 'lib/ShyCouch.rb', line 101

def pull_document(document)
  @server.pull_document(self.name, document)
rescue RestClient::ResourceNotFound
	raise ShyCouch::ResourceNotFound
end

#push_designs!Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ShyCouch.rb', line 177

def push_designs!
  @designs.each do |design|
		# push the document and update it with the rev sent by the response
		begin
		  design._rev = push_document!(design)["rev"]
		rescue ShyCouch::DesignConflict => e
		  # Get the rev of the existing one, then try again!
		  # If that fails, it should still throw an error
		  existing_design = getDocumentById design._id
		  design._rev = existing_design._rev
		  design._rev = push_document!(design)["rev"]
		end
  end
end

#push_document!(doc, opts = {}) ⇒ Object



107
108
109
110
111
# File 'lib/ShyCouch.rb', line 107

def push_document!(doc, opts = {})
  raise(ShyCouch::DocumentValidationError, "Document missing required fields: #{doc.missing_needs}") unless doc.satisfies_needs?
  raise ShyCouch::DocumentValidationError, "Document missing suggested fields, not overriden: #{doc.missing_suggestions}" unless doc.satisfies_suggestions?(opts)
  @server.push_document(self.name, doc)
end

#query_string(opts = {}) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ShyCouch.rb', line 140

def query_string(opts = {})
	# builds a http query string from options
    # need to stringify key queries
	exceptions = [:from] # stuff that might get passed in that's not for a query option

    return opts.map{ |k,v|
      raise ShyCouch::ViewError, "Illegal view option: #{k}" unless @legal_view_options.has_key? k
      if @legal_view_options[k][:stringify] == true
        "#{k.to_s}=#{URI.escape(v.to_s.to_json)}" if !exceptions.include? k
      else
        "#{k.to_s}=#{v.to_s}" if !exceptions.include? k
      end
    }.join("&")
end

#query_view(design_name, view_name, opts = {}) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ShyCouch.rb', line 126

def query_view(design_name, view_name, opts = {})
	# build query string from the options
	uri = "_design/#{design_name}/_view/#{view_name}"
	uri += "?#{query_string opts}" if opts.length > 0
  result = ShyCouch::Data::ViewResultHandler.init(getDocumentById(uri))
    if opts.has_key? :key and result.kind_of? ShyCouch::Data::CouchDocumentCollection #result.length == 1
      # Return it as a document if it was a key query
      return result[0]
      #return result
    else
	  return result
    end
end

#server_responds?Boolean

Returns:

  • (Boolean)


58
# File 'lib/ShyCouch.rb', line 58

def server_responds?; return @server.responds?; end

#uriObject



97
98
99
# File 'lib/ShyCouch.rb', line 97

def uri
  return "http://#{host}/#{port}/#{name}"
end

#view(design, view_obj) ⇒ Object



192
193
194
195
# File 'lib/ShyCouch.rb', line 192

def view(design, view_obj)
  url = "#{design._id}/_view/#{view_obj.name}"
  getDocumentById(url)
end