Module: Kan::Abilities::ClassMethods

Defined in:
lib/kan/abilities.rb

Constant Summary collapse

DEFAULT_ROLE_NAME =
:base
DEFAULT_ROLE_BLOCK =
proc { true }

Instance Method Summary collapse

Instance Method Details

#ability_listObject



41
42
43
# File 'lib/kan/abilities.rb', line 41

def ability_list
  @ability_list || {}
end

#register(*abilities, &block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/kan/abilities.rb', line 16

def register(*abilities, &block)
  abilities.map!(&:to_sym)
  raise InvalidAbilityNameError if abilities.include?(:roles)

  @ability_list ||= {}
  abilities.each { |ability| @ability_list[ability] = block }
end

#role(role_name, object = nil, &block) ⇒ Object



24
25
26
27
# File 'lib/kan/abilities.rb', line 24

def role(role_name, object = nil, &block)
  @role_name = role_name
  @role_block = object ? make_callable(object) : block
end

#role_blockObject



33
34
35
# File 'lib/kan/abilities.rb', line 33

def role_block
  @role_block || DEFAULT_ROLE_BLOCK
end

#role_nameObject



29
30
31
# File 'lib/kan/abilities.rb', line 29

def role_name
  @role_name || DEFAULT_ROLE_NAME
end

#valid_role?(*args) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/kan/abilities.rb', line 37

def valid_role?(*args)
  role_block.call(*args)
end