Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Memoized w/o block #962

Merged
merged 3 commits into from Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/faraday/options.rb
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/faraday/options/options_spec.rb
Expand Up @@ -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 }

Expand Down