Skip to content

Instantly share code, notes, and snippets.

View kennethkalmer's full-sized avatar

Kenneth Kalmer kennethkalmer

View GitHub Profile
@coffeeaddict
coffeeaddict / thing.rb
Created May 31, 2011 07:28
Scope for each state in the pluginaweek/state_machine
class Thing < ActiveRecord::Base
state_machine :initial => :pending do
# when published, the thing has to be removed
after_transition :on => :done_publishing, :do => :destroy
event :publish do
transition :pending => :publishing, :unless => :not_ready?
end
event :done_publishing do
@rweald
rweald / jammit.rake
Created June 1, 2011 19:58
Using Barista with Jammit to package and compile CoffeeScript
# simply run rake jammit:package to compile your coffeescript and then package up all your assets
# including the newly compiled javascripts
namespace :jammit do
task :package do
Rake::Task["barista:brew"].invoke
Jammit.package!
end
end
@RiANOl
RiANOl / gist:1077760
Last active April 13, 2024 06:17
AES128 / AES256 CBC with PKCS7Padding in Ruby
require "openssl"
require "digest"
def aes128_cbc_encrypt(key, data, iv)
key = Digest::MD5.digest(key) if(key.kind_of?(String) && 16 != key.bytesize)
iv = Digest::MD5.digest(iv) if(iv.kind_of?(String) && 16 != iv.bytesize)
aes = OpenSSL::Cipher.new('AES-128-CBC')
aes.encrypt
aes.key = key
aes.iv = iv
@kennethkalmer
kennethkalmer / fix_assign.rb
Created July 21, 2011 16:54
Helper files for migrating from rspec-rails 1.3 to rspec-rails 2.6
#!/usr/bin/env ruby
#
# Migrate from old assigns[]= to assign()
Dir["spec/**/*_spec.rb"].each do |file|
changed = File.readlines( file ).map do |line|
pattern = /assigns\[(:[\w\d_]+)\] = (.+)$/
if line =~ pattern
puts "Match: #{line}"
puts "Assign: #{$1}"
@robfletcher
robfletcher / DatabaseSchemaSpec.groovy
Created August 18, 2011 16:27
Example of data-driving a Spock specification using database metadata
import grails.plugin.spock.IntegrationSpec
import java.sql.Connection
import javax.sql.DataSource
import spock.lang.*
class DatabaseSchemaSpec extends IntegrationSpec {
@Shared def dataSource
@Shared List<String> tableNames
@whatupdave
whatupdave / emhiredis.rb
Created August 23, 2011 22:20
emhiredis example
require 'em-hiredis'
module EMServer
def post_init
puts "-- someone connected to the echo server!"
end
def receive_data data
redis = EM::Hiredis.connect
redis.callback do
@monde
monde / watermark.rb
Created August 25, 2011 16:27
imagemagic watermark processor for Paperclip
module Paperclip
class Watermark < Processor
##
# A Paperclip::Processor for watermarking images with imagemagick's
# composite command.
#
# Place this code in lib/paperclip_processors/watermark.rb or into a Rails initializer.
#
# Example: All orginal files are resized to be at most 480 pixels in
@Melipone
Melipone / remove-duplicates
Created October 14, 2011 14:53
remove-duplicates in clojure based on the distinct function
(defn remove-duplicates
"Returns a lazy sequence of the elements of coll with duplicates removed using a predicate"
[coll pred]
(let [step (fn step [xs seen]
(lazy-seq
((fn [[f :as xs] seen]
(when-let [s (seq xs)]
(if (some #(pred f %) seen)
(recur (rest s) seen)
(cons f (step (rest s) (conj seen f))))))
@superchris
superchris / loadTemplates.js
Created November 12, 2011 01:31
loading templates in jasmine specs
jasmine.Fixtures.prototype.loadTemplate_ = function(template) {
var templateUrl = "/backbone/templates/" + template;
var self = this;
$.ajax({
async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
cache: false,
dataType: 'html',
url: templateUrl,
success: function(data) {
$('#' + self.containerId).append(data);
@stympy
stympy / deploy.rb
Created December 12, 2011 21:27
Skip asset pre-compilation when deploying if the assets didn't change
namespace :deploy do
namespace :assets do
task :precompile, :roles => :web, :except => { :no_release => true } do
from = source.next_revision(current_revision)
if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
else
logger.info "Skipping asset pre-compilation because there were no asset changes"
end
end