Skip to content

Commit 5efe808

Browse files
committed
Revert "Remove unnecessary VariableArgsBlockCaller."
This reverts commit e27202d. Unfortunately, 1.8 and 1.9 blocks have different behavior: block = Proc.new { |x| puts x.inspect } block.call(1, 2) On 1.8, x gets [1, 2]; on 1.9, x gets 1. So this VariableArgsBlockCaller is more important than I thought to maintain 1.8 compatibility.
1 parent e27202d commit 5efe808

File tree

10 files changed

+48
-29
lines changed

10 files changed

+48
-29
lines changed

lib/vcr.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'vcr/util/variable_args_block_caller'
12
require 'vcr/util/yaml'
23

34
require 'vcr/cassette'
@@ -11,6 +12,7 @@
1112
require 'vcr/structs/http_interaction'
1213

1314
module VCR
15+
include VariableArgsBlockCaller
1416
extend self
1517

1618
autoload :BasicObject, 'vcr/util/basic_object'
@@ -56,11 +58,11 @@ def eject_cassette
5658
cassette
5759
end
5860

59-
def use_cassette(*args)
61+
def use_cassette(*args, &block)
6062
cassette = insert_cassette(*args)
6163

6264
begin
63-
yield cassette
65+
call_block(block, cassette)
6466
ensure
6567
eject_cassette
6668
end

lib/vcr/configuration.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module VCR
55
class Configuration
66
include VCR::Hooks
7+
include VCR::VariableArgsBlockCaller
78

89
define_hook :before_record
910
define_hook :before_playback
@@ -65,13 +66,13 @@ def allow_http_connections_when_no_cassette?
6566
!!@allow_http_connections_when_no_cassette
6667
end
6768

68-
def filter_sensitive_data(placeholder, tag = nil)
69+
def filter_sensitive_data(placeholder, tag = nil, &block)
6970
before_record(tag) do |interaction|
70-
interaction.filter!((yield interaction), placeholder)
71+
interaction.filter!(call_block(block, interaction), placeholder)
7172
end
7273

7374
before_playback(tag) do |interaction|
74-
interaction.filter!(placeholder, (yield interaction))
75+
interaction.filter!(placeholder, call_block(block, interaction))
7576
end
7677
end
7778

lib/vcr/middleware/common.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
module VCR
22
module Middleware
33
module Common
4+
include VCR::VariableArgsBlockCaller
5+
46
def initialize(app, &block)
57
raise ArgumentError.new("You must provide a block to set the cassette options") unless block
68
@app, @cassette_arguments_block = app, block
79
end
810

9-
private
11+
private
1012

11-
def cassette_arguments(env)
12-
arguments = CassetteArguments.new
13-
@cassette_arguments_block.call(arguments, env)
14-
[arguments.name, arguments.options]
15-
end
13+
def cassette_arguments(env)
14+
arguments = CassetteArguments.new
15+
call_block(@cassette_arguments_block, arguments, env)
16+
[arguments.name, arguments.options]
17+
end
1618
end
1719
end
1820
end

lib/vcr/util/hooks.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
module VCR
22
module Hooks
3+
include VariableArgsBlockCaller
4+
35
def self.included(klass)
46
klass.extend(ClassMethods)
57
end
68

79
def invoke_hook(hook, tag, *args)
810
hooks_for(hook, tag).each do |callback|
9-
callback.call(*args)
11+
call_block(callback, *args)
1012
end
1113
end
1214

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module VCR
2+
module VariableArgsBlockCaller
3+
def call_block(block, *args)
4+
if block.arity >= 0
5+
args = args.first([args.size, block.arity].min)
6+
end
7+
8+
block.call(*args)
9+
end
10+
end
11+
end
12+

spec/vcr/configuration_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,13 @@ def stub_no_http_stubbing_adapter
150150
before(:each) { interaction.stub(:filter!) }
151151

152152
it 'adds a before_record hook that replaces the string returned by the block with the given string' do
153-
subject.filter_sensitive_data('foo') { 'bar' }
153+
subject.filter_sensitive_data('foo', &lambda { 'bar' })
154154
interaction.should_receive(:filter!).with('bar', 'foo')
155155
subject.invoke_hook(:before_record, nil, interaction)
156156
end
157157

