Skip to content
This repository
Browse code

initalization with args, up to 0.1.3

  • Loading branch information...
commit 49068ca91596d95fa8112494d22e7909bffbea40 1 parent 50cfc57
dzaporozhets authored
6 CHANGELOG
... ... @@ -1,3 +1,9 @@
  1 +v 0.1.3
  2 + - initialization with pack as args
  3 + Ex. Six.new(:book => BookRules.new)
  4 + - initialization with multiple packs
  5 + Ex. Six.new(:book => BookRules.new, :author => AuthorRules.new)
  6 +
1 7 v 0.1.2
2 8 - allowed?(action, object, subject) -> allowed?(object, action, subject)
3 9 - use & use! are now aliases for use_pack & use_pack!
15 README.markdown
Source Rendered
@@ -151,6 +151,21 @@ end
151 151 link_to 'Edit', edit_book_path(book) if can?(@author, :edit_book, book)
152 152 ```
153 153
  154 +### :initialization
  155 +
  156 +```ruby
  157 +# simple
  158 +abilities = Six.new
  159 +
  160 +# with rules
  161 +abilities = Six.new(:book_rules => BookRules) # same as Six.new & add(:bok_rules, BookRules)
  162 +
  163 +# with more
  164 +abilities = Six.new(:book => BookRules,
  165 + :auth => AuthRules,
  166 + :managment => ManagerRules)
  167 +```
  168 +
154 169 ### :use
155 170
156 171 ```ruby
12 lib/six.rb
@@ -11,12 +11,22 @@ def message
11 11 end
12 12 end
13 13
  14 + class InitializeArgumentError < StandardError
  15 + def message
  16 + "Six.new require hash as pack argument in format {:name_of_pack => PackRules.new}"
  17 + end
  18 + end
  19 +
14 20 attr_reader :rules_packs
15 21 attr_reader :current_rule_pack
16 22
17   - def initialize()
  23 + def initialize(packs={})
  24 + raise InitializeArgumentError.new unless packs.kind_of?(Hash)
  25 +
18 26 @rules_packs = {}
19 27 @current_rule_pack = nil
  28 +
  29 + packs.each { |key, pack| add_pack!(key, pack) }
20 30 end
21 31
22 32 # Set current pack from stored packs by key
4 six.gemspec
... ... @@ -1,7 +1,7 @@
1 1 Gem::Specification.new do |s|
2 2 s.name = 'six'
3   - s.version = '0.1.2'
4   - s.date = '2011-08-27'
  3 + s.version = '0.1.3'
  4 + s.date = '2011-09-01'
5 5 s.summary = "six"
6 6 s.description = "Very simple authorization gem"
7 7 s.authors = ["Dmitriy Zaporozhets"]
36 spec/six_initialize_spec.rb
... ... @@ -0,0 +1,36 @@
  1 +require "./spec/spec_helper"
  2 +require "./lib/six"
  3 +
  4 +describe Six, "initialize" do
  5 + describe "initalization" do
  6 + before do
  7 + @jim = Author.new("Jim")
  8 + @mike = Author.new("Mike")
  9 +
  10 + @jims_book = Book.new("The Game", @jim)
  11 + @mikes_book = Book.new("Life", @mike)
  12 + end
  13 +
  14 + it "should create authorization object" do
  15 + Six.new.should be_kind_of(Six)
  16 + end
  17 +
  18 + it "should raise error if invalid argument passed" do
  19 + lambda { Six.new("wrong argument") }.should raise_error Six::InitializeArgumentError
  20 + end
  21 +
  22 + it "should create authorization object" do
  23 + Six.new(:book_rules => BookRules.new).should be_kind_of(Six)
  24 + end
  25 +
  26 + it "should create authorization object" do
  27 + Six.new(:book0 => BookRules.new, :book1 => BookRules.new).should be_kind_of(Six)
  28 + end
  29 +
  30 + describe "passing rules on initialization" do
  31 + it_should_behave_like :valid_abilities do
  32 + let(:abilities) { Six.new(:book_rules => BookRules.new) }
  33 + end
  34 + end
  35 + end
  36 +end
52 spec/six_spec.rb
@@ -2,57 +2,9 @@
2 2 require "./lib/six"
3 3
4 4 describe Six do
5   - let (:abilities) { Six.new }
6   -
7   - describe :allowed? do
  5 + it_should_behave_like :valid_abilities do
  6 + let (:abilities) { Six.new }
