Module: SimplestAuth::Model::ClassMethods

Defined in:
lib/simplest_auth/model.rb

Instance Method Summary collapse

Instance Method Details

#active_record?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/simplest_auth/model.rb', line 23

def active_record?
  defined?(ActiveRecord)
end

#authenticate(email, password) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/simplest_auth/model.rb', line 31

def authenticate(email, password)
  if active_record?
    klass = find_by_email(email)
  elsif data_mapper?
    klass = first(:email => email)
  end

  (klass && klass.authentic?(password)) ? klass : nil
end

#authenticate_by(ident) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/simplest_auth/model.rb', line 41

def authenticate_by(ident)
  if active_record?
    instance_eval <<-EOM
      def authenticate(#{ident}, password)
        klass = find_by_#{ident}(#{ident})
        (klass && klass.authentic?(password)) ? klass : nil
      end
    EOM
  elsif data_mapper?
    instance_eval <<-EOM
      def authenticate(#{ident}, password)
        klass = first(:#{ident} => #{ident})
        (klass && klass.authentic?(password)) ? klass : nil
      end
    EOM
  end
end

#data_mapper?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/simplest_auth/model.rb', line 27

def data_mapper?
  defined?(DataMapper)
end