class DeclarativePolicy::Base::AbilityMap

A map of ability => list of rules together with :enable or :prevent actions. Used to look up which rules apply to a given ability. See Base.ability_map

Attributes

map[R]

Public Class Methods

new(map = {}) click to toggle source
# File lib/declarative_policy/base.rb, line 11
def initialize(map = {})
  @map = map
end

Public Instance Methods

actions(key) click to toggle source
# File lib/declarative_policy/base.rb, line 23
def actions(key)
  @map[key] ||= []
end
enable(key, rule) click to toggle source
# File lib/declarative_policy/base.rb, line 27
def enable(key, rule)
  actions(key) << [:enable, rule]
end
merge(other) click to toggle source

This merge behavior is different than regular hashes - if both share a key, the values at that key are concatenated, rather than overridden.

# File lib/declarative_policy/base.rb, line 18
def merge(other)
  conflict_proc = proc { |_key, my_val, other_val| my_val + other_val }
  AbilityMap.new(@map.merge(other.map, &conflict_proc))
end
prevent(key, rule) click to toggle source
# File lib/declarative_policy/base.rb, line 31
def prevent(key, rule)
  actions(key) << [:prevent, rule]
end