Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

class ShopifyCli::LazyDelegator

Kevin O'Sullivan edited this page Jun 28, 2021 · 2 revisions

LazyDelegator defers initialization of its underlying delegatee until the latter is accessed for the first time due to a method call that the delegator cannot handle itself:

result = LazyDelegator.new do
  # carry out costly operation ...
end

result      # referencing the object itself does not result in Proc evaluation
result.to_h # however, calling a method on it will result in Proc evaluation

LazyDelegator lends itself to being subclassed in scenarios where some facts are known and others are costly to compute:

class LazySpecificationHandler < ShopifyCli::LazyDelegator
  attr_reader :identifier

  def initialize(identifier, &initializer)
    super(&initializer)
    @identifier = identifier
  end
end

handler = LazySpecificationHandler.new(:product_subscription) do
   # fetch specification from the Partners Dashboard API ...
end

# Accessing identifier will not result in Proc evaluation as it is
# available as an attribute of the delegator itself
handler.identifier # => :product_subscription

# Accessing the specification will result in evaluation of the Proc
# and in our example in a subsequent network call
handler.specification # => <Extension::Models::Specifcation:...>

Class Methods

new

new(&delegatee_initializer)

see source

# File lib/shopify-cli/lazy_delegator.rb, line 41
def initialize(&amp;delegatee_initializer)
  super([false, delegatee_initializer])
end

Instance Methods

getobj

__getobj__(*)

see source

# File lib/shopify-cli/lazy_delegator.rb, line 47
def __getobj__(*)
  initialized, value_or_initializer = super
  return value_or_initializer if initialized
  value_or_initializer.call.tap do |value|
    __setobj__([true, value])
  end
end

Clone this wiki locally