8 7 let (:rules) { BookRules.new }
9 8 before { abilities.add(:book_rules, rules) }
10   -
11   - before do
12   - @jim = Author.new("Jim")
13   - @mike = Author.new("Mike")
14   -
15   - @jims_book = Book.new("The Game", @jim)
16   - @mikes_book = Book.new("Life", @mike)
17   - end
18   -
19   - def allowed?(action, object, subject)
20   - # reset use
21   - abilities.reset_use
22   -
23   - # validate work of both global & local namespaces
24   - abilities.allowed?(action, object, subject) &&
25   - abilities.use(:book_rules).allowed?(action, object, subject)
26   - end
27   -
28   - describe "should return true or false depend on access" do
29   - context :read_book do
30   - it { allowed?(@jim, :read_book, @jims_book).should be_true }
31   - it { allowed?(@mike, :read_book, @mikes_book).should be_true }
32   - it { allowed?(@jim, :read_book, @mikes_book).should be_true }
33   - it { allowed?(@mike, :read_book, @jims_book).should be_true }
34   - end
35   -
36   - context :rate_book do
37   - it { allowed?(@jim, :rate_book, @jims_book).should be_false }
38   - it { allowed?(@mike, :rate_book, @mikes_book).should be_false }
39   - it { allowed?(@jim, :rate_book, @mikes_book).should be_true }
40   - it { allowed?(@mike, :rate_book, @jims_book).should be_true }
41   - end
42   -
43   - context :edit_book do
44   - it { allowed?(@jim, :edit_book, @jims_book).should be_true }
45   - it { allowed?(@mike,:edit_book, @mikes_book).should be_true }
46   - it { allowed?(@jim, :edit_book, @mikes_book).should be_false }
47   - it { allowed?(@mike,:edit_book, @jims_book).should be_false }
48   - end
49   -
50   - context :publish_book do
51   - it { allowed?(@jim, :publish_book, @jims_book).should be_false }
52   - it { allowed?(@mike,:publish_book, @mikes_book).should be_false }
53   - it { allowed?(@jim, :publish_book, @mikes_book).should be_false }
54   - it { allowed?(@mike,:publish_book, @jims_book).should be_false }
55   - end
56   - end
57 9 end
58 10 end
50 spec/support/valid_abilities_example.rb
... ... @@ -0,0 +1,50 @@
  1 +shared_examples :valid_abilities do
  2 + describe :allowed? do
  3 + before do
  4 + @jim = Author.new("Jim")
  5 + @mike = Author.new("Mike")
  6 +
  7 + @jims_book = Book.new("The Game", @jim)
  8 + @mikes_book = Book.new("Life", @mike)
  9 + end
  10 +
  11 + def allowed?(action, object, subject)
  12 + # reset use
  13 + abilities.reset_use
  14 +
  15 + # validate work of both global & local namespaces
  16 + abilities.allowed?(action, object, subject) &&
  17 + abilities.use(:book_rules).allowed?(action, object, subject)
  18 + end
  19 +
  20 + describe "should return true or false depend on access" do
  21 + context :read_book do
  22 + it { allowed?(@jim, :read_book, @jims_book).should be_true }
  23 + it { allowed?(@mike, :read_book, @mikes_book).should be_true }
  24 + it { allowed?(@jim, :read_book, @mikes_book).should be_true }
  25 + it { allowed?(@mike, :read_book, @jims_book).should be_true }
  26 + end
  27 +
  28 + context :rate_book do
  29 + it { allowed?(@jim, :rate_book, @jims_book).should be_false }
  30 + it { allowed?(@mike, :rate_book, @mikes_book).should be_false }
  31 + it { allowed?(@jim, :rate_book, @mikes_book).should be_true }
  32 + it { allowed?(@mike, :rate_book, @jims_book).should be_true }
  33 + end
  34 +
  35 + context :edit_book do
  36 + it { allowed?(@jim, :edit_book, @jims_book).should be_true }
  37 + it { allowed?(@mike,:edit_book, @mikes_book).should be_true }
  38 + it { allowed?(@jim, :edit_book, @mikes_book).should be_false }
  39 + it { allowed?(@mike,:edit_book, @jims_book).should be_false }
  40 + end
  41 +
  42 + context :publish_book do
  43 + it { allowed?(@jim, :publish_book, @jims_book).should be_false }
  44 + it { allowed?(@mike,:publish_book, @mikes_book).should be_false }
  45 + it { allowed?(@jim, :publish_book, @mikes_book).should be_false }
  46 + it { allowed?(@mike,:publish_book, @jims_book).should be_false }
  47 + end
  48 + end
  49 + end
  50 +end

0 comments on commit 49068ca

Please sign in to comment.
Something went wrong with that request. Please try again.