Class: Roleback::Definitions::Role

Inherits:
RuleBased
  • Object
show all
Defined in:
lib/roleback/definitions/role.rb

Instance Attribute Summary collapse

Attributes inherited from RuleBased

#role

Instance Method Summary collapse

Methods inherited from RuleBased

#<=>, #can, #cannot

Constructor Details

#initialize(name, parents: nil) ⇒ Role

Returns a new instance of Role.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/roleback/definitions/role.rb', line 7

def initialize(name, parents: nil)
	@name = name
	@rule_book = ::Roleback::RuleBook.new(self)
	@scopes = {}
	@resources = {}

	if parents
		if parents.is_a?(Symbol)
			@parents = [parents]
		elsif parents.is_a?(Array)
			# check for duplicates
			raise ::Roleback::BadConfiguration, "Duplicate parents found for role #{name}" if parents.uniq.length != parents.length

			@parents = parents
		else
			raise ::Roleback::BadConfiguration, "Parent must be a symbol or an array of symbols"
		end
	else
		@parents = nil
	end

	super(role: self, resource: ::Roleback::ANY, scope: ::Roleback::ANY)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/roleback/definitions/role.rb', line 4

def name
  @name
end

#parentsObject (readonly)

Returns the value of attribute parents.



5
6
7
# File 'lib/roleback/definitions/role.rb', line 5

def parents
  @parents
end

Instance Method Details

#add_rule(rule) ⇒ Object



39
40
41
# File 'lib/roleback/definitions/role.rb', line 39

def add_rule(rule)
	@rule_book.add(rule)
end

#inheritObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/roleback/definitions/role.rb', line 61

def inherit
	return if @parents.nil?

	new_rules = do_inherit

	if new_rules.empty?
		# no rules to inherit
		return
	end

	# add the new rules to the rule book
	self.rules.clear_rules
	new_rules.each do |rule|
		self.rules.add(rule)
	end
end

#keysObject



35
36
37
# File 'lib/roleback/definitions/role.rb', line 35

def keys
	@rule_book.rules.keys
end

#resource(name, options = {}, &block) ⇒ Object



43
44
45
46
47
48
# File 'lib/roleback/definitions/role.rb', line 43

def resource(name, options = {}, &block)
	raise ::Roleback::BadConfiguration, "Resource #{name} already defined" if @resources[name]

	resource = ::Roleback::Definitions::Resource.new(name, role: self, options: options, &block)
	@resources[name] = resource
end

#rulesObject



31
32
33
# File 'lib/roleback/definitions/role.rb', line 31

def rules
	@rule_book
end

#scope(name, &block) ⇒ Object



50
51
52
53
54
55
# File 'lib/roleback/definitions/role.rb', line 50

def scope(name, &block)
	raise ::Roleback::BadConfiguration, "Scope #{name} already defined" if @scopes[name]

	scope = ::Roleback::Definitions::Scope.new(name, role: self, &block)
	@scopes[name] = scope
end

#to_sObject



57
58
59
# File 'lib/roleback/definitions/role.rb', line 57

def to_s
	self.name.to_s
end