Class: Curator::Mongo::DataStore

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

Class Method Summary collapse

Class Method Details

._collection(name) ⇒ Object



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

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

._collection_name(name) ⇒ Object



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

def self._collection_name(name)
  _db.collection(name).name
end

._dbObject



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

def self._db
  client.db(_db_name)
end

._db_nameObject



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

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

._normalize_query(query) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/curator/mongo/data_store.rb', line 76

def self._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
# File 'lib/curator/mongo/data_store.rb', line 7

def self.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)
  @client = ::Mongo::Connection.new(host, port, config)
end

.delete(collection_name, id) ⇒ Object



34
35
36
37
# File 'lib/curator/mongo/data_store.rb', line 34

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

.find_by_index(collection_name, field, query) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/curator/mongo/data_store.rb', line 39

def self.find_by_index(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



49
50
51
52
53
# File 'lib/curator/mongo/data_store.rb', line 49

def self.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



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

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

.remove_all_keysObject



15
16
17
# File 'lib/curator/mongo/data_store.rb', line 15

def self.remove_all_keys
  self.reset!
end

.reset!Object



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

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

.save(options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/curator/mongo/data_store.rb', line 23

def self.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