Class: Admission::Status
- Inherits:
-
Object
- Object
- Admission::Status
- Defined in:
- lib/admission/status.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#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.
- #instantiate_arbitration(*args) ⇒ Object
- #request!(*args) ⇒ Object
Constructor Details
#initialize(person, privileges, rules, arbiter) ⇒ Status
Returns a new instance of Status.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/admission/status.rb', line 6 def initialize person, privileges, rules, arbiter @person = person @rules = rules @arbiter = arbiter @privileges = if privileges.nil? || privileges.empty? nil else # sort privileges # convenient as for arbitration doesn't have to switch between contexts too often 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
#debug ⇒ Object
Returns the value of attribute debug.
4 5 6 |
# File 'lib/admission/status.rb', line 4 def debug @debug end |
#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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/admission/status.rb', line 55 def allowed_in_contexts *args return [] unless privileges arbitration = instantiate_arbitration *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
27 28 29 30 |
# File 'lib/admission/status.rb', line 27 def can? *args return false unless privileges process_request instantiate_arbitration(*args) end |
#cannot?(*args) ⇒ Boolean
32 33 34 |
# File 'lib/admission/status.rb', line 32 def cannot? *args !can?(*args) end |
#has?(sought) ⇒ Boolean
44 45 46 47 48 49 |
# File 'lib/admission/status.rb', line 44 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 |
#instantiate_arbitration(*args) ⇒ Object
51 52 53 |
# File 'lib/admission/status.rb', line 51 def instantiate_arbitration *args @arbiter.new person, rules, *args end |