Sha256: ea87b5395a1ac77406a3efd416921314e36e47d08094e8f3a8bb2b912b29ac8e

Contents?: true

Size: 1.56 KB

Versions: 9

Compression:

Stored size: 1.56 KB

Contents

# -*- encoding : utf-8 -*-
require 'observer'

require 'reactor/cm/bridge'
require 'reactor/cache/user'
require 'reactor/session/observers'

class Reactor::Session
  attr_reader :user_name, :session_id
  include Observable

  def self.instance
    self.new
  end

  def marshal_dump
    [@user_name, @session_id]
  end

  def marshal_load(array)
    @user_name, @session_id = array
    self.add_observers
    self.proper_notify_observers(@user_name, false)
  end

  def initialize
    self.add_observers
  end


  def login(session_id)
    if !logged_in?(session_id)
      self.user_name = authenticate(session_id)
    end
  end

  def destroy
    self.session_id = self.user_name = nil
  end

  def logged_in?(session_id)
    self.session_id.present? && self.user? && self.session_id == session_id
  end

  def user?
    self.user_name.present?
  end

  def user
    Reactor::Cache::User.instance.get(self.user_name)
  end

  def user_name=(new_user_name)
    @user_name = new_user_name
    self.proper_notify_observers(@user_name, true)
    @user_name
  end

  protected
  attr_writer :session_id

  def authenticate(session_id)
    self.session_id = session_id
    Reactor::Cm::Bridge.login_for(session_id)
  end

  def add_observers
    Observers.constants.each do |possible_observer_name|
      possible_observer = Observers.const_get(possible_observer_name)
      if possible_observer.method_defined?(:update)
        self.add_observer(possible_observer.new)
      end
    end
  end

  def proper_notify_observers(*args)
    self.changed(true)
    self.notify_observers(*args)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
infopark_reactor-1.14.0.beta2 lib/reactor/session.rb
infopark_reactor-1.14.0.beta1 lib/reactor/session.rb
infopark_reactor-1.13.0 lib/reactor/session.rb
infopark_reactor-1.12.0 lib/reactor/session.rb
infopark_reactor-1.12.0.beta2 lib/reactor/session.rb
infopark_reactor-1.12.0.beta1 lib/reactor/session.rb
infopark_reactor-1.11.0.beta3 lib/reactor/session.rb
infopark_reactor-1.11.0.beta2 lib/reactor/session.rb
infopark_reactor-1.10.0.beta lib/reactor/session.rb