Class: Shark::Permissions::Rule
- Inherits:
-
Object
- Object
- Shark::Permissions::Rule
- Defined in:
- lib/shark/permissions/rule.rb
Instance Attribute Summary collapse
-
#changes ⇒ Object
readonly
Returns the value of attribute changes.
-
#effect ⇒ Object
Returns the value of attribute effect.
-
#privileges ⇒ Object
Returns the value of attribute privileges.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Boolean.
- #as_json(*_args) ⇒ Object
- #changed? ⇒ Boolean
- #clone ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(args) ⇒ Rule
constructor
A new instance of Rule.
- #privileges_as_array ⇒ Object
- #resource_model ⇒ Object
- #update(other) ⇒ Object
Constructor Details
#initialize(args) ⇒ Rule
Returns a new instance of Rule.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/shark/permissions/rule.rb', line 11 def initialize(args) symbol_args = args.symbolize_keys @resource = symbol_args.fetch(:resource) @privileges = symbol_args[:privileges] || {} normalize_privileges(@privileges) @effect = symbol_args[:effect] || 'ALLOW' @title = symbol_args[:title] @changes = Changes.new end |
Instance Attribute Details
#changes ⇒ Object (readonly)
Returns the value of attribute changes.
7 8 9 |
# File 'lib/shark/permissions/rule.rb', line 7 def changes @changes end |
#effect ⇒ Object
Returns the value of attribute effect.
6 7 8 |
# File 'lib/shark/permissions/rule.rb', line 6 def effect @effect end |
#privileges ⇒ Object
Returns the value of attribute privileges.
6 7 8 |
# File 'lib/shark/permissions/rule.rb', line 6 def privileges @privileges end |
#resource ⇒ Object
Returns the value of attribute resource.
6 7 8 |
# File 'lib/shark/permissions/rule.rb', line 6 def resource @resource end |
#title ⇒ Object
Returns the value of attribute title.
6 7 8 |
# File 'lib/shark/permissions/rule.rb', line 6 def title @title end |
Instance Method Details
#==(other) ⇒ Object
Returns Boolean.
64 65 66 |
# File 'lib/shark/permissions/rule.rb', line 64 def ==(other) resource == other.resource && effect == other.effect && privileges == other.privileges end |
#as_json(*_args) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/shark/permissions/rule.rb', line 68 def as_json(*_args) json = { 'resource' => resource, 'privileges' => privileges, 'effect' => effect, 'parent' => parent } json['title'] = title if title.present? json end |
#changed? ⇒ Boolean
42 43 44 |
# File 'lib/shark/permissions/rule.rb', line 42 def changed? changes.present? end |
#clone ⇒ Object
46 47 48 |
# File 'lib/shark/permissions/rule.rb', line 46 def clone self.class.new(as_json) end |
#empty? ⇒ Boolean
50 51 52 |
# File 'lib/shark/permissions/rule.rb', line 50 def empty? privileges.blank? end |
#privileges_as_array ⇒ Object
58 59 60 |
# File 'lib/shark/permissions/rule.rb', line 58 def privileges_as_array privileges.select { |_, v| v == true }.keys end |
#resource_model ⇒ Object
54 55 56 |
# File 'lib/shark/permissions/rule.rb', line 54 def resource_model Resource.new(resource) end |
#update(other) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/shark/permissions/rule.rb', line 22 def update(other) if resource != other.resource raise ArgumentError, "Trying to update different resource: got #{other.resource}, " \ "but expected #{resource}" end other.privileges.each do |k, v| next if privileges[k] == v old = privileges[k] privileges[k] = v next if old == 'inherited' changes.add_privilege(k, old || false, v) end self end |