[ruby/irb] Memoize helper method instances with Singleton module
Some helpers, like Rails console's `app`, requires memoization of the helper's ivars. To support it IRB needs to memoize helper method instances as well. https://github.com/ruby/irb/commit/169a9a2c30
This commit is contained in:
parent
afc7799c32
commit
e5ca3d072f
@ -1,6 +1,10 @@
|
|||||||
|
require "singleton"
|
||||||
|
|
||||||
module IRB
|
module IRB
|
||||||
module HelperMethod
|
module HelperMethod
|
||||||
class Base
|
class Base
|
||||||
|
include Singleton
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def description(description = nil)
|
def description(description = nil)
|
||||||
@description = description if description
|
@description = description if description
|
||||||
|
@ -179,7 +179,7 @@ EOF
|
|||||||
def self.install_helper_methods
|
def self.install_helper_methods
|
||||||
HelperMethod.helper_methods.each do |name, helper_method_class|
|
HelperMethod.helper_methods.each do |name, helper_method_class|
|
||||||
define_method name do |*args, **opts, &block|
|
define_method name do |*args, **opts, &block|
|
||||||
helper_method_class.new.execute(*args, **opts, &block)
|
helper_method_class.instance.execute(*args, **opts, &block)
|
||||||
end unless method_defined?(name)
|
end unless method_defined?(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -97,13 +97,38 @@ module TestIRB
|
|||||||
RUBY
|
RUBY
|
||||||
|
|
||||||
output = run_ruby_file do
|
output = run_ruby_file do
|
||||||
type <<~INPUT
|
type "my_helper"
|
||||||
my_helper
|
|
||||||
INPUT
|
|
||||||
type "exit"
|
type "exit"
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_include(output, 'Hello from MyHelper')
|
assert_include(output, 'Hello from MyHelper')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_helper_method_instances_are_memoized
|
||||||
|
write_ruby <<~RUBY
|
||||||
|
require "irb/helper_method"
|
||||||
|
|
||||||
|
class MyHelper < IRB::HelperMethod::Base
|
||||||
|
description "This is a test helper"
|
||||||
|
|
||||||
|
def execute(val)
|
||||||
|
@val ||= val
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
IRB::HelperMethod.register(:my_helper, MyHelper)
|
||||||
|
|
||||||
|
binding.irb
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
output = run_ruby_file do
|
||||||
|
type "my_helper(100)"
|
||||||
|
type "my_helper(200)"
|
||||||
|
type "exit"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_include(output, '=> 100')
|
||||||
|
assert_not_include(output, '=> 200')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user