Class: Admission::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/admission/status.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#debugObject

Returns the value of attribute debug.



4
5
6
# File 'lib/admission/status.rb', line 4

def debug
  @debug
end

#personObject (readonly)

Returns the value of attribute person.



3
4
5
# File 'lib/admission/status.rb', line 3

def person
  @person
end

#privilegesObject (readonly)

Returns the value of attribute privileges.



3
4
5
# File 'lib/admission/status.rb', line 3

def privileges
  @privileges
end

#rulesObject (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

Returns:

  • (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

Returns:

  • (Boolean)


32
33
34
# File 'lib/admission/status.rb', line 32

def cannot? *args
  !can?(*args)
end

#has?(sought) ⇒ Boolean

Returns:

  • (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

#request!(*args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/admission/status.rb', line 36

def request! *args
  can?(*args) || begin
    exception = Admission::Denied.new self, *args
    yield exception if block_given?
    raise exception
  end
end