Sha256: edd12800fa2c235b07308de76a076799602827a2e06d9f0f59767b926f682a8a

Contents?: true

Size: 592 Bytes

Versions: 1

Compression:

Stored size: 592 Bytes

Contents

module LazyConst

  CACHE = {}

  def lazy_const(name, &block)
    raise Error("lazy_const requires a block") unless block_given?
    LazyConst::CACHE.delete "#{self.name}.#{name}"
    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.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.1 lib/lazy_const.rb