2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
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-06-21 17:19:12 +05:30
|
|
|
def method_missing(...)
|
2016-06-16 23:09:34 +05:30
|
|
|
__evaluate__
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
@result.__send__(...) # rubocop:disable GitlabSecurity/PublicSend
|
2016-06-16 23:09:34 +05:30
|
|
|
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
|