module Nuggets::Object::LazyAttrMixin

Private Instance Methods

lazy_attr(name, freeze = true, &block) click to toggle source
# File lib/nuggets/object/lazy_attr_mixin.rb, line 33
def lazy_attr(name, freeze = true, &block)
  class << self; self; end.send(:attr_reader, name)

  value = instance_variable_defined?(ivar = "@#{name}") ?
    instance_variable_get(ivar) :
    instance_variable_set(ivar, instance_eval(&block))

  freeze ? value.freeze : value
end