Class: Admission::Privilege

Inherits:
Object
  • Object
show all
Defined in:
lib/admission/privilege.rb

Constant Summary collapse

RESERVED_ID =
:'^'.freeze
TOP_LEVEL_KEY =
RESERVED_ID
BASE_LEVEL_NAME =
:base
SEPARATOR =
'-'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, level = nil) ⇒ Privilege

Returns a new instance of Privilege.



11
12
13
14
15
16
17
# File 'lib/admission/privilege.rb', line 11

def initialize name, level=nil
  name = name.to_sym
  @name = name
  level = level ? level.to_sym : BASE_LEVEL_NAME
  @level = level
  @hash = [name, level].hash
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/admission/privilege.rb', line 9

def context
  @context
end

#hashObject (readonly)

Returns the value of attribute hash.



8
9
10
# File 'lib/admission/privilege.rb', line 8

def hash
  @hash
end

#inheritedObject (readonly)

Returns the value of attribute inherited.



9
10
11
# File 'lib/admission/privilege.rb', line 9

def inherited
  @inherited
end

#levelObject (readonly)

Returns the value of attribute level.



8
9
10
# File 'lib/admission/privilege.rb', line 8

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/admission/privilege.rb', line 8

def name
  @name
end

Class Method Details

.split_text_key(value) ⇒ Object



44
45
46
# File 'lib/admission/privilege.rb', line 44

def self.split_text_key value
  return value.split(SEPARATOR)
end

Instance Method Details

#dup_with_context(context = nil) ⇒ Object



23
24
25
26
27
28
# File 'lib/admission/privilege.rb', line 23

def dup_with_context context=nil
  return self if context.nil?
  with_context = dup
  with_context.instance_variable_set :@context, context
  with_context
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/admission/privilege.rb', line 30

def eql? other
  hash == other.hash
end

#eql_or_inherits?(sought) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/admission/privilege.rb', line 34

def eql_or_inherits? sought
  return true if eql? sought
  return false unless inherited
  inherited.any?{|pi| pi.eql_or_inherits? sought}
end

#inherits_from(*privileges) ⇒ Object



19
20
21
# File 'lib/admission/privilege.rb', line 19

def inherits_from *privileges
  @inherited = privileges
end

#inspectObject



48
49
50
51
52
53
54
# File 'lib/admission/privilege.rb', line 48

def inspect
  "#<#{[
      'Privilege',
      "key=#{text_key}",
      (inherited && "inherited=[#{inherited.map(&:text_key).join ','}]")
  ].compact.join ' '}>"
end

#text_keyObject



40
41
42
# File 'lib/admission/privilege.rb', line 40

def text_key
  @text_key ||= level == BASE_LEVEL_NAME ? name.to_s : "#{name}#{SEPARATOR}#{level}"
end

#to_sObject



56
57
58
59
60
61
# File 'lib/admission/privilege.rb', line 56

def to_s
  [
      "privilege #{text_key}",
      (context && ", context #{context}")
  ].join ''
end