2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-26 07:13:57 -04:00
|
|
|
module Gitlab
|
|
|
|
# A class that can be wrapped around an expensive method call so it's only
|
|
|
|
# executed when actually needed.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# object = Gitlab::Lazy.new { some_expensive_work_here }
|
|
|
|
#
|
|
|
|
# object['foo']
|
|
|
|
# object.bar
|
|
|
|
class Lazy < BasicObject
|
|
|
|
def initialize(&block)
|
|
|
|
@block = block
|
|
|
|
end
|
|
|
|
|
2022-03-30 11:09:00 -04:00
|
|
|
def method_missing(...)
|
2016-05-26 07:13:57 -04:00
|
|
|
__evaluate__
|
|
|
|
|
2022-03-30 11:09:00 -04:00
|
|
|
@result.__send__(...) # rubocop:disable GitlabSecurity/PublicSend
|
2016-05-26 07:13:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def respond_to_missing?(name, include_private = false)
|
|
|
|
__evaluate__
|
|
|
|
|
|
|
|
@result.respond_to?(name, include_private) || super
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def __evaluate__
|
|
|
|
@result = @block.call unless defined?(@result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|