raise ArgumentError if Faraday::Options.memoized is called w/o a block

This commit is contained in:
rick olson 2019-03-31 13:28:25 -06:00
parent 5063ffe963
commit 342c2e4eb8
2 changed files with 17 additions and 0 deletions

View File

@ -172,6 +172,9 @@ module Faraday
end
def self.memoized(key)
if !block_given?
raise ArgumentError, "#memoized must be called with a block"
end
memoized_attributes[key.to_sym] = Proc.new
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{key}() self[:#{key}]; end

View File

@ -250,6 +250,20 @@ RSpec.describe Faraday::Options do
end
end
describe '#memoized' do
subject(:options_class) { Class.new(ParentOptions) }
it 'requires block' do
expect {
options_class.memoized(:a)
}.to raise_error(ArgumentError)
end
it 'accepts block' do
options_class.memoized(:a) { :foo }
expect(options_class.new.a).to eql(:foo)
end
end
describe '#fetch' do
subject { SubOptions.new }