Project

pakiderm

0.0
No commit activity in last 3 years
No release in over 3 years
Pakiderm is a simple module that encapsulates a modern memoization technique.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

Pakiderm Gem Version Build Status Test Coverage License

Allows you to unobtrusively memoize methods without parameters.

class Stopwatch
  extend Pakiderm

  def start
    @started_at = Time.now
  end

  def stop
    elapsed_time
  end

  memoize \
  def elapsed_time
    Time.now - @started_at
  end
end

Memoized methods will only be called the first time; the result will be stored and returned on subsequent invocations.

stopwatch = Stopwatch.new
stopwatch.start
stopwatch.stop         # Example: 3.991826
stopwatch.elapsed_time #       => 3.991826

You can also pass a list of methods that should be memoized:

memoize :complex_calculation, :api_response, :db_query

Background

Pakiderm adds a method by using Module#prepend in order to"intercept" calls to the original method and provide memoization.

Caveat

If you override the method in a subclass, Pakiderm's method won't be able to intercept calls to the subclass' method, so you would need to call memoize again in the subclass.