Sha256: 5494d7675e88e9667aab06a4600ae1eede1654f842ce73bd855bab72ae125db5

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

module Abilities
  class Definitions

    attr_reader :user

    def initialize(user, &block)
      @user = user
      Proxy.new self, &block
    end

    def can?(action, resource)
      id = normalize_id(resource)
      if id != :any && can?(action, :any)
        true
      elsif actions = registry[id]
        if policy = (actions[action] || actions[:manage])
          if policy.is_a?(Proc)
            user.instance_exec resource, &policy
          else
            policy
          end
        else
          false
        end
      else
        false
      end
    end

    def cannot?(*args)
      !can?(*args)
    end

    def add(actions, resources, policy)
      unless actions.is_a?(Array)
        actions = [actions]
      end
      unless resources.is_a?(Array)
        resources = [resources]
      end
      resources.each do |resource|
        actions.each do |action|
          id = normalize_id(resource)
          registry[id] ||= {}
          registry[id][action] = policy
        end
      end
    end

    private

    def registry
      @registry ||= {}
    end

    def normalize_id(resource)
      case resource
      when :any
        resource
      when Class
        resource.name.underscore.to_sym
      else
        resource.class.name.underscore.to_sym
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
abilities-5.1.2 lib/abilities/definitions.rb
abilities-5.1.1 lib/abilities/definitions.rb
abilities-5.1.0 lib/abilities/definitions.rb