Sha256: 9538d3b47e5f973869bbdf81e18a420bf0b4726523f73c6ad80bce9e79b9f064

Contents?: true

Size: 963 Bytes

Versions: 2

Compression:

Stored size: 963 Bytes

Contents

module SentientUser
  
  def self.included(base)
    base.class_eval {
      def self.current
        Thread.current[:user]
      end

      def self.current=(o)
        raise(ArgumentError,
            "Expected an object of class '#{self}', got #{o.inspect}") unless (o.is_a?(self) || o.nil?)
        Thread.current[:user] = o
      end
  
      def make_current
        Thread.current[:user] = self
      end

      def current?
        !Thread.current[:user].nil? && self.id == Thread.current[:user].id
      end
      
      def self.do_as(user, &block)
        old_user = self.current

        begin
          self.current = user
          response = block.call unless block.nil?
        ensure
          self.current = old_user
        end

        response
      end
    }
  end
end

module SentientController
  def self.included(base)
    base.class_eval {
      before_filter do |c|
        User.current = c.send(:current_user)
      end
    }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sentient_user-0.3.3 lib/sentient_user.rb
sentient_user-0.3.2 lib/sentient_user.rb