reuser 3.1.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ coverage
2
+ *.gem
3
+ .rvmrc
4
+ example
5
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - ree
4
+ - 1.8.7
5
+ - 1.9.2
6
+ - 1.9.3
7
+ - ruby-head
8
+ - jruby-18mode
9
+ - jruby-19mode
10
+ - jruby-head
11
+ - rbx-18mode
12
+ - rbx-19mode
13
+ - rbx-head
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source :rubygems
2
+
3
+ gem 'rake'
4
+
5
+ group :test, :development do
6
+ gem "cucumber"
7
+ gem "rspec"
8
+ gem 'simplecov'
9
+ end
10
+
11
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Isaac Sanders
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ #ReUser [![Build Status](https://secure.travis-ci.org/isaacsanders/reuser.png?branch=master)](http://travis-ci.org/isaacsanders/reuser)
2
+
3
+ ##Purpose
4
+
5
+ Whenever you start a web app where a user has specific permissions, do you end
6
+ up writing your own solution or using something that has brain-bending
7
+ abstractions? If so, ReUser is the solution for you.
8
+
9
+ ##Description
10
+
11
+ ReUser is an Internal DSL for Ruby to create roles and manage actions.
12
+
13
+ ##Usage
14
+
15
+ Installing ReUser is easy:
16
+
17
+ gem install reuser
18
+
19
+ Now to incorporate it into a model:
20
+
21
+ ```ruby
22
+ require 'reuser'
23
+
24
+ class User
25
+ include ReUser
26
+
27
+ roles do
28
+
29
+ # declare a role with the can method, taking a list of actions.
30
+ role(:admin).can :read, :write, :execute
31
+
32
+ role :user do |usr| # pass a block, so you can
33
+ usr.can :read
34
+
35
+ # declare a role, then declare a conditional action with could.
36
+ # could takes a list of names, then assigns a test to them.
37
+ # You can then ask your model:
38
+ usr.could?(:write, 'un-owned-file')
39
+ #=> false
40
+ # or
41
+ usr.could?(:write, 'owned-file')
42
+ #=> true
43
+ # could? will pass the second argument as the block's argument'
44
+
45
+ usr.could :write do |file|
46
+ usr.owns? file
47
+ end
48
+ end
49
+
50
+ # Or you can declare a role with the name, followed by an array of names
51
+ role :writer, [:read, :write]
52
+
53
+ # Then, you can declare a default, accessible by storing :default as
54
+ # your model's role, it will point to original role.
55
+ default :user
56
+ end
57
+ end
58
+ ```
59
+
60
+ You can restrict the instances from doing things based on role:
61
+
62
+ ```ruby
63
+ def administrate
64
+ if @user.role? :admin
65
+ administer
66
+ end
67
+ end
68
+ ```
69
+
70
+ Or based on their actions:
71
+
72
+ ```ruby
73
+ def do_something_risky
74
+ if @user.can?(:do_this)
75
+ do_it
76
+ else
77
+ tell_them_to_get_out!
78
+ end
79
+ end
80
+
81
+ def do_something_with_obj
82
+ if @user.could?(:write, file)
83
+ write file
84
+ else
85
+ raise 'Not your file!'
86
+ end
87
+ end
88
+ ```
89
+
90
+ ##Issues?
91
+
92
+ Please don't hesitate to open up an issue. You can even contribute! Which brings me to...
93
+
94
+ ##Contributing!
95
+
96
+ This is open source, so I want others to help too. For now I have no plans on
97
+ adding too much more to this project, as far as functionality is concerned, but
98
+ don't let that get in your way of opening a request or forking the project and
99
+ adding something. I just ask that:
100
+
101
+ - You are polite about it.
102
+ - You test it.
103
+ - You explain it, or it is easy to read.
104
+
105
+ Following these will let us get along and make better software, quicker, and
106
+ with less bugs.(hypothetically)
107
+
108
+ I am still working on the final syntax, but we are getting closer. If you have
109
+ any suggestions on syntax, open a feature require
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :spec do
4
+ sh 'rspec --color --format=nested'
5
+ end
6
+
7
+ require 'cucumber/rake/task'
8
+ Cucumber::Rake::Task.new(:features) do |t|
9
+ t.cucumber_opts = '--color'
10
+ end
11
+
12
+ task :default => [:spec, :features]
@@ -0,0 +1,145 @@
1
+ Feature: Checking Permissions
2
+ In order to restrict my application
3
+ As a developer
4
+ I want to check permissions of my users
5
+
6
+ Scenario: Checking permissions on an empty role
7
+ Given the following class:
8
+ """
9
+ class User
10
+ include ReUser
11
+ attr_reader :role
12
+
13
+ roles do
14
+ role :admin
15
+ end
16
+
17
+ def initialize role
18
+ @role = role
19
+ end
20
+ end
21
+ """
22
+ When I create an "admin" user
23
+ And I ask if the user can read
24
+ Then I learn that they can't
25
+
26
+ Scenario: Checking permissions on a role with those permissions
27
+ Given the following class:
28
+ """
29
+ class User
30
+ include ReUser
31
+ attr_reader :role
32
+
33
+ roles do
34
+ role :admin do
35
+ can :read
36
+ end
37
+ end
38
+
39
+ def initialize role
40
+ @role = role
41
+ end
42
+ end
43
+ """
44
+ When I create an "admin" user
45
+ And I ask if the user can read
46
+ Then I learn that they can
47
+
48
+ Scenario: Checking conditional permissions
49
+ Given the following class:
50
+ """
51
+ class User
52
+ include ReUser
53
+ attr_reader :role
54
+
55
+ roles do
56
+ role :admin do
57
+ could :write do |language|
58
+ language == "English"
59
+ end
60
+ end
61
+ end
62
+
63
+ def initialize role
64
+ @role = role
65
+ end
66
+ end
67
+ """
68
+ When I create an "admin" user
69
+ And I ask if the user could write "English"
70
+ Then I learn that they can
71
+
72
+ Scenario: Checking conditional permissions
73
+ Given the following class:
74
+ """
75
+ class User
76
+ include ReUser
77
+ attr_reader :role
78
+
79
+ roles do
80
+ role :admin do
81
+ could :write do |language|
82
+ language == "English"
83
+ end
84
+ end
85
+ end
86
+
87
+ def initialize role
88
+ @role = role
89
+ end
90
+ end
91
+ """
92
+ When I create an "admin" user
93
+ And I ask if the user could write "Japanese"
94
+ Then I learn that they can't
95
+
96
+ Scenario: Checking permissions with a predicate
97
+ Given the following class:
98
+ """
99
+ class User
100
+ include ReUser
101
+ attr_reader :role
102
+
103
+ roles do
104
+ role :child do
105
+ could :read, &:old_enough?
106
+ end
107
+ end
108
+
109
+ def initialize role
110
+ @role = role
111
+ @age = 6
112
+ end
113
+
114
+ def old_enough?
115
+ @age > 5
116
+ end
117
+ end
118
+ """
119
+ When I create a "child" user
120
+ And I ask if the user can read
121
+ Then I learn that they can
122
+
123
+ Scenario: Checking permissions without state
124
+ Given the following class:
125
+ """
126
+ class User
127
+ include ReUser
128
+ attr_reader :role
129
+
130
+ roles do
131
+ role :child do
132
+ could :read do
133
+ true
134
+ end
135
+ end
136
+ end
137
+
138
+ def initialize role
139
+ @role = role
140
+ end
141
+ end
142
+ """
143
+ When I create a "child" user
144
+ And I ask if the user can read
145
+ Then I learn that they can
@@ -0,0 +1,63 @@
1
+ Feature: Declaring Roles
2
+ In order to have simple role-based permissions
3
+ As a developer
4
+ I want to declare roles in my source code
5
+
6
+ Scenario: Declaring one role
7
+ Given the following class:
8
+ """
9
+ class User
10
+ include ReUser
11
+
12
+ roles do
13
+ role :admin
14
+ end
15
+ end
16
+ """
17
+ When I access "User.roles"
18
+ Then I should have an array of 1 role
19
+
20
+ Scenario: Declaring many roles
21
+ Given the following class:
22
+ """
23
+ class User
24
+ include ReUser
25
+
26
+ roles do
27
+ role :admin
28
+ role :user
29
+ role :butcher
30
+ role :baker
31
+ role :candlestick_maker
32
+ end
33
+ end
34
+ """
35
+ When I access "User.roles"
36
+ Then I should have an array of 5 roles
37
+
38
+ Scenario: Declaring roles outside of a block raises an error
39
+ Given the following class:
40
+ """
41
+ class User
42
+ include ReUser
43
+
44
+ roles do
45
+ role :admin
46
+ end
47
+ end
48
+ """
49
+ When I access "User.role(:user)"
50
+ Then I should get an error
51
+
52
+ Scenario: Declaring roles with an array of permissions
53
+ Given the following class:
54
+ """
55
+ class User
56
+ include ReUser
57
+
58
+ roles do
59
+ role :admin, [:read, :write, :execute]
60
+ end
61
+ end
62
+ """
63
+ Then I should know that an admin can read, write, and execute
@@ -0,0 +1,33 @@
1
+ Given /^the following class:$/ do |class_code|
2
+ eval class_code, binding, "feature_user_class"
3
+ end
4
+
5
+ When /^I access "([^"]*)"$/ do |access_code|
6
+ begin
7
+ @actual = eval access_code
8
+ rescue Exception => e
9
+ @actual = e
10
+ end
11
+ end
12
+
13
+ Then /^I should get an error$/ do
14
+ @actual.should be_kind_of Exception
15
+ end
16
+
17
+ Then /^I should know that an admin can read, write, and execute$/ do
18
+ [:read, :write, :execute].each do |permission|
19
+ User.role(:admin).should be_able_to permission
20
+ end
21
+ end
22
+
23
+ Then /^I should have an array of (\d+ roles?)$/ do |role_count|
24
+ @actual.should have(role_count).items
25
+ @actual.all? do |item|
26
+ item.should be_instance_of Symbol
27
+ end
28
+ end
29
+
30
+ Transform /(\d+) roles?/ do |role_count|
31
+ Integer(role_count)
32
+ end
33
+
@@ -0,0 +1,7 @@
1
+ Then /^I should have know my role's name is "([^"]*)"$/ do |expected|
2
+ @actual.should == expected.to_sym
3
+ end
4
+
5
+ Then /^I should know that admins can read and write$/ do
6
+ @actual.should =~ [:read, :write]
7
+ end
@@ -0,0 +1,19 @@
1
+ When /^I ask if the user could write "([^"]*)"$/ do |language|
2
+ @expected = @user.could? :write, language
3
+ end
4
+
5
+ When /^I create an? "([^"]*)" user$/ do |role_name|
6
+ @user = User.new(role_name.to_sym)
7
+ end
8
+
9
+ When /^I ask if the user can read$/ do
10
+ @expected = !!(@user.could? :read)
11
+ end
12
+
13
+ Then /^I learn that they can't$/ do
14
+ @expected.should be_false
15
+ end
16
+
17
+ Then /^I learn that they can$/ do
18
+ @expected.should be_true
19
+ end
@@ -0,0 +1,15 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter '/spec/'
5
+ add_filter '/features/'
6
+ end
7
+ end
8
+ $: << 'lib'
9
+ require 'reuser'
10
+
11
+ After do
12
+ User.instance_eval do
13
+ class_variable_set(:@@roles, nil)
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module Roles
2
+ def role?
3
+ kind_of? ReUser::Role
4
+ end
5
+ end
6
+
7
+ World(Roles)
data/lib/reuser/role.rb CHANGED
@@ -1,13 +1,6 @@
1
1
  module ReUser
