Class: DeclarativePolicy::Base::AbilityMap

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative_policy/base.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map = {}) ⇒ AbilityMap

Returns a new instance of AbilityMap.


10
11
12
# File 'lib/declarative_policy/base.rb', line 10

def initialize(map = {})
  @map = map
end

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map


9
10
11
# File 'lib/declarative_policy/base.rb', line 9

def map
  @map
end

Instance Method Details

#actions(key) ⇒ Object


22
23
24
# File 'lib/declarative_policy/base.rb', line 22

def actions(key)
  @map[key] ||= []
end

#enable(key, rule) ⇒ Object


26
27
28
# File 'lib/declarative_policy/base.rb', line 26

def enable(key, rule)
  actions(key) << [:enable, rule]
end

#merge(other) ⇒ Object

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


17
18
19
20
# File 'lib/declarative_policy/base.rb', line 17

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) ⇒ Object


30
31
32
# File 'lib/declarative_policy/base.rb', line 30

def prevent(key, rule)
  actions(key) << [:prevent, rule]
end