Sha256: e7e7313b3a37db8ab94398a6abe00bc43c0d1a9aef3b0d0100a916694229b9ca

Contents?: true

Size: 599 Bytes

Versions: 1

Compression:

Stored size: 599 Bytes

Contents

module LazyConst

  CACHE = {}

  def lazy_const(name, &block)
    raise Error("lazy_const requires a block") unless block_given?
    LazyConst::CACHE["#{self.name}.#{name}"] = nil
    define_singleton_method name do
      LazyConst::CACHE["#{self.name}.#{name}"] ||= block.call
    end
  end

  def self.preload
    self.clear
    LazyConst::CACHE.keys.each do |class_dot_name|
      klass, name = class_dot_name.split('.')
      m = klass.constantize.public_send name.to_sym
    end
  end

  def self.clear
    LazyConst::CACHE.keys.each do |k|
      LazyConst::CACHE[k] = nil
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lazy_const-0.1.2 lib/lazy_const.rb