158158
it 'adds a before_playback hook that replaces the given string with the string returned by the block' do
159-
subject.filter_sensitive_data('foo') { 'bar' }
159+
subject.filter_sensitive_data('foo', &lambda { 'bar' })
160160
interaction.should_receive(:filter!).with('foo', 'bar')
161161
subject.invoke_hook(:before_playback, nil, interaction)
162162
end
@@ -173,14 +173,14 @@ def stub_no_http_stubbing_adapter
173173

174174
it 'yields the interaction to the block for the before_record hook' do
175175
yielded_interaction = nil
176-
subject.filter_sensitive_data('foo') { |i| yielded_interaction = i; 'bar' }
176+
subject.filter_sensitive_data('foo', &lambda { |i| yielded_interaction = i; 'bar' })
177177
subject.invoke_hook(:before_record, nil, interaction)
178178
yielded_interaction.should equal(interaction)
179179
end
180180

181181
it 'yields the interaction to the block for the before_playback hook' do
182182
yielded_interaction = nil
183-
subject.filter_sensitive_data('foo') { |i| yielded_interaction = i; 'bar' }
183+
subject.filter_sensitive_data('foo', &lambda { |i| yielded_interaction = i; 'bar' })
184184
subject.invoke_hook(:before_playback, nil, interaction)
185185
yielded_interaction.should equal(interaction)
186186
end

spec/vcr/middleware/faraday_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@
3232

3333
it 'sets the cassette options based on the provided block' do
3434
app = lambda { |env| VCR.current_cassette.erb.should eq({ :foo => :bar }) }
35-
instance = described_class.new(app) do |c|
35+
instance = described_class.new(app, &lambda do |c|
3636
c.name 'c'
3737
c.options :erb => { :foo => :bar }
38-
end
38+
end)
3939

4040
instance.call(env_hash)
4141
end
4242

4343
it 'yields the env to the provided block when the block accepts 2 arguments' do
44-
instance = described_class.new(lambda { |env| }) do |c, env|
44+
instance = described_class.new(lambda { |env| }, &lambda do |c, env|
4545
env.should eq(env_hash)
4646
c.name 'c'
47-
end
47+
end)
4848

4949
instance.call(env_hash)
5050
end

spec/vcr/middleware/rack_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@
3434

3535
it 'sets the cassette options based on the provided block' do
3636
rack_app = lambda { |env| VCR.current_cassette.erb.should eq({ :foo => :bar }) }
37-
instance = described_class.new(rack_app) do |c|
37+
instance = described_class.new(rack_app, &lambda do |c|
3838
c.name 'c'
3939
c.options :erb => { :foo => :bar }
40-
end
40+
end)
4141

4242
instance.call({})
4343
end
4444

4545
it 'yields the rack env to the provided block when the block accepts 2 arguments' do
46-
instance = described_class.new(lambda { |env| }) do |c, env|
46+
instance = described_class.new(lambda { |env| }, &lambda do |c, env|
4747
env.should eq(env_hash)
4848
c.name 'c'
49-
end
49+
end)
5050

5151
instance.call(env_hash)
5252
end

spec/vcr/util/hooks_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@
7777
end
7878

7979
it 'only passes along 1 argument when the block accepts only 1 arguments' do
80-
subject.before_foo { |a| invocations << a }
80+
subject.before_foo(&lambda { |a| invocations << a })
8181
subject.invoke_hook(:before_foo, nil, :arg1, :arg2)
8282
invocations.flatten.should eq([:arg1])
8383
end
8484

8585
it 'passes along all arguments when the block accepts a variable number of args' do
86-
subject.before_foo { |*a| invocations << a }
86+
subject.before_foo(&lambda { |*a| invocations << a })
8787
subject.invoke_hook(:before_foo, nil, :arg1, :arg2)
8888
invocations.flatten.should eq([:arg1, :arg2])
8989
end

spec/vcr_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ def insert_cassette(name = :cassette_test)
5252

5353
it 'yields' do
5454
yielded = false
55-
VCR.use_cassette(:cassette_test) { yielded = true }
55+
VCR.use_cassette(:cassette_test, &lambda { yielded = true })
5656
yielded.should be_true
5757
end
5858

5959
it 'yields the cassette instance if the block expects an argument' do
60-
VCR.use_cassette('name', :record => :new_episodes) do |cassette|
60+
VCR.use_cassette('name', :record => :new_episodes, &lambda do |cassette|
6161
cassette.should equal(VCR.current_cassette)
62-
end
62+
end)
6363
end
6464

6565
it 'yields the cassette instance if the block expects a variable number of args' do

0 commit comments

Comments
 (0)