Class: Curator::Mongo::DataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/curator/mongo/data_store.rb

Instance Method Summary collapse

Instance Method Details

#_collection(name) ⇒ Object



60
61
62
# File 'lib/curator/mongo/data_store.rb', line 60

def _collection(name)
  _db.collection(name)
end

#_dbObject



64
65
66
# File 'lib/curator/mongo/data_store.rb', line 64

def _db
  client.db(_db_name)
end

#_db_nameObject



72
73
74
# File 'lib/curator/mongo/data_store.rb', line 72

def _db_name
  @database_name
end

#_normalize_query(query) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/curator/mongo/data_store.rb', line 81

def _normalize_query(query)
  query.inject({}) do |hash, (key, value)|
    case value
    when Range
      hash[key] = {'$gte' => value.first, '$lt' => value.last}
    else
      hash[key] = value
    end
    hash
  end
end

#clientObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/curator/mongo/data_store.rb', line 7

def client
  return @client if @client
  config = YAML.load(File.read(Curator.config.mongo_config_file))[Curator.config.environment]
  host = config.delete(:host)
  port = config.delete(:port)
  password = config.delete(:password)
  username = config.delete(:username)
  @database_name = config.delete(:database) || default_db_name
  @client = ::Mongo::Connection.new(host, port, config)
  @client.add_auth(@database_name, username, password) if username and password
  @client
end

#default_db_nameObject



68
69
70
# File 'lib/curator/mongo/data_store.rb', line 68

def default_db_name
  "#{Curator.config.database}:#{Curator.config.environment}"
end

#delete(collection_name, id) ⇒ Object



39
40
41
42
# File 'lib/curator/mongo/data_store.rb', line 39

def delete(collection_name, id)
  collection = _collection(collection_name)
  collection.remove(:_id => id)
end

#find_by_attribute(collection_name, field, query) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/curator/mongo/data_store.rb', line 44

def find_by_attribute(collection_name, field, query)
  return [] if query.nil?

  exp = {}
  exp[field] = query
  collection = _collection(collection_name)
  documents = collection.find(_normalize_query(exp))
  documents.map {|doc| normalize_document(doc) }
end

#find_by_key(collection_name, id) ⇒ Object



54
55
56
57
58
# File 'lib/curator/mongo/data_store.rb', line 54

def find_by_key(collection_name, id)
  collection = _collection(collection_name)
  document = collection.find_one({:_id => id})
  normalize_document(document) unless document.nil?
end

#normalize_document(doc) ⇒ Object



76
77
78
79
# File 'lib/curator/mongo/data_store.rb', line 76

def normalize_document(doc)
  key = doc.delete '_id'
  Hash[:key => key, :data => doc]
end

#remove_all_keysObject



20
21
22
# File 'lib/curator/mongo/data_store.rb', line 20

def remove_all_keys
  self.reset!
end

#reset!Object



24
25
26
# File 'lib/curator/mongo/data_store.rb', line 24

def reset!
  _db.collections.each {|coll| coll.drop unless coll.name =~ /system/ }
end

#save(options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/curator/mongo/data_store.rb', line 28

def save(options)
  collection = _collection options[:collection_name]
  key = options.delete(:key)
  document = options[:value]
  document.merge!({:_id => key}) unless key.nil?
  options.fetch(:index, {}).each do |index_name, index_value|
    collection.ensure_index index_name
  end
  collection.save document
end