Class: ShopifyCLI::LazyDelegator
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- ShopifyCLI::LazyDelegator
- Defined in:
- lib/shopify_cli/lazy_delegator.rb
Overview
‘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:...>
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(&delegatee_initializer) ⇒ LazyDelegator
constructor
A new instance of LazyDelegator.
Constructor Details
#initialize(&delegatee_initializer) ⇒ LazyDelegator
Returns a new instance of LazyDelegator.
41 42 43 |
# File 'lib/shopify_cli/lazy_delegator.rb', line 41 def initialize(&delegatee_initializer) super([false, delegatee_initializer]) end |