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
# File 'lib/admission/privilege.rb', line 11

def initialize name, level=nil
  @name = name.to_sym
  @level = level ? level.to_sym : BASE_LEVEL_NAME
  @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



42
43
44
# File 'lib/admission/privilege.rb', line 42

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

Instance Method Details

#dup_with_context(context = nil) ⇒ Object



21
22
23
24
25
26
# File 'lib/admission/privilege.rb', line 21

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)


28
29
30
# File 'lib/admission/privilege.rb', line 28

def eql? other
  hash == other.hash
end

#eql_or_inherits?(sought) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/admission/privilege.rb', line 32

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



17
18
19
# File 'lib/admission/privilege.rb', line 17

def inherits_from *privileges
  @inherited = privileges
end

#inspectObject



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

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

#text_keyObject



38
39
40
# File 'lib/admission/privilege.rb', line 38

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

#to_sObject



54
55
56
57
58
59
# File 'lib/admission/privilege.rb', line 54

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