Class: MongoPlug
- Inherits:
-
Object
- Object
- MongoPlug
- Defined in:
- lib/obvious/files/external/mongo_plug.rb
Overview
To get the mongo system to work you need to have a counters collection. The code here will increment the seq field which we use to set the id. If you are having problems, you probably need to create the collection and add entries for each other collection you want to have an id sequence.
Instance Method Summary collapse
- #clean_up(input) ⇒ Object
- #get(input) ⇒ Object
-
#initialize(collection) ⇒ MongoPlug
constructor
A new instance of MongoPlug.
- #list ⇒ Object
- #remove(input) ⇒ Object
- #save(input) ⇒ Object
- #symbolize_keys(hash) ⇒ Object
Constructor Details
#initialize(collection) ⇒ MongoPlug
Returns a new instance of MongoPlug.
9 10 11 12 |
# File 'lib/obvious/files/external/mongo_plug.rb', line 9 def initialize collection @collection = collection @session = MONGO_SESSION end |
Instance Method Details
#clean_up(input) ⇒ Object
42 43 44 45 46 |
# File 'lib/obvious/files/external/mongo_plug.rb', line 42 def clean_up input result = symbolize_keys input result.delete :_id result end |
#get(input) ⇒ Object
23 24 25 26 |
# File 'lib/obvious/files/external/mongo_plug.rb', line 23 def get input result = symbolize_keys @session[@collection].find(:id => input[:id]).first clean_up result end |
#list ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/obvious/files/external/mongo_plug.rb', line 14 def list result = @session[@collection].find.entries result.map! do |entry| clean_up entry end result end |
#remove(input) ⇒ Object
37 38 39 40 |
# File 'lib/obvious/files/external/mongo_plug.rb', line 37 def remove input @session[@collection].find(:id => input[:id]).remove true end |
#save(input) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/obvious/files/external/mongo_plug.rb', line 28 def save input if input[:id] == -1 id = @session[:counters].find(:_id => @collection).modify({ "$inc" => { seq: 1 } }, new:true)["seq"] input[:id] = id end result = @session[@collection].find(:id => input[:id]).modify(input, upsert: true, new: true) clean_up result end |
#symbolize_keys(hash) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/obvious/files/external/mongo_plug.rb', line 48 def symbolize_keys hash hash.inject({}){|result, (key, value)| new_key = case key when String then key.to_sym else key end new_value = case value when Hash then symbolize_keys(value) else value end result[new_key] = new_value result } end |