Skip to content

Instantly share code, notes, and snippets.

View kakra's full-sized avatar
🏠
Let's inject fun into Linux Gaming

Kai Krakow kakra

🏠
Let's inject fun into Linux Gaming
View GitHub Profile
@kakra
kakra / helper_proxy.rb
Created May 21, 2014 12:23
Snippet of a Rails helper proxy to make helpers available from anywhere without polluting namespace
# use helpers from models or any part of your app (you shouldn't do that, however...)
# ... put in config/initializers
#
# class MyModel < ActiveRecord::Base
# def some_attribute
# HelperProxy.instance.number_format some_value, precision: 4
# end
# end
class HelperProxy
@kakra
kakra / weekday.rb
Created September 22, 2008 12:15
How to apply case-when on lambdas
#!/usr/bin/env ruby
class Proc
def ===(*parameters)
self.call(*parameters)
end
end
sunday = Proc.new { |time| time.wday == 0 }
monday = Proc.new { |time| time.wday == 1 }
@kakra
kakra / gist:11975
Created September 22, 2008 12:21
AdoDBRecord usage example
<?php
require('adodbrecord/init.php'); # v0.5 or higher required
# CREATE TABLE cars (id INTEGER PRIMARY KEY,
# brand VARCHAR(50),
# color VARCHAR(50),
# destination VARCHAR(50)
# )
require_once("AdoDBRecord://Car_Base");
@kakra
kakra / shorten-mp3-names.rb
Created September 28, 2008 22:22
Useful if your mp3 player doesn't like fancy long filenames
#!/usr/bin/env ruby
#
# Useful if your mp3 player doesn't like fancy long filenames
#
require 'digest/sha1'
Dir.glob("*.mp3") do |f|
name = f.downcase.split(".");
next if name.length < 2
@kakra
kakra / mp4creator.rb
Created November 11, 2008 11:56
RVideo Extensions
module RVideo
module Tools
class Mp4creator
include AbstractTool::InstanceMethods
attr_reader :raw_metadata
def tool_command
'mp4creator'
end
@kakra
kakra / with_statement.rb
Created August 2, 2009 05:55
A ruby implementation of python's with statement
# A ruby implementation of python's with statement
#
# not sure if this is semantically identical, it was meant as a small excercise
module WithStatement
def with(*objs)
raise "no block given" unless block_given?
options = objs.unshift! if objs.last.is_a? Hash
@kakra
kakra / application_helper.rb
Created August 12, 2009 11:06
Auto-labelling formbuilder, use with :builder => LabellingFormBuilder
module ApplicationHelper
def labelled_form_for(name, object, options, &proc)
form_for(name, object, options.merge(:builder => LabellingFormBuilder), &proc)
end
def multipart_form_for(name, object, options, &proc)
html_options = options.delete(:html) || {}
html_options.merge!(:multipart => true)
form_for(name, object, options.merge(:html => html_options), &proc)
@kakra
kakra / patch_delayed_job.rb
Created November 13, 2009 15:43
patch DelayedJob in the case no default_timezone is defined
module Delayed
class Job < ActiveRecord::Base
def self.db_time_now
(ActiveRecord::Base.default_timezone == :utc) ? Time.now.utc : Time.zone.now
rescue NoMethodError
Time.now
end
end
#slide-images {
position: relative;
display: block;
margin: 0px;
padding: 0px;
width: 738px;
height: 234px
overflow: hidden;
}
@kakra
kakra / tableless.rb
Created November 29, 2009 22:23
tableless ActiveRecord models
# Tableless ActiveRecord models
# http://stackoverflow.com/questions/315850/rails-model-without-database/318919#318919
class Tableless < ActiveRecord::Base
def self.columns
@columns ||= [];
end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)