allowance 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,50 @@
1
1
  # Allowance
2
2
 
3
- TODO: Write a gem description
3
+ Allowance is a general-use permission management library that can be used
4
+ in any framework or application.
5
+
6
+ A simple Example:
7
+
8
+ ``` ruby
9
+ p = Allowance.define do |can|
10
+ can.sing!
11
+ can.play!
12
+ end
13
+
14
+ p.sing? # true
15
+ p.play? # true
16
+ p.dance? # false
17
+ ```
18
+
19
+ You can specify permissions on objects (and their classes), too:
20
+
21
+ ``` ruby
22
+ p = Allowance.define do |can|
23
+ # Everyone can view posts that have been published
24
+ can.view! Post, :published => true
25
+
26
+ # Post owners can delete their own posts
27
+ can.delete! Post, :user_id => current_user.id
28
+
29
+ # Admin users can view and delete all posts
30
+ if current_user.admin?
31
+ can.view! Post
32
+ can.delete! Post
33
+ end
34
+ end
35
+ ```
36
+
37
+ Instead of condition hashes, you can specify lambdas. This is great for model
38
+ classes that are ActiveModel based (eg. ActiveRecord, Mongoid etc.):
39
+
40
+ ``` ruby
41
+ p = Allowance.define do |can|
42
+ # Everyone can view posts that have been published
43
+ can.view! Post, lambda { |posts| posts.visible_posts }
44
+ end
45
+ ```
46
+
47
+ More documentation coming up soon.
4
48
 
5
49
  ## Installation
6
50
 
@@ -43,7 +43,7 @@ module Allowance
43
43
  if p.is_a?(Hash)
44
44
  model.where(p)
45
45
  elsif p.is_a?(Proc)
46
- model.instance_exec(&p)
46
+ p.call(model)
47
47
  else
48
48
  model
49
49
  end
@@ -1,3 +1,3 @@
1
1
  module Allowance
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/allowance.rb CHANGED
@@ -2,4 +2,7 @@ require 'allowance/version'
2
2
  require 'allowance/permissions'
3
3
 
4
4
  module Allowance
5
+ def self.define(&blk)
6
+ Permissions.new(&blk)
7
+ end
5
8
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ module Allowance
4
+ describe "#define" do
5
+ it "should return an instance of Allowance::Permissions" do
6
+ Allowance.define.should be_kind_of(Allowance::Permissions)
7
+ end
8
+
9
+ it "should make sure the passed block is executed" do
10
+ p = Allowance.define do |allow|
11
+ allow.sing!
12
+ end
13
+
14
+ insist p.sing?
15
+ end
16
+ end
17
+ end
@@ -21,6 +21,19 @@ module Allowance
21
21
  insist p.sing?
22
22
  end
23
23
 
24
+ it "should not modify the block's scope" do
25
+ yup = true
26
+ nope = false
27
+
28
+ p = Permissions.new do |can|
29
+ can.sing! if yup
30
+ can.dance! if nope
31
+ end
32
+
33
+ insist p.sing?
34
+ refuse p.dance?
35
+ end
36
+
24
37
  it "should allow verbs and objects" do
25
38
  subject.update! SomeClass
26
39
 
@@ -46,7 +59,7 @@ module Allowance
46
59
 
47
60
  model_class.should_receive(:find).and_return(model_instance)
48
61
 
49
- subject.view! model_class, lambda { some_scope }
62
+ subject.view! model_class, lambda { |r| r.some_scope }
50
63
 
51
64
  insist subject.view? model_instance
52
65
  end
@@ -56,7 +69,7 @@ module Allowance
56
69
  model = mock
57
70
  model.should_receive(:some_scope).and_return(scoped_model = mock)
58
71
 
59
- subject.view! model, lambda { some_scope }
72
+ subject.view! model, lambda { |r| r.some_scope }
60
73
  subject.scoped_model(:view, model).should == scoped_model
61
74
  end
62
75
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: allowance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-27 00:00:00.000000000 Z
12
+ date: 2012-05-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70251993567840 !ruby/object:Gem::Requirement
16
+ requirement: &70168031557120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70251993567840
24
+ version_requirements: *70168031557120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70251993566520 !ruby/object:Gem::Requirement
27
+ requirement: &70168031554220 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70251993566520
35
+ version_requirements: *70168031554220
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: watchr
38
- requirement: &70251993563400 !ruby/object:Gem::Requirement
38
+ requirement: &70168031553600 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70251993563400
46
+ version_requirements: *70168031553600
47
47
  description: A generic, but decidedly awesome authorization control layer.
48
48
  email:
49
49
  - hendrik@mans.de
@@ -63,6 +63,7 @@ files:
63
63
  - lib/allowance.rb
64
64
  - lib/allowance/permissions.rb
65
65
  - lib/allowance/version.rb
66
+ - spec/allowance_spec.rb
66
67
  - spec/permissions_spec.rb
67
68
  - spec/spec_helper.rb
68
69
  homepage: ''
@@ -90,6 +91,7 @@ signing_key:
90
91
  specification_version: 3
91
92
  summary: A generic, but decidedly awesome authorization control layer.
92
93
  test_files:
94
+ - spec/allowance_spec.rb
93
95
  - spec/permissions_spec.rb
94
96
  - spec/spec_helper.rb
95
97
  has_rdoc: