Sha256: bf503a310a3fc1fd1e9a8373f0521e641b689c75f3fbba3f182e69a3144d6cb0

Contents?: true

Size: 1005 Bytes

Versions: 2

Compression:

Stored size: 1005 Bytes

Contents

module CanTango
  module Ability
    module Cache
      class Key
        attr_reader :user, :subject

        def initialize user, subject = nil
          @user = user
          @subject = subject || user
        end

        def self.create_for ability
          self.new ability.user, ability.subject
        end

        def value
          raise "No key could be generated for User:#{user.inspect} and Subject:#{subject} - key field: #{user_key_field}" if !user_key
          @value ||= user_key.hash
        end

        def to_s
          "key hash: #{value}"
        end

        protected

        def user_key
          # raise "#{user.class} must have a method ##{user_key_field}. You can configure this with CanTango.config#user.unique_key_field" if !user.respond_to?(user_key_field)
          user.send(user_key_field) if user.respond_to? user_key_field
        end

        def user_key_field
          CanTango.config.user.unique_key_field || :email
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cantango-core-0.1.1 lib/cantango/ability/cache/key.rb
cantango-core-0.1.0 lib/cantango/ability/cache/key.rb