Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the array syntax to #can

  ReUser#can supports array syntax
  ReUser::Role#can supports array syntax
  • Loading branch information...
commit f8972757f00d449e875984e2bed0d4159a7b4356 1 parent 2954d9f
Isaac Sanders authored
View
14 features/declaring_roles.feature
@@ -48,3 +48,17 @@ Feature: Declaring Roles
"""
When I access "User.role(:user)"
Then I should get an error
+
+ Scenario: Declaring roles with an array of permissions
+ Given the following class:
+ """
+ class User
+ include ReUser
+
+ roles do
+ role :admin, [:read, :write, :execute]
+ end
+ end
+ """
+ When I access "User.role(:admin).permissions"
+ Then I should know that an admin can read, write, and execute
View
4 features/step_definitions/declaring_roles_steps.rb
@@ -14,6 +14,10 @@
@actual.should be_kind_of Exception
end
+Then /^I should know that an admin can read, write, and execute$/ do
+ @actual.should == [:read, :write, :execute]
+end
+
Then /^I should have an array of (\d+ roles?)$/ do |role_count|
@actual.should have(role_count).items
@actual.all? do |item|
View
8 lib/reuser.rb
@@ -8,13 +8,11 @@ def self.included klass
def roles &block
@@roles ||= {}
yield if block_given?
- @@roles.freeze
- @@roles.keys
+ @@roles.freeze.keys
end
- def role name, &block
- @@roles[name] ||= ReUser::Role.new name
- role = @@roles[name]
+ def role name, permissions=[], &block
+ role = ( @@roles[name] ||= ReUser::Role.new(name, permissions) )
yield(role) if block_given?
role
end
View
9 lib/reuser/role.rb
@@ -6,13 +6,16 @@ def permissions
@permissions.keys
end
- def initialize name
+ def initialize name, permissions=[]
@name = name
@permissions = {}
+ self.can *permissions
end
- def can permission
- @permissions[permission] = lambda {|*args| true }
+ def can *permissions
+ permissions.each do |permission|
+ @permissions[permission] = lambda {|*args| true }
+ end
end
def can? permission
View
4 spec/reuser/class_spec.rb
@@ -32,6 +32,10 @@
lambda { subject.role(:admin) }.should_not raise_error
end
+ it "takes a symbol, and an optional array" do
+ lambda { subject.role(:admin, [:read, :write]) }.should_not raise_error
+ end
+
it "returns a ReUser::Role instance" do
subject.role(:admin).should be_instance_of ReUser::Role
end
View
12 spec/reuser/role_spec.rb
@@ -7,12 +7,18 @@
role
end
- it 'is initialized with a name' do
+ it 'is initialized with a name, and optionally an array of permissions' do
role = ReUser::Role.new :admin
- role.name.should === :admin
+ role.name.should == :admin
+ role.permissions.should == []
role = ReUser::Role.new :user
- role.name.should === :user
+ role.name.should == :user
+ role.permissions.should == []
+
+ role = ReUser::Role.new :user, [:read, :write]
+ role.name.should == :user
+ role.permissions.should == [:read, :write]
end
context 'shares its name and permissions' do
Please sign in to comment.
Something went wrong with that request. Please try again.