reuser 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ module ReUser
2
+ class Error < StandardError; end
3
+ class RoleNotDefined < Error
4
+ def initialize(name)
5
+ @name = name
6
+ end
7
+
8
+ def message
9
+ "Role `#{@name}` is not defined"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ module ReUser
2
+ class RoleDefinition
3
+ def initialize(definition)
4
+ @roles = {}
5
+ instance_eval &definition
6
+ end
7
+
8
+ def roles
9
+ @roles
10
+ end
11
+
12
+ def role name, permissions=[], &block
13
+ role = ReUser::Role.new(name, permissions)
14
+ yield(role) if block_given?
15
+ @roles[name] = role
16
+ end
17
+ end
18
+ end
@@ -1,6 +1,6 @@
1
1
  module ReUser
2
2
  MAJOR = 3
3
- MINOR = 0
3
+ MINOR = 1
4
4
  PATCH = 0
5
5
  VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}"
6
6
  end
data/lib/reuser.rb CHANGED
@@ -1,41 +1,42 @@
1
- require "reuser/role"
1
+ require 'reuser/role'
2
+ require 'reuser/role_definition'
3
+ require 'reuser/errors'
2
4
 
3
5
  module ReUser
4
-
5
- def self.included klass
6
-
7
- klass.instance_eval do
8
- def roles &block
9
- @@roles ||= {}
10
- yield if block_given?
11
- @@roles.freeze.keys
6
+ module ClassMethods
7
+ def roles &definition_block
8
+ if block_given?
9
+ definition = ReUser::RoleDefinition.new(definition_block)
10
+ @@roles = definition.roles
11
+ else
12
+ @@roles.keys
12
13
  end
14
+ end
13
15
 
14
- def role name, permissions=[], &block
15
- role = ( @@roles[name] ||= ReUser::Role.new(name, permissions) )
16
- yield(role) if block_given?
17
- role
18
- end
16
+ def role(name)
17
+ @@roles.fetch(name.to_sym)
18
+ rescue ::IndexError
19
+ raise RoleNotDefined.new(name)
19
20
  end
20
21
  end
21
22
 
22
- def permissions
23
- self.class.role(self.role.to_sym).permissions
23
+ def self.included(base)
24
+ base.extend ReUser::ClassMethods
24
25
  end
25
26
 
26
27
  def can? permission
27
- self.class.role(self.role.to_sym).can? permission
28
+ self.class.role(self.role).can? permission
28
29
  end
29
30
 
30
31
  def cant? permission
31
- !(can? permission)
32
+ !can?(permission)
32
33
  end
33
34
 
34
35
  def could? permission, block_args
35
- self.class.role(self.role.to_sym).could? permission, block_args
36
+ self.class.role(self.role).could? permission, block_args
36
37
  end
37
38
 
38
39
  def couldnt? permission, block_args
39
- !(could? permission, block_args)
40
+ !could?(permission, block_args)
40
41
  end
41
42
  end
@@ -32,10 +32,6 @@ describe "Classes including ReUser" do
32
32
  lambda { subject.role(:admin) }.should_not raise_error
33
33
  end
34
34
 
35
- it "takes a symbol, and an optional array" do
36
- lambda { subject.role(:admin, [:read, :write]) }.should_not raise_error
37
- end
38
-
39
35
  it "returns a ReUser::Role instance" do
40
36
  subject.role(:admin).should be_instance_of ReUser::Role
41
37
  end
@@ -54,7 +50,7 @@ describe "Classes including ReUser" do
54
50
  role(:admin)
55
51
  end
56
52
  end
57
- lambda { subject.role(:user) }.should raise_error
53
+ lambda { subject.role(:user) }.should raise_error(ReUser::RoleNotDefined)
58
54
  end
59
55
  end
60
56
  end
@@ -29,12 +29,6 @@ describe "Instances of a Class including ReUser" do
29
29
  klass.role(:admin)
30
30
  end
31
31
 
32
- describe '#permissions' do
33
- it 'returns the array of permissions on the subject\'s role' do
34
- subject.permissions.should =~ [:read, :write]
35
- end
36
- end
37
-
38
32
  specify "#can? is delegated to the ReUser::Role" do
39
33
  admin_role.should_receive :can?
40
34
  subject.can? :read
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe ReUser::RoleDefinition do
4
+ subject do
5
+ definition = proc do
6
+ role :foo
7
+ end
8
+
9
+ described_class.new definition
10
+ end
11
+
12
+ describe '#role' do
13
+ it "takes a symbol, and an optional array" do
14
+ puts subject
15
+ lambda { subject.role(:admin, [:read, :write]) }.should_not raise_error
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reuser
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-19 00:00:00.000000000 Z
12
+ date: 2012-08-14 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ReUse is an Internal DSL for Ruby to create roles and manage actions.
15
15
  email: isaac@isaacsanders.com
@@ -17,11 +17,14 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - lib/reuser/errors.rb
20
21
  - lib/reuser/role.rb
22
+ - lib/reuser/role_definition.rb
21
23
  - lib/reuser/version.rb
22
24
  - lib/reuser.rb
23
25
  - spec/reuser/class_spec.rb
24
26
  - spec/reuser/instances_spec.rb
27
+ - spec/reuser/role_definition_spec.rb
25
28
  - spec/reuser/role_spec.rb
26
29
  - spec/spec_helper.rb
27
30
  homepage: http://isaacbfsanders.com/reuser
@@ -38,7 +41,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
41
  version: '0'
39
42
  segments:
40
43
  - 0
41
- hash: -2919554755411533887
44
+ hash: -285099739461963091
42
45
  required_rubygems_version: !ruby/object:Gem::Requirement
43
46
  none: false
44
47
  requirements:
@@ -54,5 +57,6 @@ summary: An internal DSL for Ruby to make user role management simple.
54
57
  test_files:
55
58
  - spec/reuser/class_spec.rb
56
59
  - spec/reuser/instances_spec.rb
60
+ - spec/reuser/role_definition_spec.rb
57
61
  - spec/reuser/role_spec.rb
58
62
  - spec/spec_helper.rb