Sha256: 7c2a1f89968c09e571eecd2cd7c8fb13136351d82b6df21c48bfc33107c28853

Contents?: true

Size: 842 Bytes

Versions: 4

Compression:

Stored size: 842 Bytes

Contents

# frozen_string_literal: true

module SimpleAuth
  class Session
    def self.record_not_found_exceptions
      @record_not_found_exceptions ||= []
    end

    def self.create(**kwargs)
      new(**kwargs)
    end

    def initialize(scope:, session:, record: nil)
      @scope = scope
      @session = session
      self.record = record if record
    end

    def record=(record)
      @session[record_key] = record ? record.to_gid.to_s : nil
      @record = record
    end

    def record
      return unless record_id_from_session

      @record ||= GlobalID::Locator.locate(record_id_from_session)
    rescue *Session.record_not_found_exceptions
      nil
    end

    def record_key
      :"#{@scope}_id"
    end

    def record_id_from_session
      @session[record_key]
    end

    def valid?
      record.present?
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
simple_auth-3.1.4 lib/simple_auth/session.rb
simple_auth-3.1.3 lib/simple_auth/session.rb
simple_auth-3.1.2 lib/simple_auth/session.rb
simple_auth-3.1.1 lib/simple_auth/session.rb