Class: Ramaze::Helper::User::Wrapper
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- Ramaze::Helper::User::Wrapper
- Defined in:
- lib/ramaze/helper/user.rb
Overview
Wrapper for the ever-present “user” in your application. It wraps around an arbitrary instance and worries about authentication and storing information about the user in the session.
In order to not interfere with the wrapped instance/model we start our methods with an underscore.
Patches and suggestions are highly appreciated.
Instance Attribute Summary collapse
-
#_callback ⇒ Object
Returns the value of attribute _callback.
-
#_model ⇒ Object
Returns the value of attribute _model.
-
#_user ⇒ Object
Returns the value of attribute _user.
Instance Method Summary collapse
-
#_logged_in? ⇒ true false
Whether the current user is logged in.
-
#_login(creds = _persistence) ⇒ Ramaze::Helper::User::Wrapper
Wrapped return value from model or callback.
- #_logout ⇒ Object
- #_persistence ⇒ Object
- #_persistence=(creds) ⇒ Object
-
#_would_login?(creds) ⇒ Boolean
The callback should return an instance of the user, otherwise it should answer with nil.
-
#initialize(model, callback) ⇒ Wrapper
constructor
A new instance of Wrapper.
-
#method_missing(meth, *args, &block) ⇒ Object
Refer everything not known THINK: This might be quite confusing…
Constructor Details
#initialize(model, callback) ⇒ Wrapper
Returns a new instance of Wrapper.
163 164 165 166 167 |
# File 'lib/ramaze/helper/user.rb', line 163 def initialize(model, callback) @_model, @_callback = model, callback @_user = nil _login end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
Refer everything not known THINK: This might be quite confusing… should we raise instead?
222 223 224 225 |
# File 'lib/ramaze/helper/user.rb', line 222 def method_missing(meth, *args, &block) return unless _user _user.send(meth, *args, &block) end |
Instance Attribute Details
#_callback ⇒ Object
Returns the value of attribute _callback.
161 162 163 |
# File 'lib/ramaze/helper/user.rb', line 161 def _callback @_callback end |
#_model ⇒ Object
Returns the value of attribute _model.
161 162 163 |
# File 'lib/ramaze/helper/user.rb', line 161 def _model @_model end |
#_user ⇒ Object
Returns the value of attribute _user.
161 162 163 |
# File 'lib/ramaze/helper/user.rb', line 161 def _user @_user end |
Instance Method Details
#_logged_in? ⇒ true false
Returns whether the current user is logged in.
208 209 210 |
# File 'lib/ramaze/helper/user.rb', line 208 def _logged_in? !!_user end |
#_login(creds = _persistence) ⇒ Ramaze::Helper::User::Wrapper
Returns wrapped return value from model or callback.
174 175 176 177 178 |
# File 'lib/ramaze/helper/user.rb', line 174 def _login(creds = _persistence) if @_user = _would_login?(creds) self._persistence = creds end end |
#_logout ⇒ Object
199 200 201 202 |
# File 'lib/ramaze/helper/user.rb', line 199 def _logout _persistence.clear Current.request.env['ramaze.helper.user'] = nil end |
#_persistence ⇒ Object
216 217 218 |
# File 'lib/ramaze/helper/user.rb', line 216 def _persistence Current.session[:USER] || {} end |
#_persistence=(creds) ⇒ Object
212 213 214 |
# File 'lib/ramaze/helper/user.rb', line 212 def _persistence=(creds) Current.session[:USER] = creds end |
#_would_login?(creds) ⇒ Boolean
The callback should return an instance of the user, otherwise it should answer with nil.
This will not actually login, just check whether the credentials would result in a user.
185 186 187 188 189 190 191 192 193 194 |
# File 'lib/ramaze/helper/user.rb', line 185 def _would_login?(creds) if c = @_callback c.call(creds) elsif _model.respond_to?(:authenticate) _model.authenticate(creds) else Log.warn("Helper::User has no callback and there is no %p::authenticate" % _model) nil end end |