Class: LetMeIn::Session

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/letmein.rb

Overview

LetMeIn::Session object. Things like UserSession are created automatically after the initialization

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = { }) ⇒ Session

Returns a new instance of Session.


55
56
57
58
59
60
61
62
# File 'lib/letmein.rb', line 55

def initialize(params = { })
  model = self.class.to_s.gsub('Session', '')
  model = LetMeIn.config.models.member?(model) ? model : LetMeIn.config.models.first
  self.class.model      ||= model
  self.class.attribute  ||= LetMeIn.accessor(:attribute, LetMeIn.config.models.index(self.class.model))
  self.      = params[:login] || params[self.class.attribute.to_sym]
  self.password   = params[:password]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object


80
81
82
83
84
85
86
87
# File 'lib/letmein.rb', line 80

def method_missing(method_name, *args)
  case method_name.to_s
    when self.class.attribute         then self.
    when "#{self.class.attribute}="   then self. = args[0]
    when self.class.model.underscore  then self.object
    else super
  end
end

Class Attribute Details

.attributeObject

Returns the value of attribute attribute


44
45
46
# File 'lib/letmein.rb', line 44

def attribute
  @attribute
end

.modelObject

Returns the value of attribute model


44
45
46
# File 'lib/letmein.rb', line 44

def model
  @model
end

Instance Attribute Details

#loginObject


49
50
51
# File 'lib/letmein.rb', line 49

def 
  @login
end

#objectObject


49
50
51
# File 'lib/letmein.rb', line 49

def object
  @object
end

#passwordObject


49
50
51
# File 'lib/letmein.rb', line 49

def password
  @password
end

Class Method Details

.create(params = {}) ⇒ Object


72
73
74
# File 'lib/letmein.rb', line 72

def self.create(params = {})
  object = self.new(params); object.save; object
end

.create!(params = {}) ⇒ Object


76
77
78
# File 'lib/letmein.rb', line 76

def self.create!(params = {})
  object = self.new(params); object.save!; object
end

Instance Method Details

#authenticateObject


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/letmein.rb', line 89

def authenticate
  p = LetMeIn.accessor(:password, LetMeIn.config.models.index(self.class.model))
  s = LetMeIn.accessor(:salt, LetMeIn.config.models.index(self.class.model))
  
  object = self.class.model.constantize.where("#{self.class.attribute}" => self.).first
  self.object = if object && !object.send(p).blank? && object.send(p) == BCrypt::Engine.hash_secret(self.password, object.send(s))
    object
  else
    errors.add :base, 'Failed to authenticate'
    nil
  end
end

#saveObject


64
65
66
# File 'lib/letmein.rb', line 64

def save
  self.valid?
end

#save!Object


68
69
70
# File 'lib/letmein.rb', line 68

def save!
  save || raise(LetMeIn::Error, 'Failed to authenticate')
end

#to_keyObject


102
103
104
# File 'lib/letmein.rb', line 102

def to_key
  nil
end