Class: DCA::MongoStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/dca/storage/mongo_storage.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, context, options = {}) ⇒ MongoStorage

Returns a new instance of MongoStorage.



5
6
7
8
9
# File 'lib/dca/storage/mongo_storage.rb', line 5

def initialize(connection, context, options = {})
  @database = connection.db(options[:database] || DCA.project_name.underscore)
  @connection = connection
  @collection = database.collection(options[:collection] || get_alias(context))
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



3
4
5
# File 'lib/dca/storage/mongo_storage.rb', line 3

def collection
  @collection
end

#databaseObject (readonly)

Returns the value of attribute database.



3
4
5
# File 'lib/dca/storage/mongo_storage.rb', line 3

def database
  @database
end

Class Method Details

.establish_connection(config) ⇒ Object



11
12
13
# File 'lib/dca/storage/mongo_storage.rb', line 11

def self.establish_connection(config)
  Mongo::Connection.new config[:host], config[:port]
end

Instance Method Details

#context(object) ⇒ Object



45
46
47
48
49
# File 'lib/dca/storage/mongo_storage.rb', line 45

def context object
  result = self.clone
  result.instance_variable_set :@collection, result.database.collection(get_alias object)
  result
end

#create(item) ⇒ Object



33
34
35
# File 'lib/dca/storage/mongo_storage.rb', line 33

def create(item)
  item.id = collection.insert hash_from item
end

#find(position) ⇒ Object



25
26
27
# File 'lib/dca/storage/mongo_storage.rb', line 25

def find position
  collection.find_one base_id: position.base_id unless position.base_id.nil?
end

#refresh(item, state) ⇒ Object



29
30
31
# File 'lib/dca/storage/mongo_storage.rb', line 29

def refresh(item, state)
  send state, item
end

#remove(item) ⇒ Object



41
42
43
# File 'lib/dca/storage/mongo_storage.rb', line 41

def remove(item)
  collection.remove _id: item.id
end

#state(position) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/dca/storage/mongo_storage.rb', line 15

def state(position)
  item = find position
  return :create if item.nil?

  position.id = item['_id']
  return :unmodified if item['checksum'] == position.checksum

  return :update
end

#update(item) ⇒ Object



37
38
39
# File 'lib/dca/storage/mongo_storage.rb', line 37

def update(item)
  collection.update({_id: item.id}, hash_from(item))
end