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.



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

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



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

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

#enable(key, rule) ⇒ Object



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

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.



18
19
20
21
# 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) ⇒ Object



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

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