Class: Ramaze::Helper::User::Wrapper

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(controller, model) ⇒ Wrapper

new Wrapper, pass it your definition of user.

Raises:

  • (ArgumentError)


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
  (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

#controllerObject (readonly)

Returns the value of attribute controller.



21
22
23
# File 'lib/ramaze/helper/user.rb', line 21

def controller
  @controller
end

#modelObject (readonly)

Returns the value of attribute model.



21
22
23
# File 'lib/ramaze/helper/user.rb', line 21

def model
  @model
end

#userObject

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?

Returns:

  • (Boolean)


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 (hash = Request.current.params)
  return if hash.empty?
  if found = login?(hash)
    @user = found
    self.persist = hash
  end
end

#login?(hash) ⇒ Boolean

Returns:

  • (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

#logoutObject

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

#persistObject



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