From 342c2e4eb81b2062148dc64932a366861d151d28 Mon Sep 17 00:00:00 2001 From: rick olson Date: Sun, 31 Mar 2019 13:28:25 -0600 Subject: [PATCH 1/3] raise ArgumentError if Faraday::Options.memoized is called w/o a block --- lib/faraday/options.rb | 3 +++ spec/faraday/options/options_spec.rb | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/faraday/options.rb b/lib/faraday/options.rb index bbb913d51..71bea614d 100644 --- a/lib/faraday/options.rb +++ b/lib/faraday/options.rb @@ -172,6 +172,9 @@ def self.attribute_options 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 diff --git a/spec/faraday/options/options_spec.rb b/spec/faraday/options/options_spec.rb index a9d73199b..9a11ad982 100644 --- a/spec/faraday/options/options_spec.rb +++ b/spec/faraday/options/options_spec.rb @@ -250,6 +250,20 @@ 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 } From 7aa80e21b95f42391725506990a882f882479a7b Mon Sep 17 00:00:00 2001 From: rick olson Date: Sun, 31 Mar 2019 13:39:21 -0600 Subject: [PATCH 2/3] stop relying on surprising Proc.new behavior this raises a warning in ruby 2.7.0 https://docs.ruby-lang.org/en/trunk/NEWS.html --- lib/faraday/options.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/faraday/options.rb b/lib/faraday/options.rb index 71bea614d..2a5d17289 100644 --- a/lib/faraday/options.rb +++ b/lib/faraday/options.rb @@ -171,11 +171,11 @@ def self.attribute_options @attribute_options ||= {} end - def self.memoized(key) + def self.memoized(key, &block) if !block_given? raise ArgumentError, "#memoized must be called with a block" end - memoized_attributes[key.to_sym] = Proc.new + memoized_attributes[key.to_sym] = block class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{key}() self[:#{key}]; end RUBY From 34bf23ae404cd38aa528db11dd174a1f8e8d717a Mon Sep 17 00:00:00 2001 From: rick olson Date: Sun, 31 Mar 2019 13:42:02 -0600 Subject: [PATCH 3/3] feed rubocop --- lib/faraday/options.rb | 5 +++-- spec/faraday/options/options_spec.rb | 4 +--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/faraday/options.rb b/lib/faraday/options.rb index 2a5d17289..8e8af499d 100644 --- a/lib/faraday/options.rb +++ b/lib/faraday/options.rb @@ -172,9 +172,10 @@ def self.attribute_options end def self.memoized(key, &block) - if !block_given? - raise ArgumentError, "#memoized must be called with a 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 diff --git a/spec/faraday/options/options_spec.rb b/spec/faraday/options/options_spec.rb index 9a11ad982..9758eccf5 100644 --- a/spec/faraday/options/options_spec.rb +++ b/spec/faraday/options/options_spec.rb @@ -253,9 +253,7 @@ class ParentOptions < Faraday::Options.new(:a, :b, :c) describe '#memoized' do subject(:options_class) { Class.new(ParentOptions) } it 'requires block' do - expect { - options_class.memoized(:a) - }.to raise_error(ArgumentError) + expect { options_class.memoized(:a) }.to raise_error(ArgumentError) end it 'accepts block' do