2
2
  class Role
3
- attr_reader :name
4
-
5
- def permissions
6
- @permissions.keys
7
- end
8
-
9
- def initialize name, permissions=[]
10
- @name = name
3
+ def initialize *permissions
11
4
  @permissions = {}
12
5
  self.can *permissions
13
6
  end
@@ -19,7 +12,7 @@ module ReUser
19
12
  end
20
13
 
21
14
  def can? permission
22
- @permissions[permission].is_a? Proc
15
+ @permissions.has_key?(permission) && @permissions[permission].is_a?(Proc)
23
16
  end
24
17
 
25
18
  def could permission, &block
@@ -27,8 +20,13 @@ module ReUser
27
20
  @permissions[permission] = block
28
21
  end
29
22
 
30
- def could? permission, block_args
31
- @permissions[permission].call(block_args)
23
+ def could? permission, *block_args
24
+ if @permissions.has_key?(permission)
25
+ @permissions[permission].call(*block_args)
26
+ else
27
+ false
28
+ end
32
29
  end
30
+ alias_method :able_to?, :could?
33
31
  end
34
32
  end
@@ -1,3 +1,5 @@
1
+ require 'reuser/role'
2
+
1
3
  module ReUser
2
4
  class RoleDefinition
3
5
  def initialize(definition)
