Skip to content

Instantly share code, notes, and snippets.

module NameIdCache
def self.included(base)
m = Module.new
base.all.each do |record|
cache_key = NameIdCache.cache_key(base, record)
Rails.cache.write(cache_key, record.id)
m.__send__(:define_method, record.name) do
Rails.cache.fetch(cache_key) do
module NameIdCache
def self.included(base)
m = Module.new
base.all.each do |record|
m.__send__(:define_method, record.name) do
record.record_id # using #record_id because OpenStruct is iffy about #id
end
end
base.extend(m)
end
@marcbowes
marcbowes / gist:1899731
Created February 24, 2012 09:31
Envelope encryption in Ruby using OpenSSL
# Explanation at http://stackoverflow.com/questions/5212213/ruby-equivalent-of-php-openssl-seal/9428217#9428217
# Implementation
class EnvelopeEncryption
require "openssl"
# This method takes in plaintext and produces ciphertext and an
$ cat Gemfile
source 'http://rubygems.org'
gem "rails", ">=3.0.5"
# To use debugger
# gem 'ruby-debug'
gem "guid"
gem "mysql2"
diff -ur marcbowes-UsingYAML-590c028/lib/using_yaml/array.rb UsingYAML-590c028/lib/using_yaml/array.rb
--- marcbowes-UsingYAML-590c028/lib/using_yaml/array.rb 2010-03-06 22:14:19.000000000 +0200
+++ UsingYAML-590c028/lib/using_yaml/array.rb 2010-03-29 18:05:01.000000000 +0200
@@ -25,5 +25,6 @@
# Load in the extensions for this instance
array.extend(extensions)
+ array
end
end
# This is a cheap stab at demonstrating the behavior of
# Inotify::CREATE. That is, CREATE will trigger inotify events
# immediately upon file creation.
require 'inotify'
require 'fileutils'
# Method which fakes a slow write by sleeping for one second.
def slow_write(filename)
File.open(filename, "w") do |f|