Class: Grognard::Guard
- Inherits:
-
Object
show all
- Defined in:
- lib/grognard/guard.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*arr_roles, hash_roles: {}) ⇒ Guard
Returns a new instance of Guard.
9
10
11
12
13
14
15
|
# File 'lib/grognard/guard.rb', line 9
def initialize(*arr_roles, hash_roles: {})
@can_dos = {}
@current_abilities = nil
insert_array_roles(arr_roles) ||
insert_hash_roles(hash_roles)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/grognard/guard.rb', line 56
def method_missing(meth, *args, &block)
meth = meth.to_s
case
when meth.match(/^be_a_(\w+)/)
be_a($1, *args, &block)
when meth.match(/^can_be_a_(\w+)?/)
can_be_a?($1, *args, &block)
when meth.match(/^can_(\w+)?/)
can?($1.to_sym, *args, &block)
else
super
end
end
|
Instance Attribute Details
#can_dos ⇒ Object
Returns the value of attribute can_dos.
7
8
9
|
# File 'lib/grognard/guard.rb', line 7
def can_dos
@can_dos
end
|
#current_abilities ⇒ Object
Returns the value of attribute current_abilities.
7
8
9
|
# File 'lib/grognard/guard.rb', line 7
def current_abilities
@current_abilities
end
|
Instance Method Details
#<<(object) ⇒ Object
30
31
32
33
|
# File 'lib/grognard/guard.rb', line 30
def <<(object)
add_role(object.name.underscore, object)
self
end
|
#abilities_defined!(object) ⇒ Object
43
44
45
46
|
# File 'lib/grognard/guard.rb', line 43
def abilities_defined!(object)
abilities_defined?(object) ||
raise_no_such_abilites
end
|
#abilities_defined?(object) ⇒ Boolean
39
40
41
|
# File 'lib/grognard/guard.rb', line 39
def abilities_defined?(object)
object.respond_to?(:allowed_to)
end
|
#add_role(name, object) ⇒ Object
22
23
24
|
# File 'lib/grognard/guard.rb', line 22
def add_role(name, object)
@can_dos[name.to_sym] = object if abilities_defined!(object)
end
|
#be_a(name) ⇒ Object
17
18
19
20
|
# File 'lib/grognard/guard.rb', line 17
def be_a(name)
@current_abilities = @can_dos[name.to_sym] if role_defined! name
self
end
|
#can?(action, object, subject) ⇒ Boolean
35
36
37
|
# File 'lib/grognard/guard.rb', line 35
def can?(action, object, subject)
@current_abilities.allowed_to(object, subject).include? action
end
|
#remove_role(name) ⇒ Object
26
27
28
|
# File 'lib/grognard/guard.rb', line 26
def remove_role(name)
@can_dos.delete name.to_sym
end
|
#respond_to?(meth, include_private = false) ⇒ Boolean
70
71
72
73
74
|
# File 'lib/grognard/guard.rb', line 70
def respond_to?(meth, include_private = false)
meth.to_s.start_with?("be_a_") ||
meth.to_s.start_with?("can_") ||
super
end
|
#role_defined!(name) ⇒ Object
52
53
54
|
# File 'lib/grognard/guard.rb', line 52
def role_defined!(name)
role_defined?(name) || raise_no_such_role
end
|
#role_defined?(name) ⇒ Boolean
Also known as:
can_be_a?
48
49
50
|
# File 'lib/grognard/guard.rb', line 48
def role_defined?(name)
@can_dos.key?(name.to_sym)
end
|