Class: Roleback::Definitions::Resource

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

Constant Summary collapse

DEFAULT_ACTION_PATH =
[:create, :show, :update, :destroy, :index, :new, :edit]

Instance Attribute Summary collapse

Attributes inherited from RuleBased

#resource, #role

Instance Method Summary collapse

Methods inherited from RuleBased

#<=>, #can, #cannot

Constructor Details

#initialize(name, role:, scope: ::Roleback::ANY, options: {}, &block) ⇒ Resource

Returns a new instance of Resource.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/roleback/definitions/resource.rb', line 8

def initialize(name, role:, scope: ::Roleback::ANY, options: {}, &block)
	@name = name
	@role = role
	@scope = scope
	@options = options

	super(role: role, resource: self, scope: scope)

	validate_options!

	# create rules for each action
	selected_actions.each do |action|
		do_rule(role: @role, resource: self, scope: @scope, action: action, outcome: ::Roleback::ALLOW)
	end

	instance_eval(&block) if block_given?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



31
32
33
34
35
# File 'lib/roleback/definitions/resource.rb', line 31

def ==(other)
	return false unless other.respond_to?(:name)

	other.name == name
end

#match(resource) ⇒ Object



26
27
28
29
# File 'lib/roleback/definitions/resource.rb', line 26

def match(resource)
	to_check = resource.is_a?(::Roleback::Definitions::Resource) ? resource.name.to_s : resource.to_s
	@name.to_s == to_check || @name == ::Roleback.any || resource == ::Roleback.any
end