Skip to content

Commit

Permalink
add ability to temporary enable yaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Võ Anh Duy committed Apr 12, 2014
1 parent 71788a3 commit f724ba2
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 19 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -9,5 +9,6 @@ gem 'guard'
gem 'guard-bundler'
gem 'guard-rspec'
gem 'simplecov', '~> 0.7.1'
gem 'database_cleaner'

gem "sqlite3"
7 changes: 7 additions & 0 deletions lib/yaps.rb
Expand Up @@ -28,4 +28,11 @@ def self.reset
def self.configure
yield(configuration)
end

def self.with_pushlisher_enable &block
previous = configuration.enable
configuration.enable = true
block.call
configuration.enable = previous
end
end
4 changes: 1 addition & 3 deletions lib/yaps/publisher.rb
@@ -1,9 +1,7 @@
module Yaps
module ExtendPublisher
def publish(*args)
if Yaps.configuration.enable?
super
end
Yaps.configuration.enable? && super
end
end

Expand Down
52 changes: 38 additions & 14 deletions spec/publisher_spec.rb
@@ -1,24 +1,48 @@
require 'spec_helper'

describe Yaps::Publisher do
describe 'messages' do
it "it should fired a message after create" do
user = User.new(:name => 'John')
expect(user).to receive(:publish).once.with(:user_created, user)
user.save
describe 'active record' do
describe 'when a model was created ' do
it "it should call publish" do
user = User.new(:name => 'John')
expect(user).to receive(:publish).once.with(:user_created, user)
user.save
end
end

it "it should fired a message after update" do
user = User.create(:name => 'John')
user.name = 'Jack'
expect(user).to receive(:publish).once.with(:user_updated, user)
user.save
describe 'when a model was updated ' do
it "it should call publish" do
user = User.create(:name => 'John')
user.name = 'Jack'
expect(user).to receive(:publish).once.with(:user_updated, user)
user.save
end
end

it "it should fired a message after update" do
user = User.create(:name => 'John')
expect(user).to receive(:publish).once.with(:user_deleted, user)
user.destroy
describe 'when a model was deleted ' do
it "it should call publish" do
user = User.create(:name => 'John')
expect(user).to receive(:publish).once.with(:user_deleted, user)
user.destroy
end
end
end

describe '.publish' do
context 'Yaps is disable' do
it "should not publish message" do
Yaps.configuration.enable = false
user = User.create(:name => 'John')
expect(user.send(:publish, :user_created, user)).to eq(false)
end
end

context 'Yaps is enable' do
it "should publish message" do
Yaps.configuration.enable = true
user = User.create(:name => 'John')
expect(user.send(:publish, :user_created, user)).not_to eq(false)
end
end
end
end
20 changes: 18 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -15,18 +15,20 @@
end

require 'active_record'
require 'yaps'
require 'database_cleaner'

ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Schema.define do
self.verbose = false

create_table :users, :force => true do |t|
t.string :name
end
end

require 'yaps'
require 'supports/user'


RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
Expand All @@ -37,4 +39,18 @@
# the seed, which is printed after each run.
# --seed 1234
config.order = 'random'

config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
DatabaseCleaner.start
end

config.after(:each) do
DatabaseCleaner.clean
Yaps.reset
end
end
27 changes: 27 additions & 0 deletions spec/yaps_spec.rb
@@ -0,0 +1,27 @@
require 'spec_helper'

describe Yaps do
describe '#with_pushlisher_enable' do
it 'should enable Yaps for block in its scope' do
Yaps.configuration.enable = false
Yaps.with_pushlisher_enable do
expect(Yaps.configuration.enable).to eq(true)
end
end

it 'should not change configuration state outside of its scope' do
Yaps.configuration.enable = false
Yaps.with_pushlisher_enable do
end
expect(Yaps.configuration.enable).to eq(false)
end

it ' should execute the block pass to it' do
a = 5
Yaps.with_pushlisher_enable do
a = 10
end
expect(a).to eq(10)
end
end
end

0 comments on commit f724ba2

Please sign in to comment.