Class: PapersPlease::Role
- Inherits:
-
Object
- Object
- PapersPlease::Role
- Defined in:
- lib/papers_please/role.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#permissions ⇒ Object
readonly
Returns the value of attribute permissions.
-
#predicate ⇒ Object
readonly
Returns the value of attribute predicate.
Instance Method Summary collapse
- #add_permission(actions, klass, query: nil, predicate: nil, granted_by: nil) ⇒ Object (also: #grant)
- #applies_to?(user) ⇒ Boolean
- #find_permission(action, subject) ⇒ Object
-
#initialize(name, predicate: nil) ⇒ Role
constructor
A new instance of Role.
- #permission_exists?(action, subject) ⇒ Boolean
Constructor Details
#initialize(name, predicate: nil) ⇒ Role
Returns a new instance of Role.
5 6 7 8 9 |
# File 'lib/papers_please/role.rb', line 5 def initialize(name, predicate: nil) @name = name @predicate = predicate @permissions = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/papers_please/role.rb', line 3 def name @name end |
#permissions ⇒ Object (readonly)
Returns the value of attribute permissions.
3 4 5 |
# File 'lib/papers_please/role.rb', line 3 def @permissions end |
#predicate ⇒ Object (readonly)
Returns the value of attribute predicate.
3 4 5 |
# File 'lib/papers_please/role.rb', line 3 def predicate @predicate end |
Instance Method Details
#add_permission(actions, klass, query: nil, predicate: nil, granted_by: nil) ⇒ Object Also known as: grant
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/papers_please/role.rb', line 17 def (actions, klass, query: nil, predicate: nil, granted_by: nil) prepare_actions(actions).each do |action| raise DuplicatePermission if (action, klass) if !granted_by.nil? && !valid_grant?(granted_by) raise InvalidGrant, 'granted_by must be an array of [Class, Proc]' end << ( action, actions, klass, query: query, predicate: predicate, granted_by: granted_by ) end end |
#applies_to?(user) ⇒ Boolean
11 12 13 14 15 |
# File 'lib/papers_please/role.rb', line 11 def applies_to?(user) return @predicate.call(user) if @predicate.is_a? Proc true end |
#find_permission(action, subject) ⇒ Object
38 39 40 41 42 |
# File 'lib/papers_please/role.rb', line 38 def (action, subject) .detect do || .matches? action, subject end end |
#permission_exists?(action, subject) ⇒ Boolean
44 45 46 |
# File 'lib/papers_please/role.rb', line 44 def (action, subject) !(action, subject).nil? end |