Class: Rushiro::Permission

Inherits:
Object
  • Object
show all
Defined in:
lib/rushiro/permission.rb

Constant Summary collapse

WildCard =
:*

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(permission) ⇒ Permission

Returns a new instance of Permission.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
# File 'lib/rushiro/permission.rb', line 7

def initialize(permission)
  raise ArgumentError.new("Invalid permission, empty string") if permission.empty?
  @original = permission
  @parts = permission.split(GSEP).map {|part| part.split(FSEP).map(&:to_sym)}
  @size = @parts.size
end

Instance Attribute Details

#originalObject (readonly)

Returns the value of attribute original.



3
4
5
# File 'lib/rushiro/permission.rb', line 3

def original
  @original
end

#partsObject (readonly)

Returns the value of attribute parts.



3
4
5
# File 'lib/rushiro/permission.rb', line 3

def parts
  @parts
end

#sizeObject (readonly)

Returns the value of attribute size.



3
4
5
# File 'lib/rushiro/permission.rb', line 3

def size
  @size
end

Instance Method Details

#==(other) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/rushiro/permission.rb', line 29

def ==(other)
  return false unless @size == other.size
  @parts.each_with_index do |part, idx|
    return false unless (part - other.parts[idx]).empty?
  end
  true
end

#implied?(perm) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rushiro/permission.rb', line 14

def implied?(perm)
  if perm.include?(FSEP)
    msg = "Field separator: #{FSEP} used in input. Be specific, one of domain, action or instance per section."
    raise ArgumentError.new(msg)
  end
  array = perm.split(GSEP).map(&:to_sym)
  array.each_with_index do |part_sym, idx|
    # If the permission has less parts than the permission being tested, everything after the number
    # of parts contained in this permission is automatically implied, so return true
    break if @size.pred < idx
    return false if (@parts[idx] & [WildCard, part_sym]).empty?
  end
  true
end

#serializeObject



37
38
39
# File 'lib/rushiro/permission.rb', line 37

def serialize
  @parts.map { |part| part.map(&:to_s).join(FSEP) }.join(GSEP)
end