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 = {}, actions = [], block = nil) ⇒ Permission

Returns a new instance of Permission.



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

def initialize(granted, action, subject, user = nil, conditions = {}, actions = [], block = nil)
  @action     = action
  @user       = user
  @granted    = granted
  @subject    = subject
  @conditions = conditions
  @actions    = actions
  @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

#actionsObject (readonly)

Returns the value of attribute actions.



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

def actions
  @actions
end

#blockObject (readonly)

Returns the value of attribute block.



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

def block
  @block
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



47
48
49
# File 'lib/access-granted/permission.rb', line 47

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/access-granted/permission.rb', line 40

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

#matches_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#matches_conditions?(subject) ⇒ Boolean

Returns:

  • (Boolean)


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

def matches_conditions?(subject)
  if @block
    @block.call(subject, @user)
  elsif !@conditions.empty?
    matches_hash_conditions?(subject)
  else
    true
  end
end

#matches_hash_conditions?(subject) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/access-granted/permission.rb', line 33

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)


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

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