@@ -10,8 +12,8 @@ module ReUser
10
12
  end
11
13
 
12
14
  def role name, permissions=[], &block
13
- role = ReUser::Role.new(name, permissions)
14
- yield(role) if block_given?
15
+ role = ReUser::Role.new(*permissions)
16
+ role.instance_eval &block if block_given?
15
17
  @roles[name] = role
16
18
  end
17
19
  end
@@ -1,6 +1,3 @@
1
1
  module ReUser
2
- MAJOR = 3
3
- MINOR = 1
4
- PATCH = 0
5
- VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}"
2
+ VERSION = "3.1.1"
6
3
  end
data/lib/reuser.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'reuser/role'
2
1
  require 'reuser/role_definition'
3
2
  require 'reuser/errors'
4
3
 
@@ -32,11 +31,14 @@ module ReUser
32
31
  !can?(permission)
33
32
  end
34
33
 
35
- def could? permission, block_args
36
- self.class.role(self.role).could? permission, block_args
34
+ def could? permission, *block_args
35
+ if block_args.empty?
36
+ block_args << self
37
+ end
38
+ self.class.role(self.role).could? permission, *block_args
37
39
  end
38
40
 
39
- def couldnt? permission, block_args
40
- !could?(permission, block_args)
41
+ def couldnt? permission, *block_args
42
+ !could?(permission, *block_args)
41
43
  end
