Class: AccessGranted::Permission
- Inherits:
-
Object
- Object
- AccessGranted::Permission
- Defined in:
- lib/access-granted/permission.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#granted ⇒ Object
readonly
Returns the value of attribute granted.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#initialize(granted, action, subject, user = nil, conditions = {}, actions = [], block = nil) ⇒ Permission
constructor
A new instance of Permission.
- #matches_action?(action) ⇒ Boolean
- #matches_conditions?(subject) ⇒ Boolean
- #matches_hash_conditions?(subject) ⇒ Boolean
- #matches_subject?(subject) ⇒ Boolean
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
#action ⇒ Object (readonly)
Returns the value of attribute action.
3 4 5 |
# File 'lib/access-granted/permission.rb', line 3 def action @action end |
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
3 4 5 |
# File 'lib/access-granted/permission.rb', line 3 def actions @actions end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
3 4 5 |
# File 'lib/access-granted/permission.rb', line 3 def block @block end |
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
3 4 5 |
# File 'lib/access-granted/permission.rb', line 3 def conditions @conditions end |
#granted ⇒ Object (readonly)
Returns the value of attribute granted.
3 4 5 |
# File 'lib/access-granted/permission.rb', line 3 def granted @granted end |
#subject ⇒ Object (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
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
15 16 17 |
# File 'lib/access-granted/permission.rb', line 15 def matches_action?(action) @action == action end |
#matches_conditions?(subject) ⇒ 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
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
19 20 21 |
# File 'lib/access-granted/permission.rb', line 19 def matches_subject?(subject) subject == @subject || subject.class <= @subject end |