Browse files

Creating message bus consumer

  • Loading branch information...
1 parent 8549bd1 commit b2fa6c9294380bef34a6640c993d4703b5f32280 @guiocavalcanti guiocavalcanti committed Sep 14, 2012
Showing with 37 additions and 41 deletions.
  1. +1 −1 .rspec
  2. +2 −1 lib/permit.rb
  3. +1 −1 lib/permit/policy.rb
  4. +2 −2 lib/permit/rule.rb
  5. +19 −17 spec/policy_spec.rb
  6. +12 −19 spec/rule_spec.rb
View
2 .rspec
@@ -1,2 +1,2 @@
--color
---format progress
+--format documentation
View
3 lib/permit.rb
@@ -6,7 +6,8 @@
module Permit
end
+require 'permit/connection'
+require 'permit/consumer'
require 'permit/rule'
require 'permit/policy'
require 'permit/api'
-require 'permit/connection'
View
2 lib/permit/policy.rb
@@ -7,7 +7,7 @@ def initialize(opts={})
end
def rules(args={})
- opts = { :db => @db, :logger => @logger }.merge @filter.merge(args)
+ opts = { :logger => @logger }.merge @filter.merge(args)
Rule.new(opts)
end
end
View
4 lib/permit/rule.rb
@@ -5,7 +5,7 @@ module Permit
class Rule
def initialize(opts={})
- @db = Connection.pool
+ @db = Connection.pool || opts.delete(:db)
@logger = opts.delete(:logger)
@filter = opts
end
@@ -30,7 +30,7 @@ def insert(opts={})
@logger.debug "Rule##{__method__} with #{document}" if @logger
- @db.collection.safe_insert(document)
+ @db.collection('rules').safe_insert(document)
end
end
end
View
36 spec/policy_spec.rb
@@ -2,55 +2,57 @@
module Permit
describe Policy do
- before(:all) do
- EventMachine.synchrony do
- Permit::Connection.establish_connections(1, "test")
- EM.stop
- end
- end
- let(:policy) { Policy.new(:resource_id => 'r') }
- let(:rules) do
- Permit::Connection.pool.collection("rules")
- end
-
context "finders" do
+ # let(:policy) { Policy.new(:resource_id => 'r') }
let(:fixtures) do
[{ "resource_id" => "r", "subject_id" => "s", "actions" => { "a" => true} },
{ "resource_id" => "t", "subject_id" => "s", "actions" => { "a" => true} }]
end
- around do
- EventMachine.synchrony do
- rules.remove({})
- EM.stop
- end
- end
it "should return a instance of rule" do
EventMachine.synchrony do
+ Connection.establish_connections(1, "test")
+ rules = Connection.pool.collection("rules")
+ policy = Policy.new(:resource_id => 'r')
policy.rules.should be_a Rule
EM.stop
end
end
it "should count the rules" do
EventMachine.synchrony do
+ Permit::Connection.establish_connections(1, "test")
+ rules = Permit::Connection.pool.collection("rules")
+ rules.remove({})
rules.safe_insert(fixtures)
+ policy = Policy.new(:resource_id => 'r')
policy.rules.count.should == 1
+ rules.remove({})
EM.stop
end
end
it "should return a collection" do
EventMachine.synchrony do
+ Permit::Connection.establish_connections(1, "test")
+ rules = Permit::Connection.pool.collection("rules")
+ rules.remove({})
rules.safe_insert(fixtures)
+ policy = Policy.new(:resource_id => 'r')
policy.rules.find.should respond_to :map
+ rules.remove({})
EM.stop
end
end
it "should retreive the rules theyselves" do
EventMachine.synchrony do
+ Permit::Connection.establish_connections(1, "test")
+ rules = Permit::Connection.pool.collection("rules")
+ rules.remove({})
rules.safe_insert(fixtures)
+ policy = Policy.new(:resource_id => 'r')
policy.rules.find.first["resource_id"].should == 'r'
+ rules.remove({})
EM.stop
end
end
View
31 spec/rule_spec.rb
@@ -2,44 +2,33 @@
module Permit
describe Rule do
- before(:all) do
- EventMachine.synchrony do
- Permit::Connection.establish_connections(1, "test")
- EM.stop
- end
- end
- around do
- EventMachine.synchrony do
- rules.remove({})
- EM.stop
- end
- end
- let(:db) do
- Permit::Connection.pool
- end
- let(:rules) do
- db.collection("rules")
- end
-
context "finders" do
let(:fixtures) do
[{ :resource_id => "r", :subject_id => "s", :actions => { :a => true} },
{ :resource_id => "t", :subject_id => "s", :actions => { :a => true} }]
end
it "should count data from db" do
EventMachine.synchrony do
+ Permit::Connection.establish_connections(1, "test")
+ rules = Permit::Connection.pool.collection('rules')
+ rules.remove({})
rules.safe_insert(fixtures)
rule = Rule.new(:resource_id => 'r')
rule.count.should == 1
+ rules.remove({})
EM.stop
end
end
it "should find data from db" do
EventMachine.synchrony do
+ Permit::Connection.establish_connections(1, "test")
+ rules = Permit::Connection.pool.collection('rules')
+ rules.remove({})
rules.safe_insert(fixtures)
rule = Rule.new(:resource_id => 'r')
rule.find.to_a == fixtures.first.to_a
+ rules.remove({})
EM.stop
end
end
@@ -48,9 +37,13 @@ module Permit
context "inserts" do
it "should insert rule" do
EventMachine.synchrony do
+ Permit::Connection.establish_connections(1, "test")
+ rules = Permit::Connection.pool.collection('rules')
rule = Rule.new(:resource_id => 'r', :subject_id => 's')
+ rules.remove({})
rule.insert(:action => :read)
rule.count(:actions => { :read => true }).should == 1
+ rules.remove({})
EM.stop
end
end

0 comments on commit b2fa6c9

Please sign in to comment.