Class: Admission::Status
- Inherits:
-
Object
- Object
- Admission::Status
- Defined in:
- lib/admission/status.rb
Instance Attribute Summary collapse
-
#person ⇒ Object
readonly
Returns the value of attribute person.
-
#privileges ⇒ Object
readonly
Returns the value of attribute privileges.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
- #allowed_in_contexts(*args) ⇒ Object
- #can?(*args) ⇒ Boolean
- #cannot?(*args) ⇒ Boolean
- #has?(sought) ⇒ Boolean
-
#initialize(person, privileges, rules, arbiter) ⇒ Status
constructor
A new instance of Status.
- #request!(*args) ⇒ Object
Constructor Details
#initialize(person, privileges, rules, arbiter) ⇒ Status
Returns a new instance of Status.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/admission/status.rb', line 5 def initialize person, privileges, rules, arbiter @person = person @rules = rules @arbiter = arbiter @privileges = if privileges.nil? || privileges.empty? nil else grouped = privileges.inject Hash.new do |h, p| hash = p.context.hash rescue nil.hash (h[hash] ||= []) << p h end grouped.values.flatten.freeze end end |
Instance Attribute Details
#person ⇒ Object (readonly)
Returns the value of attribute person.
3 4 5 |
# File 'lib/admission/status.rb', line 3 def person @person end |
#privileges ⇒ Object (readonly)
Returns the value of attribute privileges.
3 4 5 |
# File 'lib/admission/status.rb', line 3 def privileges @privileges end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
3 4 5 |
# File 'lib/admission/status.rb', line 3 def rules @rules end |
Instance Method Details
#allowed_in_contexts(*args) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/admission/status.rb', line 48 def allowed_in_contexts *args return [] unless privileges arbitration = @arbiter.new person, rules, *args privileges.reduce [] do |list, privilege| context = privilege.context unless list.include? context arbitration.prepare_sitting context list << context if arbitration.rule_per_privilege(privilege).eql? true end list end end |
#can?(*args) ⇒ Boolean
24 25 26 27 |
# File 'lib/admission/status.rb', line 24 def can? *args return false unless privileges process_request @arbiter.new(person, rules, *args) end |
#cannot?(*args) ⇒ Boolean
29 30 31 |
# File 'lib/admission/status.rb', line 29 def cannot? *args !can?(*args) end |
#has?(sought) ⇒ Boolean
41 42 43 44 45 46 |
# File 'lib/admission/status.rb', line 41 def has? sought return false unless privileges list = sought.context ? privileges.select{|p| p.context == sought.context} : privileges list.any?{|p| p.eql_or_inherits? sought} end |