class SigtermExtensions::Lazy

A class that can be wrapped around an expensive method call so it's only executed when actually needed.

Usage:

object = SigtermExtensions::Lazy.new { some_expensive_work_here }

object['foo']
object.bar

Public Class Methods

new(&block) click to toggle source
# File lib/sigterm_extensions/lazy.rb, line 12
def initialize(&block)
  @block = block
end

Public Instance Methods

method_missing(name, *args, &block) click to toggle source
# File lib/sigterm_extensions/lazy.rb, line 16
def method_missing(name, *args, &block)
  __evaluate__

  @result.__send__(name, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
end
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/sigterm_extensions/lazy.rb, line 22
def respond_to_missing?(name, include_private = false)
  __evaluate__

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

Private Instance Methods

__evaluate__() click to toggle source
# File lib/sigterm_extensions/lazy.rb, line 30
def __evaluate__
  @result = @block.call unless defined?(@result)
end