reuser 0.2.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -16,9 +16,9 @@ end
16
16
  desc 'make (build and install the gem from gemspec)'
17
17
  task :make => [:build, :install]
18
18
 
19
- desc 'run all the specs in the `spec/` directory.'
20
- task :spec do
21
- Dir.glob('./spec/reuser/**/*.rb').each do |file|
22
- sh "rspec #{file}"
23
- end
19
+ require 'rspec/core/rake_task'
20
+ RSpec::Core::RakeTask.new(:spec) do |t|
21
+ t.rspec_opts = '--color'
24
22
  end
23
+
24
+ task :default => :spec
data/lib/reuser/reuser.rb CHANGED
@@ -6,7 +6,6 @@ module ReUser
6
6
  instance_eval do
7
7
  def included(subclass)
8
8
  subclass.instance_eval do
9
- @@roles = {}
10
9
  def roles(&block)
11
10
  @@roles ||= {}
12
11
  yield if block
@@ -23,35 +22,23 @@ module ReUser
23
22
  end
24
23
  role_name
25
24
  end
26
-
27
- def default(name = nil)
28
- if name
29
- @@roles[:default] = @@roles[name]
30
- else
31
- @@roles[:default]
32
- end
33
- end
34
25
  end
35
26
  subclass.class_eval do
36
27
 
37
- def role?(name)
38
- @role == self.class.role(name)
39
- end
40
-
41
- def can?(name)
42
- !!@role.can?(name)
28
+ def can?(action)
29
+ !!@role.can?(action)
43
30
  end
44
31
 
45
- def cant?(name)
46
- !@role.can?(name)
32
+ def cant?(action)
33
+ !@role.can?(action)
47
34
  end
48
35
 
49
- def could?(name, obj)
50
- @role.could?(name, obj)
36
+ def could?(action, obj)
37
+ !!@role.could?(action, obj)
51
38
  end
52
39
 
53
- def couldnt?(name, obj)
54
- !@role.could?(name, obj)
40
+ def couldnt?(action, obj)
41
+ !@role.could?(action, obj)
55
42
  end
56
43
  end
57
44
  end
data/lib/reuser/role.rb CHANGED
@@ -2,16 +2,13 @@ require_relative('../reuser')
2
2
 
3
3
  module ReUser
4
4
  class Role
5
- attr_reader :actions
6
-
7
5
  def initialize(name)
6
+ @name = name
8
7
  @actions = {}
9
8
  end
10
9
 
11
10
  def can(*names)
12
- names.each do |name|
13
- action(name)
14
- end
11
+ actions(*names)
15
12
  end
16
13
 
17
14
  def can?(name)
@@ -25,7 +22,13 @@ module ReUser
25
22
  end
26
23
 
27
24
  def could?(name, data)
28
- @actions[name] == Proc ? @actions[name].call(data) : true
25
+ if actions[name]
26
+ if actions[name].is_a?(Proc)
27
+ actions[name].call(data)
28
+ else
29
+ true
30
+ end
31
+ end
29
32
  end
30
33
 
31
34
  private
@@ -1,36 +1,4 @@
1
- require 'rspec'
2
- require 'pry'
3
- require_relative '../reuser'
4
-
5
- class TestReUser
6
- include ReUser
7
-
8
- roles do
9
- role(:admin).can(:read, :write, :execute)
10
- role(:user) do |usr|
11
- usr.can :read
12
-
13
- usr.could :execute, :read do |obj|
14
- obj == 1
15
- end
16
- end
17
- role :writer, :write
18
- role :sysadmin, [:write, :execute]
19
- default(:user)
20
- end
21
-
22
- def initialize(name = :default)
23
- @role = TestReUser.role(name)
24
- end
25
- end
26
- REUSER_METHODS = [:roles,
27
- :role,
28
- :role?,
29
- :default,
30
- :can?,
31
- :cant?,
32
- :could?,
33
- :couldnt?].sort
1
+ require_relative 'test_reuser'
34
2
 
35
3
  describe TestReUser do
36
4
  context "has included the ReUser module" do
@@ -46,9 +14,9 @@ describe TestReUser do
46
14
 
47
15
  context "has defined the :admin and :user roles," do
48
16
  context "is instantiated as test_ru" do
49
- context "set :user to the default" do
17
+ context "with role :user" do
50
18
  before do
51
- @test_ru = TestReUser.new
19
+ @test_ru = TestReUser.new(:user)
52
20
  end
53
21
  context " using the default role" do
54
22
  it "should not be able to write" do
@@ -0,0 +1,22 @@
1
+ require_relative '../reuser'
2
+
3
+ class TestReUser
4
+ include ReUser
5
+
6
+ roles do
7
+ role(:admin).can(:read, :write, :execute)
8
+ role(:user) do |usr|
9
+ usr.can :read
10
+
11
+ usr.could :execute do |obj|
12
+ obj == 1
13
+ end
14
+ end
15
+ role :writer, :write
16
+ role :sysadmin, [:write, :execute]
17
+ end
18
+
19
+ def initialize(name)
20
+ @role = TestReUser.role(name)
21
+ end
22
+ 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: 0.2.2
4
+ version: 1.0.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: 2011-10-17 00:00:00.000000000Z
12
+ date: 2011-12-25 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: isaacsanders@gmail.com
@@ -23,7 +23,8 @@ files:
23
23
  - Rakefile
24
24
  - README.md
25
25
  - spec/reuser.rb
26
- - spec/reuser/reuser.rb
26
+ - spec/reuser/test_reuser.rb
27
+ - spec/reuser/reuser_spec.rb
27
28
  homepage: http://isaacsanders.github.com/reuser
28
29
  licenses: []
29
30
  post_install_message:
@@ -44,10 +45,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
45
  version: '0'
45
46
  requirements: []
46
47
  rubyforge_project:
47
- rubygems_version: 1.8.10
48
+ rubygems_version: 1.8.6
48
49
  signing_key:
49
50
  specification_version: 3
50
51
  summary: An internal DSL for Ruby to make user role management simple.
51
52
  test_files:
52
53
  - spec/reuser.rb
53
- - spec/reuser/reuser.rb
54
+ - spec/reuser/test_reuser.rb
55
+ - spec/reuser/reuser_spec.rb