lib/reuser/role.rb in reuser-1.0.0 vs lib/reuser/role.rb in reuser-2.0.0
- old
+ new
@@ -1,46 +1,34 @@
-require_relative('../reuser')
-
module ReUser
class Role
- def initialize(name)
- @name = name
- @actions = {}
- end
+ attr_reader :name
- def can(*names)
- actions(*names)
+ def permissions
+ @permissions.keys
end
- def can?(name)
- actions[name]
+ def initialize name, permissions=[]
+ @name = name
+ @permissions = {}
+ self.can *permissions
end
- def could(*names, &test)
- names.each do |name|
- action(name, test)
+ def can *permissions
+ permissions.each do |permission|
+ @permissions[permission] = lambda {|*args| true }
end
end
- def could?(name, data)
- if actions[name]
- if actions[name].is_a?(Proc)
- actions[name].call(data)
- else
- true
- end
- end
+ def can? permission
+ @permissions[permission].is_a? Proc
end
- private
- def actions(*names)
- names.each do |name|
- action(name)
- end
- @actions
- end
+ def could permission, &block
+ raise "#could requires a block" unless block_given?
+ @permissions[permission] = block
+ end
- def action(name, test = true)
- @actions[name] = test
- end
+ def could? permission, block_args
+ @permissions[permission].call(block_args)
+ end
end
end