debian-mirror-gitlab/lib/gitlab/lazy.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

37 lines
748 B
Ruby
Raw Normal View History

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