Module: SimpleCan

Defined in:
lib/simple_can.rb,
lib/simple_can/basic_strategy.rb

Defined Under Namespace

Modules: BasicStrategy, ClassMethods Classes: Unauthorized

Constant Summary collapse

THREAD_VAR =
"simple_can.capability"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.strategyObject

Returns the value of attribute strategy.



6
7
8
# File 'lib/simple_can.rb', line 6

def strategy
  @strategy
end

Class Method Details

.included(mod) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simple_can.rb', line 10

def self.included(mod)
  mod.strategy_set!

  meta = class << mod; self; end
  meta.send(:alias_method, :orig_method_added, :method_added)
  meta.send(:alias_method, :orig_singleton_method_added,
    :singleton_method_added)
  mod.extend(ClassMethods)

  strategy.roles.each do |role|
    [meta, mod].each do |scope|
      scope.send(:define_method, "#{role}?") do
        mod.strategy_set!
        SimpleCan.strategy.test(role, mod.capability)
      end
      scope.send(:define_method, "#{role}!") do
        mod.strategy_set!
        next if SimpleCan.strategy.test(role, mod.capability)
        raise SimpleCan::Unauthorized, "unauthorized with #{role}"
      end
    end
  end
end