class Femto::Model::MongoAdapter
Attributes
client[RW]
db[RW]
Public Class Methods
connect(options=nil)
click to toggle source
# File lib/femto/model/mongo_adapter.rb, line 11 def connect(options=nil) if options @client = Mongo::Connection.new(options[:host], options[:port]) @db = @client[options[:db]] else @client = Mongo::Connection.new @db = @client['test'] end end
find(cls, query)
click to toggle source
# File lib/femto/model/mongo_adapter.rb, line 30 def find(cls, query) results = [] get_coll(cls).find(query).each do |res| model = cls.new(symbolize_keys(res)) model.id = res['_id'] results << model end results end
get_coll(cls)
click to toggle source
# File lib/femto/model/mongo_adapter.rb, line 56 def get_coll(cls) @db[cls.model_attrs[:storage_name]] end
remove(model)
click to toggle source
# File lib/femto/model/mongo_adapter.rb, line 49 def remove(model) coll = get_coll model.class if model.id coll.remove(:_id => model.id) end end
symbolize_keys(hash)
click to toggle source
# File lib/femto/model/mongo_adapter.rb, line 60 def symbolize_keys(hash) hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} end
to_hash(model)
click to toggle source
# File lib/femto/model/mongo_adapter.rb, line 21 def to_hash(model) result = {} model.class.model_attrs[:fields].each do |f| var = model.send f result[f] = var if var end result end
update(model)
click to toggle source
# File lib/femto/model/mongo_adapter.rb, line 40 def update(model) coll = get_coll model.class if model.id coll.update({:_id => model.id}, model.to_hash) else model.id = coll.insert model.to_hash end end