reuser 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,13 +18,33 @@
18
18
  include ReUser
19
19
 
20
20
  roles do
21
- role(:admin).can :read, :write, :execute
22
- role :user do |usr|
21
+
22
+ # declare a role with the can method, taking a list of actions.
23
+ role(:admin).can :read, :write, :execute
24
+
25
+ role :user do |usr| # pass a block, so you can
23
26
  usr.can :read
24
- usr.could :write {|obj| usr.owns?(obj)}
25
- usr.cant :execute
27
+
28
+ # declare a role, then declare a conditional action with could.
29
+ # could takes a list of names, then assigns a test to them.
30
+ # You can then ask your model:
31
+ ##usr.could?(:write, 'un-owned-file')
32
+ ###=> false
33
+ # or
34
+ ##usr.could?(:write, 'owned-file')
35
+ ###=> true
36
+ # could? will pass the second argument as the block's argument'
37
+
38
+ usr.could :write do |file|
39
+ usr.owns? file
40
+ end
26
41
  end
42
+
43
+ # Or you can declare a role with the name, followed by an array of names
27
44
  role :writer, [:read, :write]
45
+
46
+ # Then, you can declare a default, accessible by storing :default as
47
+ # your model's role, it will point to original role.
28
48
  default :user
29
49
  end
30
50
  end
data/lib/reuser/reuser.rb CHANGED
@@ -34,35 +34,24 @@ module ReUser
34
34
  end
35
35
  subclass.class_eval do
36
36
 
37
- def role(name = nil)
38
- if name
39
- if @@roles[name]
40
- @role = @@roles[name]
41
- else
42
- raise NoRoleError, "No role #{name} defined for #{self}"
43
- end
44
- end
45
- @role
46
- end
47
-
48
37
  def role?(name)
49
- role == self.class.role(name)
38
+ @role == self.class.role(name)
50
39
  end
51
40
 
52
41
  def can?(name)
53
- !!role.can?(name)
42
+ !!@role.can?(name)
54
43
  end
55
44
 
56
45
  def cant?(name)
57
- !role.can?(name)
46
+ !@role.can?(name)
58
47
  end
59
48
 
60
49
  def could?(name, obj)
61
- role.could?(name, obj)
50
+ @role.could?(name, obj)
62
51
  end
63
52
 
64
53
  def couldnt?(name, obj)
65
- !role.could?(name, obj)
54
+ !@role.could?(name, obj)
66
55
  end
67
56
  end
68
57
  end
@@ -20,7 +20,7 @@ class TestReUser
20
20
  end
21
21
 
22
22
  def initialize(name = :default)
23
- @role = role(name)
23
+ @role = TestReUser.role(name)
24
24
  end
25
25
  end
26
26
  REUSER_METHODS = [:roles,
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: 0.2.0
4
+ version: 0.2.1
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: 2011-10-12 00:00:00.000000000Z
12
+ date: 2011-10-16 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: ReUse is an Internal DSL for Ruby to create roles and manage actions.
15
15
  email: isaacsanders@gmail.com