Class: 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.
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(controller, model) ⇒ Wrapper
constructor
new Wrapper, pass it your definition of user.
-
#logged_in? ⇒ Boolean
Do we have a @user yet?.
- #login(hash = Request.current.params) ⇒ Object
- #login?(hash) ⇒ Boolean
-
#logout ⇒ Object
Clear the persistance layer, forgetting all information we have.
-
#method_missing(meth, *args, &block) ⇒ Object
Refer everything not known.
- #persist ⇒ Object
- #persist=(hash) ⇒ Object
Constructor Details
#initialize(controller, model) ⇒ Wrapper
new Wrapper, pass it your definition of user.
27 28 29 30 31 32 |
# File 'lib/ramaze/helper/user.rb', line 27 def initialize(controller, model) raise ArgumentError, "No model defined for Helper::User" unless model @controller, @model = controller, model @user = nil login(persist) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
Refer everything not known
72 73 74 |
# File 'lib/ramaze/helper/user.rb', line 72 def method_missing(meth, *args, &block) user.send(meth, *args, &block) end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
21 22 23 |
# File 'lib/ramaze/helper/user.rb', line 21 def controller @controller end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
21 22 23 |
# File 'lib/ramaze/helper/user.rb', line 21 def model @model end |
#user ⇒ Object
Returns the value of attribute user.
20 21 22 |
# File 'lib/ramaze/helper/user.rb', line 20 def user @user end |
Instance Method Details
#logged_in? ⇒ Boolean
Do we have a @user yet?
35 36 37 |
# File 'lib/ramaze/helper/user.rb', line 35 def logged_in? !!user end |
#login(hash = Request.current.params) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/ramaze/helper/user.rb', line 58 def login(hash = Request.current.params) return if hash.empty? if found = login?(hash) @user = found self.persist = hash end end |
#login?(hash) ⇒ Boolean
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ramaze/helper/user.rb', line 39 def login?(hash) credentials = {} hash.each{|k,v| credentials[k.to_sym] = v.to_s } if checker = controller.trait[:user_check] checker.call(credentials) else model.check(credentials) end end |
#logout ⇒ Object
Clear the persistance layer, forgetting all information we have.
67 68 69 |
# File 'lib/ramaze/helper/user.rb', line 67 def logout persist.clear end |
#persist ⇒ Object
50 51 52 |
# File 'lib/ramaze/helper/user.rb', line 50 def persist session[:USER] ||= {} end |
#persist=(hash) ⇒ Object
54 55 56 |
# File 'lib/ramaze/helper/user.rb', line 54 def persist=(hash) session[:USER] = hash end |