42
44
  end
data/reuser.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'reuser/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "reuser"
8
+ gem.version = ReUser::VERSION
9
+ gem.authors = ["Isaac Sanders"]
10
+ gem.email = ["isaac@isaacbfsanders.com"]
11
+ gem.description = %q{ReUser is a DSL for Ruby to create roles and manage actions.}
12
+ gem.summary = %q{An internal DSL for Ruby to make user role management simple.}
13
+ gem.homepage = "http://isaacbfsanders.com/reuser"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -11,7 +11,6 @@ describe ReUser::RoleDefinition do
11
11
 
12
12
  describe '#role' do
13
13
  it "takes a symbol, and an optional array" do
14
- puts subject
15
14
  lambda { subject.role(:admin, [:read, :write]) }.should_not raise_error
16
15
  end
17
16
  end
@@ -2,35 +2,22 @@ require 'spec_helper'
2
2
 
3
3
  describe ReUser::Role do
4
4
  subject do
5
- role = ReUser::Role.new(:admin)
5
+ role = ReUser::Role.new
6
6
  role.can(:read)
7
7
  role
8
8
  end
9
9
 
10
- it 'is initialized with a name, and optionally an array of permissions' do
11
- role = ReUser::Role.new :admin
12
- role.name.should == :admin
13
- role.permissions.should == []
14
-
15
- role = ReUser::Role.new :user
16
- role.name.should == :user
17
- role.permissions.should == []
18
-
19
- role = ReUser::Role.new :user, [:read, :write]
20
- role.name.should == :user
21
- role.permissions.should =~ [:read, :write]
22
- end
23
-
24
- context 'shares its name and permissions' do
25
- its(:name) { should === :admin }
26
- its(:permissions) { should === [:read] }
10
+ it 'is initialized with an optional array of permissions' do
11
+ role = ReUser::Role.new :read, :write
12
+ role.should be_able_to :read
13
+ role.should be_able_to :write
27
14
  end
