On Sun, Jul 26, 2009 at 9:24 AM, Michael
D'Auria<[email protected]> wrote:
>
> What orm are you guys using and which adapters?  I am thinking about
> going with a document db for part of a side project and when I looked
> around, the dm couchdb adapter was removed from dm-adapters.  I
> haven't searched around much for an adapter for mongodb.

I'm currently using MongoDB with merb in several applications as the
primary database. I've also used Redis as the primary DB with great
success. I don't use an ORM since this often doesn't make much sense,
if you have a one to one mapping of objects to collections this works
(I believe DM couchdb adapter made this assumption). To get the real
value of a document database or key-value store you're often using
some of the more interesting features like embedded queries, the mongo
increment/update operations, capped collections, etc. these don't map
well to your typical ORM.

In regard to the original question about authentication, the merb-auth
slices out of the box just require that you implement the class method
authenticate(login, pass) and return the user object if successfully
authenticated, or nil if unsuccessful. The class below is a self
contained User model object for merb which will enable you to
save/retrieve/authenticate users with merb-auth.


class User
  attr_accessor :id, :login, :password

  @@collection =
XGen::Mongo::Driver::Mongo.new.db('mydb').collection('mycollection')


  def initialize(params=nil)
    params = params.kind_of?(Hash) ? params : {}
    @id, @login, @password = params[:_id], params[:login], params[:password]
  end

  def save
    self.id ||= XGen::Mongo::Driver::ObjectID.new
    @@collection.save({:_id => self.id, :login => self.login,
:password => self.password})
  end

  class << self
    def get(login)
      h = @@collection.find_first(:login => login)
      h.nil? ? nil : User.new(h.to_mash)
    end

    def authenticate(login, password)
      u = get(login)
      if u && u.login == login && u.password == password
        return u
      end
      nil
    end
  end
end

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"merb" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/merb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to