Class: DataMapper::Adapters::CouchDBAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/couchdb_adapter/adapter.rb

Constant Summary collapse

ConnectionError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#create(resources) ⇒ Integer

Persists one or many new resources

Adapters provide specific implementation of this method

Examples:

adapter.create(collection)  # => 1

Parameters:

  • resources (Enumerable<Resource>)

    The list of resources (model instances) to create

Returns:

  • (Integer)

    The number of records that were actually saved into the data-store

Raises:

  • (NotImplementedError)


20
21
22
# File 'lib/couchdb_adapter/adapter.rb', line 20

def create(resources)
  raise NotImplementedError, "#{self.class}#create not implemented"
end

#db_nameObject

Returns the name of the CouchDB database.

Raises:

  • (RuntimeError)

    if the CouchDB database name is invalid.



85
86
87
88
89
90
91
92
# File 'lib/couchdb_adapter/adapter.rb', line 85

def db_name
  result = options[:path].scan(/^\/?([-_+%()$a-z0-9]+?)\/?$/).flatten[0]
  if result != nil
    return Addressable::URI.unencode_component(result)
  else
    raise StandardError, "Invalid database path: '#{options[:path]}'"
  end
end

#delete(collection) ⇒ Integer

Deletes one or many existing resources

Adapters provide specific implementation of this method

Examples:

adapter.delete(collection)  # => 1

Parameters:

  • collection (Collection)

    collection of records to be deleted

Returns:

  • (Integer)

    the number of records deleted

Raises:

  • (NotImplementedError)


78
79
80
# File 'lib/couchdb_adapter/adapter.rb', line 78

def delete(collection)
  raise NotImplementedError, "#{self.class}#delete not implemented"
end

#escaped_db_nameObject

Returns the name of the CouchDB database after being escaped.



95
96
97
98
# File 'lib/couchdb_adapter/adapter.rb', line 95

def escaped_db_name
  return Addressable::URI.encode_component(
    self.db_name, Addressable::URI::CharacterClasses::UNRESERVED)
end

#read(query) ⇒ Enumerable<Hash>

Reads one or many resources from a datastore

Adapters provide specific implementation of this method

Examples:

adapter.read(query)  # => [ { 'name' => 'Dan Kubb' } ]

Parameters:

  • query (Query)

    the query to match resources in the datastore

Returns:

  • (Enumerable<Hash>)

    an array of hashes to become resources



38
39
40
41
42
# File 'lib/couchdb_adapter/adapter.rb', line 38

def read(query)
  with_connection do |connection|
    
  end
end

#update(attributes, collection) ⇒ Integer

Updates one or many existing resources

Adapters provide specific implementation of this method

Examples:

adapter.update(attributes, collection)  # => 1

Parameters:

  • attributes (Hash(Property => Object))

    hash of attribute values to set, keyed by Property

  • collection (Collection)

    collection of records to be updated

Returns:

  • (Integer)

    the number of records updated

Raises:

  • (NotImplementedError)


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

def update(attributes, collection)
  raise NotImplementedError, "#{self.class}#update not implemented"
end