28
15
 
29
16
  it 'permissions are added and checked with #can and #can?' do
30
- subject.can?(:read).should be_true
31
- subject.can?(:write).should be_false
17
+ subject.should be_able_to :read
18
+ subject.should_not be_able_to :write
32
19
  subject.can(:write)
33
- subject.can?(:write).should be_true
20
+ subject.should be_able_to :write
34
21
  end
35
22
 
36
23
  it 'you need to supply #could with a test block' do
data/spec/spec_helper.rb CHANGED
@@ -1 +1,8 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter '/spec/'
5
+ add_filter '/features/'
6
+ end
7
+ end
1
8
  require 'reuser'
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.1.0
4
+ version: 3.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,34 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-14 00:00:00.000000000 Z
12
+ date: 2012-08-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: ReUse is an Internal DSL for Ruby to create roles and manage actions.
15
- email: isaac@isaacsanders.com
14
+ description: ReUser is a DSL for Ruby to create roles and manage actions.
15
+ email:
16
+ - isaac@isaacbfsanders.com
16
17
  executables: []
17
18
  extensions: []
18
19
  extra_rdoc_files: []
19
20
  files:
21
+ - .gitignore
22
+ - .travis.yml
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - features/checking_permissions.feature
28
+ - features/declaring_roles.feature
29
+ - features/step_definitions/declaring_roles_steps.rb
30
+ - features/step_definitions/differentiation_steps.rb
31
+ - features/step_definitions/permission_steps.rb
32
+ - features/support/env.rb
33
+ - features/support/predicates.rb
34
+ - lib/reuser.rb
20
35
  - lib/reuser/errors.rb
21
36
  - lib/reuser/role.rb
22
37
  - lib/reuser/role_definition.rb
23
38
  - lib/reuser/version.rb
24
- - lib/reuser.rb
39
+ - reuser.gemspec
25
40
  - spec/reuser/class_spec.rb
26
41
  - spec/reuser/instances_spec.rb
27
42
  - spec/reuser/role_definition_spec.rb
@@ -41,13 +56,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
41
56
  version: '0'
42
57
  segments:
43
58
  - 0
44
- hash: -285099739461963091
59
+ hash: -1602940265211324019
45
60
  required_rubygems_version: !ruby/object:Gem::Requirement
46
61
  none: false
47
62
  requirements:
48
63
  - - ! '>='
49
64
  - !ruby/object:Gem::Version
50
65
  version: '0'
66
+ segments:
67
+ - 0
68
+ hash: -1602940265211324019
51
69
  requirements: []
52
70
  rubyforge_project:
53
71
  rubygems_version: 1.8.24
@@ -55,6 +73,13 @@ signing_key:
55
73
  specification_version: 3
56
74
  summary: An internal DSL for Ruby to make user role management simple.
57
75
  test_files:
76
+ - features/checking_permissions.feature
77
+ - features/declaring_roles.feature
78
+ - features/step_definitions/declaring_roles_steps.rb
79
+ - features/step_definitions/differentiation_steps.rb
80
+ - features/step_definitions/permission_steps.rb
81
+ - features/support/env.rb
82
+ - features/support/predicates.rb
58
83
  - spec/reuser/class_spec.rb
59
84
  - spec/reuser/instances_spec.rb
60
85
  - spec/reuser/role_definition_spec.rb