Sha256: a710abbd2e23f40b864959c2b0967abe33a22d2952fb3bab5ec206d91c42704c

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

module Authoreyes
  module Authorization
    class AuthorizationRuleSet
      include Enumerable
      extend Forwardable
      def_delegators :@rules, :each, :length, :[]

      def initialize(rules = [])
        @rules = rules.clone
        reset!
      end

      def initialize_copy(_source)
        @rules = @rules.collect(&:clone)
        reset!
      end

      def matching(roles, privileges, context)
        roles = [roles] unless roles.is_a?(Array)
        rules = cached_auth_rules[context] || []
        rules.select do |rule|
          rule.matches? roles, privileges, context
        end
      end

      def delete(rule)
        @rules.delete rule
        reset!
      end

      def <<(rule)
        @rules << rule
        reset!
      end

      def each(&block)
        @rules.each &block
      end

      private

      def reset!
        @cached_auth_rules = nil
      end

      def cached_auth_rules
        return @cached_auth_rules if @cached_auth_rules
        @cached_auth_rules = {}
        @rules.each do |rule|
          rule.contexts.each do |context|
            @cached_auth_rules[context] ||= []
            @cached_auth_rules[context] << rule
          end
        end
        @cached_auth_rules
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
authoreyes-0.2.4 lib/authoreyes/authorization/authorization_rule_set.rb
authoreyes-0.2.3 lib/authoreyes/authorization/authorization_rule_set.rb
authoreyes-0.2.2 lib/authoreyes/authorization/authorization_rule_set.rb