Permalink
Browse files

initalization with args, up to 0.1.3

  • Loading branch information...
1 parent 50cfc57 commit 49068ca91596d95fa8112494d22e7909bffbea40 dzaporozhets committed Sep 1, 2011
Showing with 122 additions and 53 deletions.
  1. +6 −0 CHANGELOG
  2. +15 −0 README.markdown
  3. +11 −1 lib/six.rb
  4. +2 −2 six.gemspec
  5. +36 −0 spec/six_initialize_spec.rb
  6. +2 −50 spec/six_spec.rb
  7. +50 −0 spec/support/valid_abilities_example.rb
View
@@ -1,3 +1,9 @@
+v 0.1.3
+ - initialization with pack as args
+ Ex. Six.new(:book => BookRules.new)
+ - initialization with multiple packs
+ Ex. Six.new(:book => BookRules.new, :author => AuthorRules.new)
+
v 0.1.2
- allowed?(action, object, subject) -> allowed?(object, action, subject)
- use & use! are now aliases for use_pack & use_pack!
View
@@ -151,6 +151,21 @@ end
link_to 'Edit', edit_book_path(book) if can?(@author, :edit_book, book)
```
+### :initialization
+
+```ruby
+# simple
+abilities = Six.new
+
+# with rules
+abilities = Six.new(:book_rules => BookRules) # same as Six.new & add(:bok_rules, BookRules)
+
+# with more
+abilities = Six.new(:book => BookRules,
+ :auth => AuthRules,
+ :managment => ManagerRules)
+```
+
### :use
```ruby
View
@@ -11,12 +11,22 @@ def message
end
end
+ class InitializeArgumentError < StandardError
+ def message
+ "Six.new require hash as pack argument in format {:name_of_pack => PackRules.new}"
+ end
+ end
+
attr_reader :rules_packs
attr_reader :current_rule_pack
- def initialize()
+ def initialize(packs={})
+ raise InitializeArgumentError.new unless packs.kind_of?(Hash)
+
@rules_packs = {}
@current_rule_pack = nil
+
+ packs.each { |key, pack| add_pack!(key, pack) }
end
# Set current pack from stored packs by key
View
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = 'six'
- s.version = '0.1.2'
- s.date = '2011-08-27'
+ s.version = '0.1.3'
+ s.date = '2011-09-01'
s.summary = "six"
s.description = "Very simple authorization gem"
s.authors = ["Dmitriy Zaporozhets"]
@@ -0,0 +1,36 @@
+require "./spec/spec_helper"
+require "./lib/six"
+
+describe Six, "initialize" do
+ describe "initalization" do
+ before do
+ @jim = Author.new("Jim")
+ @mike = Author.new("Mike")
+
+ @jims_book = Book.new("The Game", @jim)
+ @mikes_book = Book.new("Life", @mike)
+ end
+
+ it "should create authorization object" do
+ Six.new.should be_kind_of(Six)
+ end
+
+ it "should raise error if invalid argument passed" do
+ lambda { Six.new("wrong argument") }.should raise_error Six::InitializeArgumentError
+ end
+
+ it "should create authorization object" do
+ Six.new(:book_rules => BookRules.new).should be_kind_of(Six)
+ end
+
+ it "should create authorization object" do
+ Six.new(:book0 => BookRules.new, :book1 => BookRules.new).should be_kind_of(Six)
+ end
+
+ describe "passing rules on initialization" do
+ it_should_behave_like :valid_abilities do
+ let(:abilities) { Six.new(:book_rules => BookRules.new) }
+ end
+ end
+ end
+end
View
@@ -2,57 +2,9 @@
require "./lib/six"
describe Six do
- let (:abilities) { Six.new }
-
- describe :allowed? do
+ it_should_behave_like :valid_abilities do
+ let (:abilities) { Six.new }
let (:rules) { BookRules.new }
before { abilities.add(:book_rules, rules) }
-
- before do
- @jim = Author.new("Jim")
- @mike = Author.new("Mike")
-
- @jims_book = Book.new("The Game", @jim)
- @mikes_book = Book.new("Life", @mike)
- end
-
- def allowed?(action, object, subject)
- # reset use
- abilities.reset_use
-
- # validate work of both global & local namespaces
- abilities.allowed?(action, object, subject) &&
- abilities.use(:book_rules).allowed?(action, object, subject)
- end
-
- describe "should return true or false depend on access" do
- context :read_book do
- it { allowed?(@jim, :read_book, @jims_book).should be_true }
- it { allowed?(@mike, :read_book, @mikes_book).should be_true }
- it { allowed?(@jim, :read_book, @mikes_book).should be_true }
- it { allowed?(@mike, :read_book, @jims_book).should be_true }
- end
-
- context :rate_book do
- it { allowed?(@jim, :rate_book, @jims_book).should be_false }
- it { allowed?(@mike, :rate_book, @mikes_book).should be_false }
- it { allowed?(@jim, :rate_book, @mikes_book).should be_true }
- it { allowed?(@mike, :rate_book, @jims_book).should be_true }
- end
-
- context :edit_book do
- it { allowed?(@jim, :edit_book, @jims_book).should be_true }
- it { allowed?(@mike,:edit_book, @mikes_book).should be_true }
- it { allowed?(@jim, :edit_book, @mikes_book).should be_false }
- it { allowed?(@mike,:edit_book, @jims_book).should be_false }
- end
-
- context :publish_book do
- it { allowed?(@jim, :publish_book, @jims_book).should be_false }
- it { allowed?(@mike,:publish_book, @mikes_book).should be_false }
- it { allowed?(@jim, :publish_book, @mikes_book).should be_false }
- it { allowed?(@mike,:publish_book, @jims_book).should be_false }
- end
- end
end
end
@@ -0,0 +1,50 @@
+shared_examples :valid_abilities do
+ describe :allowed? do
+ before do
+ @jim = Author.new("Jim")
+ @mike = Author.new("Mike")
+
+ @jims_book = Book.new("The Game", @jim)
+ @mikes_book = Book.new("Life", @mike)
+ end
+
+ def allowed?(action, object, subject)
+ # reset use
+ abilities.reset_use
+
+ # validate work of both global & local namespaces
+ abilities.allowed?(action, object, subject) &&
+ abilities.use(:book_rules).allowed?(action, object, subject)
+ end
+
+ describe "should return true or false depend on access" do
+ context :read_book do
+ it { allowed?(@jim, :read_book, @jims_book).should be_true }
+ it { allowed?(@mike, :read_book, @mikes_book).should be_true }
+ it { allowed?(@jim, :read_book, @mikes_book).should be_true }
+ it { allowed?(@mike, :read_book, @jims_book).should be_true }
+ end
+
+ context :rate_book do
+ it { allowed?(@jim, :rate_book, @jims_book).should be_false }
+ it { allowed?(@mike, :rate_book, @mikes_book).should be_false }
+ it { allowed?(@jim, :rate_book, @mikes_book).should be_true }
+ it { allowed?(@mike, :rate_book, @jims_book).should be_true }
+ end
+
+ context :edit_book do
+ it { allowed?(@jim, :edit_book, @jims_book).should be_true }
+ it { allowed?(@mike,:edit_book, @mikes_book).should be_true }
+ it { allowed?(@jim, :edit_book, @mikes_book).should be_false }
+ it { allowed?(@mike,:edit_book, @jims_book).should be_false }
+ end
+
+ context :publish_book do
+ it { allowed?(@jim, :publish_book, @jims_book).should be_false }
+ it { allowed?(@mike,:publish_book, @mikes_book).should be_false }
+ it { allowed?(@jim, :publish_book, @mikes_book).should be_false }
+ it { allowed?(@mike,:publish_book, @jims_book).should be_false }
+ end
+ end
+ end
+end

0 comments on commit 49068ca

Please sign in to comment.