diff --git a/lib/faraday/options.rb b/lib/faraday/options.rb index bbb913d51..8e8af499d 100644 --- a/lib/faraday/options.rb +++ b/lib/faraday/options.rb @@ -171,8 +171,12 @@ def self.attribute_options @attribute_options ||= {} end - def self.memoized(key) - memoized_attributes[key.to_sym] = Proc.new + def self.memoized(key, &block) + unless block_given? + raise ArgumentError, '#memoized must be called with a block' + end + + memoized_attributes[key.to_sym] = block class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{key}() self[:#{key}]; end RUBY diff --git a/spec/faraday/options/options_spec.rb b/spec/faraday/options/options_spec.rb index a9d73199b..9758eccf5 100644 --- a/spec/faraday/options/options_spec.rb +++ b/spec/faraday/options/options_spec.rb @@ -250,6 +250,18 @@ class ParentOptions < Faraday::Options.new(:a, :b, :c) 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 }