Class: Abilities::Definitions

Inherits:
Object
  • Object
show all
Defined in:
lib/abilities/definitions.rb

Instance Method Summary collapse

Constructor Details

#initialize(actor, &block) ⇒ Definitions

Returns a new instance of Definitions.



4
5
6
7
# File 'lib/abilities/definitions.rb', line 4

def initialize(actor, &block)
  @actor = actor
  Proxy.new(actor, self, &block)
end

Instance Method Details

#add(actions, subjects, behavior, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/abilities/definitions.rb', line 9

def add(actions, subjects, behavior, &block)
  actions = [actions] unless actions.is_a? Array
  subjects = [subjects] unless subjects.is_a? Array
  subjects.each do |subject|
    actions.each do |action|
      (all[find_subject_id(subject)] ||= {})[action.to_s] = block_given? ? block : behavior
    end
  end
end

#can?(action, subject) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/abilities/definitions.rb', line 19

def can?(action, subject)
  subject_id = find_subject_id(subject)
  if subject_id != 'all' and can?(action, 'all')
    true
  elsif actions = all[subject_id]
    if behavior = (actions[action.to_s] || actions['manage'])
      if behavior.is_a? Proc
        @actor.instance_exec subject, &behavior
      else
        behavior
      end
    else
      false
    end
  else
    false
  end
end

#cannot?(*args) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/abilities/definitions.rb', line 38

def cannot?(*args)
  !can?(*args)
end