Skip to content

Instantly share code, notes, and snippets.

View berkes's full-sized avatar

Bèr Kessels berkes

View GitHub Profile
@berkes
berkes / line_item.rb
Created February 21, 2015 11:03
Ruby Metaprogramming to create a duck-type converter. Example is a converter that allows me to convert a Product Duck Type into a self (LineItem).
module Spree
module Piwik
class LineItem
include ActiveModel::Model
ATTRIBUTES = [:sku, :name, :price, :quantity, :categories]
attr_accessor(*ATTRIBUTES)
##
# Create a LineItem from a Spree::LinteItem-ish thing (line_items,
@berkes
berkes / ;etc;nginx;sites-available;examplecom.conf
Last active August 29, 2015 14:07 — forked from michiels/0_site.conf
Filenames cannot have /, so `s/;/\//g`
server {
listen 80;
server_name example.com;
root /u/apps/examplecom/current/public;
passenger_enabled on;
passenger_app_env production;
try_files $uri @app;
location @app {
@berkes
berkes / sources.list
Created September 26, 2014 12:47
vanilla-ish sources.list for 14.10
#############################################################
################### OFFICIAL UBUNTU REPOS ###################
#############################################################
###### Ubuntu Main Repos
deb http://nl.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://nl.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
###### Ubuntu Update Repos
deb http://nl.archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
#!/usr/bin/env ruby
require "sinatra"
include Math
# Anything but a space, number, .+-*/ or ^ is dissallowed
WHITELIST = %r{[^ 0-9.+\-*\/\^]+}
html = <<-EOT
<html>
@berkes
berkes / hamster2timetrap.rb
Created September 8, 2013 12:02
Quick-n-dirty import of hamster time tracking (https://projecthamster.wordpress.com) into timetrap (https://github.com/samg/timetrap).
require "sqlite3"
hamster = SQLite3::Database.new ".local/share/hamster-applet/hamster.db"
timetrap = SQLite3::Database.new ".timetrap.db"
unparsed = hamster.execute("select * from facts inner join activities on activities.id = facts.activity_id inner join categories on categories.id = activities.category_id")
entries = unparsed.map do |e|
timetrap.execute("insert into entries (note, start, end, sheet) values (?, ?, ?, ?)",
"#{e[6]} #{e[4]}",
@berkes
berkes / generate.php
Last active December 22, 2015 09:28
Siege
<?php
$data = "";
for ($i = 0; $i < 52000; $i++) {
$data .= "X";
}
for ($i = 0; $i < 1000; $i++) {
$name = uniqid("siege-") . ".php";
$fp = fopen($name, 'w');
fwrite($fp, "<?php echo \"file {$i} {$name}\"; /** {$data} **/ ?>");
@berkes
berkes / 478-backup-arbitrary-variables.patch
Created August 31, 2013 10:40
Add chapter about how to pass in arbitrary variables. This closes #478
diff --git a/Performing-Backups.md b/Performing-Backups.md
index d5e1511..472e9a2 100644
--- a/Performing-Backups.md
+++ b/Performing-Backups.md
@@ -191,6 +191,24 @@ The `backup perform` command will exit with the following status codes:
**2**: All triggers were _processed_, but some failed.
**3**: A fatal error caused Backup to exit. Some triggers may not have been processed.
+Passing Arbitrary Variables
+---------------------------
<?php
class View{
var $attributes = array();
var $object;
public function __construct($object) {
$this->object = $object;
$reflection = new ReflectionClass($object);
$props = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($props as $prop) {
@berkes
berkes / smartcropper_document.rb
Created November 28, 2012 14:58
smartcropper document generator
require 'RMagick'
require 'smartcropper'
class DemoChopper
def initialize width, height, filename
@width, @height, @filename = width, height, filename
@files = [{
image: ::Magick::ImageList.new(filename).resize_to_fit!(600, 1000),
@berkes
berkes / pass_by_ref_WTF.php
Created October 29, 2012 13:46
another WTF in PHP. Makes sense, technically, but as a use it makes no sense.
<?php
function foo($bar) {
var_dump($bar);
}
foo(array()); # => array(0) {}
## Works as expected. But now...
function foo(&$bar) {
var_dump($bar);