Class: Sinatra::AuthLane::SerializedUser

Inherits:
Object
  • Object
show all
Defined in:
lib/authlane/serializeduser.rb

Overview

Storage class for logged in user credentials. Behaves like a Hash and an Object.

Instance Method Summary collapse

Constructor Details

#initialize(user, attributes = []) ⇒ SerializedUser

Sets up the Object to be serialized.

Receives an Object user and stores its attributes specified by attributes in a Hash. If attributes is empty, the whole object will be stored as-is.

Parameters:

  • user (Object)

    The User object that needs to be serialized

  • attributes (Array<Symbol>) (defaults to: [])

    A list of attribute names to be serialized from user


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/authlane/serializeduser.rb', line 20

def initialize(user, attributes = [])
  if attributes.size == 0
    @user = user
  else
    @user = Hash.new

    attributes.each do |attrs|
      if user.respond_to? attrs
        @user[attrs] = user.__send__(attrs.to_sym)
      elsif user.is_a?(Hash) and user.key? attrs
        @user[attrs] = user[attrs]
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Enables Object-like access to the stored attributes.

If the whole Object was stored, it sends the method name m as a message to that Object. Otherwise it will access the Hash using m as the key.

Parameters:

  • m (Symbol)

    The name of the serialized object's attribute to be read


59
60
61
62
63
64
65
# File 'lib/authlane/serializeduser.rb', line 59

def method_missing(m, *args, &block)
  if @user.is_a? Hash
    @user[m]
  else
    @user.__send__(m.to_sym, args)
  end
end

Instance Method Details

#[](a) ⇒ Object

Access stored attributes like a Hash.

If the whole Object was stored, it sends the Hash key a as a message to that Object.

Parameters:

  • a (String, Symbol)

    The name of the serialized object's attribute to be read


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

def [](a)
  (@user.is_a? Hash) ? @user[a] : @user.__send__(a.to_sym)
end

#to_hHash

Return a Hash representing the serialized object's attributes and values.

If the whole Object that was stored is a Hash itself, its to_h will be called. Otherwise, the Object's instance methods are called and mapped to a Hash. This is only the case when the passed user object is not a Hash and no specific attributes for storage are set (see #initialize)

In either case, attribute names can be accessed by Symbol or String key alike.

Returns:

  • (Hash)

    the Hash representation of the stored object.


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/authlane/serializeduser.rb', line 80

def to_h
  universal_hash = Hash.new

  if @user.is_a? Hash
    hash = @user.to_h
  else
    hash = {}
    @user.class.instance_methods(false).each do |key|
      hash[key] = @user.__send__ key
    end
  end

  hash.each_pair do |key, value|
    universal_hash[key.to_s] = value if key.is_a? Symbol

    universal_hash[key] = value
  end

  universal_hash
end