class Shark::Permissions::Rule

Attributes

changes[R]
effect[RW]
privileges[RW]
resource[RW]
title[RW]

Public Class Methods

new(args) click to toggle source
# 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

Public Instance Methods

==(other) click to toggle source

@return Boolean @api public

# File lib/shark/permissions/rule.rb, line 64
def ==(other)
  resource == other.resource && effect == other.effect && privileges == other.privileges
end
as_json(*_args) click to toggle source
# 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?() click to toggle source
# File lib/shark/permissions/rule.rb, line 42
def changed?
  changes.present?
end
clone() click to toggle source
# File lib/shark/permissions/rule.rb, line 46
def clone
  self.class.new(as_json)
end
empty?() click to toggle source
# File lib/shark/permissions/rule.rb, line 50
def empty?
  privileges.blank?
end
privileges_as_array() click to toggle source
# File lib/shark/permissions/rule.rb, line 58
def privileges_as_array
  privileges.select { |_, v| v == true }.keys
end
resource_model() click to toggle source
# File lib/shark/permissions/rule.rb, line 54
def resource_model
  Resource.new(resource)
end
update(other) click to toggle source
# 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

Private Instance Methods

normalize_privileges(privileges) click to toggle source
# File lib/shark/permissions/rule.rb, line 82
def normalize_privileges(privileges)
  privileges.each do |k, v|
    privileges[k] = case v
                    when 'inherited'
                      'inherited'
                    when true, 'true', 1
                      true
                    when false, 'false', 0
                      false
                    else
                      false
                    end
  end

  @privileges = privileges.stringify_keys
end