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



101
102
103
# File 'lib/curator/mongo/data_store.rb', line 101

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

#_dbObject



105
106
107
# File 'lib/curator/mongo/data_store.rb', line 105

def _db
  client.db(_db_name)
end

#_db_nameObject



113
114
115
# File 'lib/curator/mongo/data_store.rb', line 113

def _db_name
  @database_name
end

#_find_by_key_as_object_id(collection, id) ⇒ Object



95
96
97
98
99
# File 'lib/curator/mongo/data_store.rb', line 95

def _find_by_key_as_object_id(collection, id)
  if BSON::ObjectId.legal?(id)
    collection.find_one(:_id => BSON::ObjectId.from_string(id))
  end
end

#_find_by_key_as_provided(collection, id) ⇒ Object



91
92
93
# File 'lib/curator/mongo/data_store.rb', line 91

def _find_by_key_as_provided(collection, id)
  collection.find_one(:_id => id)
end

#_normalize_query(query) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/curator/mongo/data_store.rb', line 122

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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/curator/mongo/data_store.rb', line 7

def client
  return @client if @client

  if Curator.config.client
    @client        = Curator.config.client
    @database_name = Curator.config.database
    return @client
  end

  if ENV['MONGO_URI']
    require 'uri'
    uri = URI.parse(ENV['MONGO_URI'])
    @database_name = uri.path.gsub(/^\//, '')
    @client = ::Mongo::Connection.from_uri(uri.to_s)
    return @client
  end

  config = YAML.load(File.read(Curator.config.mongo_config_file))[Curator.config.environment]
  config = config.symbolize_keys

  host = config.delete(:host)
  port = config.delete(:port)
  password = ENV['MONGO_PASSWORD'] || config.delete(:password)
  username = ENV['MONGO_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



109
110
111
# File 'lib/curator/mongo/data_store.rb', line 109

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

#delete(collection_name, id) ⇒ Object



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

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

#find_all(collection_name) ⇒ Object



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

def find_all(collection_name)
  collection = _collection(collection_name)
  documents = collection.find
  documents.map {|doc| normalize_document(doc) }
end

#find_by_attribute(collection_name, field, query) ⇒ Object



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

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



85
86
87
88
89
# File 'lib/curator/mongo/data_store.rb', line 85

def find_by_key(collection_name, id)
  collection = _collection(collection_name)
  document = _find_by_key_as_provided(collection, id) || _find_by_key_as_object_id(collection, id)
  normalize_document(document) unless document.nil?
end

#normalize_document(doc) ⇒ Object



117
118
119
120
# File 'lib/curator/mongo/data_store.rb', line 117

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

#remove_all_keysObject



45
46
47
# File 'lib/curator/mongo/data_store.rb', line 45

def remove_all_keys
  self.reset!
end

#reset!Object



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

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

#save(options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/curator/mongo/data_store.rb', line 53

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

#settings(collection_name) ⇒ Object

Raises:

  • (StandardError)


37
38
39
# File 'lib/curator/mongo/data_store.rb', line 37

def settings(collection_name)
  raise StandardError, "Not implemented yet"
end

#update_settings!(collection_name, updated_settings) ⇒ Object

Raises:

  • (StandardError)


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

def update_settings!(collection_name, updated_settings)
  raise StandardError, "Not implemented yet"
end