Class: Shark::Permissions::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/shark/permissions/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Resource

Returns a new instance of Resource.



8
9
10
11
12
13
14
15
16
# File 'lib/shark/permissions/resource.rb', line 8

def initialize(value)
  @name = case value
          when String
            value
          when Array
            value.map(&:to_s).join(Shark::Permissions.delimiter)
          end
  @parts = @name.split(Shark::Permissions.delimiter)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/shark/permissions/resource.rb', line 6

def name
  @name
end

#partsObject (readonly)

Returns the value of attribute parts.



6
7
8
# File 'lib/shark/permissions/resource.rb', line 6

def parts
  @parts
end

Instance Method Details

#ancestorsObject



27
28
29
# File 'lib/shark/permissions/resource.rb', line 27

def ancestors
  ancestors_and_self[0..-2]
end

#ancestors_and_selfObject



18
19
20
21
22
23
24
25
# File 'lib/shark/permissions/resource.rb', line 18

def ancestors_and_self
  names = []
  parts.each_with_index do |_, i|
    names << parts[0..i].join(Shark::Permissions.delimiter)
  end

  names
end

#parentObject



31
32
33
34
# File 'lib/shark/permissions/resource.rb', line 31

def parent
  parent_name = parts[0..-2].join(Shark::Permissions.delimiter)
  parent_name.presence
end

#subresource_of?(value) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/shark/permissions/resource.rb', line 36

def subresource_of?(value)
  return true if name == value

  regexp = value.gsub('*', '[a-z\-_0-9]*')
  "#{name}::".match(/\A#{regexp}::/).present?
end

#super_resource_of?(value) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/shark/permissions/resource.rb', line 43

def super_resource_of?(value)
  return true if name == value

  regexp = name.gsub('*', '[a-z\-_0-9]*')
  "#{value}::".match(/\A#{regexp}::/).present?
end

#to_sObject



54
55
56
# File 'lib/shark/permissions/resource.rb', line 54

def to_s
  name
end

#wildcard?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/shark/permissions/resource.rb', line 50

def wildcard?
  parts.last == Shark::Permissions.any_matcher
end