Class: Gitlab::Lazy

Inherits:
BasicObject
Defined in:
lib/gitlab/lazy.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Lazy

Returns a new instance of Lazy.



14
15
16
# File 'lib/gitlab/lazy.rb', line 14

def initialize(&block)
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject



18
19
20
21
22
# File 'lib/gitlab/lazy.rb', line 18

def method_missing(...)
  __evaluate__

  @result.__send__(...) # rubocop:disable GitlabSecurity/PublicSend
end

Instance Method Details

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/gitlab/lazy.rb', line 24

def respond_to_missing?(name, include_private = false)
  __evaluate__

  @result.respond_to?(name, include_private) || super
end