Class: CanTango::PermissionEngine::Evaluator

Inherits:
Object
  • Object
show all
Includes:
Helpers::Debug, Rules
Defined in:
lib/cantango/permission_engine/evaluator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Rules::Dsl

included, #scope

Methods included from Helpers::Debug

#debug

Constructor Details

#initialize(ability, rule) ⇒ Evaluator

Returns a new instance of Evaluator.



10
11
12
13
14
# File 'lib/cantango/permission_engine/evaluator.rb', line 10

def initialize ability, rule
  @ability = ability
  @rule = rule
  safe_rules!
end

Instance Attribute Details

#abilityObject (readonly)

Returns the value of attribute ability.



6
7
8
# File 'lib/cantango/permission_engine/evaluator.rb', line 6

def ability
  @ability
end

#ruleObject (readonly)

Returns the value of attribute rule.



6
7
8
# File 'lib/cantango/permission_engine/evaluator.rb', line 6

def rule
  @rule
end

Instance Method Details

#evaluate!(user) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/cantango/permission_engine/evaluator.rb', line 16

def evaluate! user
  debug "Evaluating rule:"
  debug rule.can
  debug rule.cannot
  @user = user
  instance_eval rule.can if rule.can?
  instance_eval rule.cannot if rule.cannot?
end

#rulesObject



25
26
27
# File 'lib/cantango/permission_engine/evaluator.rb', line 25

def rules
  ability.send :rules
end

#safe_rules!Object



33
34
35
36
# File 'lib/cantango/permission_engine/evaluator.rb', line 33

def safe_rules!
  rule.can.gsub!(/(User|UserAccount)/,'::\1') if rule.can?
  rule.cannot.gsub!(/(User|UserAccount)/,'::\1') if rule.cannot?
end

#userObject



29
30
31
# File 'lib/cantango/permission_engine/evaluator.rb', line 29

def user
  @user
end