Module: CachedAttr

Defined in:
lib/cached_attr.rb

Overview

This module implements attributes whose reader method executes once, and the returned value is cached for later use. This might be handy for expensive operations.

An example, given the following method:

def object_to_get

if @object_to_get.nil?
  @object_to_get = [code_to_retrieve_object]
end
@object_to_get

end

…the cached attribute format would be:

cache_attr :object_to_get do

[code_to_retrieve_object]

end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



20
21
22
# File 'lib/cached_attr.rb', line 20

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#cached_attributes_storeObject

The store of cached properties



111
112
113
# File 'lib/cached_attr.rb', line 111

def cached_attributes_store
  @cached_attributes_store ||= {}
end