module Universa::Lazy
Add class-level lazy declaration like:
class Test lazy(:foo) { puts "foo calculated" "bar" } t = Test.new t.foo # "foo calculated" -> "bar" t.foo #=> "bar"
Public Class Methods
included(other)
click to toggle source
# File lib/universa/lazy.rb, line 56 def Lazy.included(other) # @!method lazy(name, &block) # Provides create lazy instance variable calculated by a provided block # @param [String] name of the created field # @return [string] Returns the greeting. # @yield calculates the value for the variable def other.lazy(name, &block) define_method(name.to_sym) { x = @@lazy_creation_mutex.synchronize { cache_name = :"@__#{name}__cache" if !(x = instance_variable_get(cache_name)) x = LazyValue.new { instance_exec &block } instance_variable_set(cache_name, x) end x } x.get } end end