Class: Notes::DatabaseController

Inherits:
Object
  • Object
show all
Defined in:
lib/notes/controller/db_controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection_name) ⇒ DatabaseController

Returns a new instance of DatabaseController.



3
4
5
# File 'lib/notes/controller/db_controller.rb', line 3

def initialize collection_name
  @collection = Mongo::Connection.new('localhost').db('notes')[collection_name]
end

Instance Method Details

#create(note) ⇒ Object



22
23
24
# File 'lib/notes/controller/db_controller.rb', line 22

def create note
  @collection.insert(note.to_hash)
end

#find(criterias) ⇒ Object



7
8
9
10
11
12
# File 'lib/notes/controller/db_controller.rb', line 7

def find criterias
  if should_apply_to_all?(criterias)
    return get_all_records
  end
  @collection.find({'$and' => get_criterias_as_hashes(criterias)})
end

#find_one(criterias) ⇒ Object



14
15
16
# File 'lib/notes/controller/db_controller.rb', line 14

def find_one criterias
  @collection.find_one({'$and' => get_criterias_as_hashes(criterias)})
end

#get_all_recordsObject



18
19
20
# File 'lib/notes/controller/db_controller.rb', line 18

def get_all_records
  @collection.find
end

#remove(criterias) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/notes/controller/db_controller.rb', line 26

def remove criterias
  if should_apply_to_all?(criterias)
    remove_all
    return 
  end
  @collection.remove({'$and' =>  get_criterias_as_hashes(criterias)})
end

#remove_allObject



34
35
36
# File 'lib/notes/controller/db_controller.rb', line 34

def remove_all
  @collection.remove({})
end

#update(note, criterias) ⇒ Object



38
39
40
41
42
# File 'lib/notes/controller/db_controller.rb', line 38

def update note, criterias
  criterias.each do |criteria|
    @collection.update({'token' => note.token}, {'$set' => {criteria.name => criteria.value}})
  end
end