Skip to content

Commit

Permalink
Merge pull request #962 from lostisland/memoized-wo-block
Browse files Browse the repository at this point in the history
Fix Memoized w/o block
  • Loading branch information
technoweenie committed Mar 31, 2019
2 parents ef8d9eb + 34bf23a commit cda3a01
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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

0 comments on commit cda3a01

Please sign in to comment.