Class: Couchy::Database

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, database_name) ⇒ Database

Returns a new instance of Database.



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

def initialize(server, database_name)
  @name = database_name
  @server = server
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/couchy/database.rb', line 6

def name
  @name
end

#serverObject

Returns the value of attribute server.



6
7
8
# File 'lib/couchy/database.rb', line 6

def server
  @server
end

Instance Method Details

#bulk_save(docs) ⇒ Hash

Saves or updates a bunch of documents

Parameters:

  • docs (Array)

    The documents to save

Returns:

  • (Hash)

    Parsed server response



82
83
84
# File 'lib/couchy/database.rb', line 82

def bulk_save(docs)
  server.post("#{name}/_bulk_docs", {:docs => docs})
end

#delete(document, revision = nil) ⇒ Hash

Deletes a document

Parameters:

  • document (String, Hash)

    Document ID or an Hash representing the document

  • revision (String) (defaults to: nil)

    Document’s revision

Returns:

  • (Hash)

    Parsed server response

Raises:

  • ArgumentError When the Hash representing the document neither has an ID nor a revision

  • ArgumentError When document is neither an ID nor an Hash representing a document



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/couchy/database.rb', line 98

def delete(document, revision=nil)
  case document
  when String
    raise ArgumentError, 'Document revision must be specified' unless revision
    server.delete "#{name}/#{CGI.escape(document)}", :rev => revision
  when Hash
    raise ArgumentError, 'Document ID and revision must be specified' unless
      document['_id'] && document['_rev']
    server.delete("#{name}/#{CGI.escape(document['_id'])}", :rev => document['_rev'])
  else
    raise ArgumentError, 'Document must be an Hash representing the document or its ID'
  end
end

#delete!Hash

Deletes the current database

Returns:

  • (Hash)

    Parsed server response



115
116
117
# File 'lib/couchy/database.rb', line 115

def delete!
  server.delete(name)
end

#documents(params = {}) ⇒ Hash

Gets a list of all the documents available in the database

Parameters:

  • params (Hash) (defaults to: {})

Returns:

  • (Hash)

    Parsed server response



18
19
20
# File 'lib/couchy/database.rb', line 18

def documents(params={})
  server.get("#{name}/_all_docs", params)
end

#fetch_attachment(document_id, attachment_name) ⇒ Hash

Retrieves an attachment from a document

Parameters:

  • document_id (String)

    The ID of the document

  • attachment_name (String)

    The name of the attachment

Returns:

  • (Hash)

    Parsed server response



58
59
60
# File 'lib/couchy/database.rb', line 58

def fetch_attachment(document_id, attachment_name)
  server.get("#{name}/#{CGI.escape(document_id)}/#{CGI.escape(attachment_name)}", :no_json => true)
end

#get(id) ⇒ Hash

Retrieves a document by its ID

Parameters:

  • id (String)

    The ID of the document

Returns:

  • (Hash)

    Parsed server response



48
49
50
# File 'lib/couchy/database.rb', line 48

def get(id)
  server.get("#{name}/#{CGI.escape(id)}")
end

#save(doc) ⇒ Hash

Saves or updates a document

Parameters:

  • doc (Hash)

    The document

Returns:

  • (Hash)

    Parsed server response



67
68
69
70
71
72
73
74
75
# File 'lib/couchy/database.rb', line 67

def save(doc)
  doc = encode_attachments_of(doc)

  if doc['_id']
    server.put("#{name}/#{CGI.escape(doc['_id'])}", doc)
  else
    server.post("#{name}", doc)
  end
end

#temp_view(function, params = {}) ⇒ Hash

Creates a temporary view

Parameters:

  • function (String)

    The view function

  • params (Hash) (defaults to: {})

Returns:

  • (Hash)

    Parsed server response



28
29
30
31
# File 'lib/couchy/database.rb', line 28

def temp_view(function, params={})
  server.post("#{name}/_temp_view", function,
    params.merge!(:headers => {'Content-Type' => 'application/json'}))
end

#view(view_name, params = {}) ⇒ Hash

Query a view

Parameters:

  • view_name (String)

    The name of the view

  • params (Hash) (defaults to: {})

Returns:

  • (Hash)

    Parsed server response



39
40
41
# File 'lib/couchy/database.rb', line 39

def view(view_name, params={})
  server.get("#{name}/_view/#{view_name}", params)
end