Class: Crocoduck::Store

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/crocoduck/store.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.id_fieldObject

Returns the value of attribute id_field.



21
22
23
# File 'lib/crocoduck/store.rb', line 21

def id_field
  @id_field
end

.server_clusterObject

Returns the value of attribute server_cluster.



21
22
23
# File 'lib/crocoduck/store.rb', line 21

def server_cluster
  @server_cluster
end

.server_collectionObject

Returns the value of attribute server_collection.



21
22
23
# File 'lib/crocoduck/store.rb', line 21

def server_collection
  @server_collection
end

.server_dbObject

Returns the value of attribute server_db.



21
22
23
# File 'lib/crocoduck/store.rb', line 21

def server_db
  @server_db
end

Instance Attribute Details

#collection=(value) ⇒ Object

Sets the attribute collection

Parameters:

  • value

    the value to set the attribute collection to.



24
25
26
# File 'lib/crocoduck/store.rb', line 24

def collection=(value)
  @collection = value
end

#database=(value) ⇒ Object

Sets the attribute database

Parameters:

  • value

    the value to set the attribute database to.



24
25
26
# File 'lib/crocoduck/store.rb', line 24

def database=(value)
  @database = value
end

#store=(value) ⇒ Object

Sets the attribute store

Parameters:

  • value

    the value to set the attribute store to.



24
25
26
# File 'lib/crocoduck/store.rb', line 24

def store=(value)
  @store = value
end

Instance Method Details

#closeObject



34
35
36
# File 'lib/crocoduck/store.rb', line 34

def close
  store.close
end

#get(id) ⇒ Object

Returns a single document given its ID



48
49
50
51
52
# File 'lib/crocoduck/store.rb', line 48

def get(id)
  collection.find_one({
    Crocoduck::Store.id_field => id.to_i
  })
end

#insert(document) ⇒ Object

Inserts a brand new document into the database



64
65
66
# File 'lib/crocoduck/store.rb', line 64

def insert(document)
  collection.insert document
end

#remove(criteria = nil) ⇒ Object

Use this method to remove documents from your datastore. Cares has been taken to prevent accidental database destruction. Only pass {} to this method if you are 100% sure you want to clear the database.



58
59
60
61
# File 'lib/crocoduck/store.rb', line 58

def remove(criteria=nil)
  return if criteria.nil?
  collection.remove criteria
end

#setup?Boolean

A nice method to determine if there is enough information to potentially connect to the backing database.

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/crocoduck/store.rb', line 28

def setup?
  Crocoduck::Store.server_cluster && 
  Crocoduck::Store.server_db && 
  Crocoduck::Store.server_collection
end

#update(entry_id, field, value) ⇒ Object

A simple convienance method to update a single document in your datastore.



40
41
42
43
44
45
# File 'lib/crocoduck/store.rb', line 40

def update(entry_id, field, value)
  collection.update({
    Crocoduck::Store.id_field => entry_id}, 
    {'$set' => { field => value}
  }, :safe => true)
end