PersistentBlocks

Persist the output of ruby blocks

Installation

Add this line to your application's Gemfile:

gem 'persistent_blocks'

And then execute:

$ bundle

Or install it yourself as:

$ gem install persistent_blocks

Usage

put the following in a rakefile.rb

require 'persistent_blocks'
extend PersistentBlocks

persist :some_persistent_data do
    # Do a long running process to generate some data called :some_persistent_data
    # You don't want to repeat the calculation unecessarily, so persist will
    # automatically persist it to disk for you.
    sleep(1)
    puts "I will return an array that will be automatically persisted to disk"
    puts "Next time you run the rakefile I won't be run."
    ["First persisted element", "Second persisted element"]
end

persist do |some_persistent_data|
    puts "Now I will print out the persistent data"
    p some_persistent_data # => ["First persisted element", "Second persisted element"]
end

Then run the rake file using rake from the command line. The

first time you run it the block generating some_persistent_data will

be run, but the second time you run it :some_persistent_data will

just be retrieved from disk. The name "some_persistent_data"

between the pipes in the second block is important because it tells

persist which data to lookup from the marshal files and pass to the

block of code.