Class: AccessGranted::Permission

Inherits:
Object
  • Object
show all
Defined in:
lib/access-granted/permission.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(granted, action, subject, user = nil, conditions = {}, block = nil) ⇒ Permission

Returns a new instance of Permission.



5
6
7
8
9
10
11
12
# File 'lib/access-granted/permission.rb', line 5

def initialize(granted, action, subject, user = nil, conditions = {}, block = nil)
  @action     = action
  @user       = user
  @granted    = granted
  @subject    = subject
  @conditions = conditions
  @block      = block
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



3
4
5
# File 'lib/access-granted/permission.rb', line 3

def action
  @action
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



3
4
5
# File 'lib/access-granted/permission.rb', line 3

def conditions
  @conditions
end

#grantedObject (readonly)

Returns the value of attribute granted.



3
4
5
# File 'lib/access-granted/permission.rb', line 3

def granted
  @granted
end

#subjectObject (readonly)

Returns the value of attribute subject.



3
4
5
# File 'lib/access-granted/permission.rb', line 3

def subject
  @subject
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
# File 'lib/access-granted/permission.rb', line 44

def ==(other)
  eql?(other)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/access-granted/permission.rb', line 37

def eql?(other)
  other.class == self.class &&
    @action == other.action &&
      @subject == other.subject &&
        @granted == other.granted
end

#matches_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/access-granted/permission.rb', line 14

def matches_action?(action)
  @action == action
end

#matches_conditions?(subject) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/access-granted/permission.rb', line 22

def matches_conditions?(subject)
  if @block && !subject.is_a?(Class)
    @block.call(subject, @user)
  else
    matches_hash_conditions?(subject)
  end
end

#matches_hash_conditions?(subject) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/access-granted/permission.rb', line 30

def matches_hash_conditions?(subject)
  @conditions.each_pair do |name, value|
    return false if subject.send(name) != value
  end
  true
end

#matches_subject?(subject) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/access-granted/permission.rb', line 18

def matches_subject?(subject)
  subject == @subject || subject.class <= @subject
end