Class: MongoI18n::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo-i18n/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, options = {}) ⇒ Store

Returns a new instance of Store.



6
7
8
# File 'lib/mongo-i18n/store.rb', line 6

def initialize(collection, options={})
  @collection, @options = collection, options
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



4
5
6
# File 'lib/mongo-i18n/store.rb', line 4

def collection
  @collection
end

Instance Method Details

#[](key, options = nil) ⇒ Object

alias for read



17
18
19
20
21
# File 'lib/mongo-i18n/store.rb', line 17

def [](key, options=nil)
  if doc = collection.find_one(:_id => key.to_s)
    doc["value"].to_s
  end
end

#[]=(key, value, options = {}) ⇒ Object



10
11
12
13
14
# File 'lib/mongo-i18n/store.rb', line 10

def []=(key, value, options = {})
  key = key.to_s
  doc = {:_id => key, :value => value}
  collection.save(doc)
end

#available_localesObject

Thankfully borrowed from Jodosha’s redis-store github.com/jodosha/redis-store/blob/master/lib/i18n/backend/redis.rb



37
38
39
40
41
42
43
# File 'lib/mongo-i18n/store.rb', line 37

def available_locales
  locales = self.keys.map { |k| k =~ /\./; $` }
  locales.uniq!
  locales.compact!
  locales.map! { |k| k.to_sym }
  locales
end

#del(key) ⇒ Object



31
32
33
# File 'lib/mongo-i18n/store.rb', line 31

def del(key)
  collection.remove({:_id => key})
end

#keysObject



23
24
25
26
27
28
29
# File 'lib/mongo-i18n/store.rb', line 23

def keys
  keys = []
  collection.find({}, :fields => ["_id"]).each do |row|
    keys.push(row["_id"])
  end
  keys
end