Skip to content

Instantly share code, notes, and snippets.

@apeiros
apeiros / benchmark.js
Created October 27, 2012 01:32
js benchmark utils
/* Utilities to benchmark code. Look at Benchmark.pbench */
function Benchmark() {
}
// Install a global 'pbench' function for convenience
Benchmark.convenience = function convenience() {
window.pbench = Benchmark.pbench
}
/*
@apeiros
apeiros / application_controller.rb
Created July 27, 2012 07:09
EWS - Multiple DB connections from rails session
# app/controllers/application_controller.rb
class ApplicationController
before_filter :setup_ews_client
private
def setup_ews_client
Thread.current[:ews_client] = proc { ews }
end
def ews
@ews ||= Ews.client(session.slice(:username, :password, :email).merge(endpoint: ews_endpoint))
class Array
def remove(items_to_delete)
lut = Hash.new(0)
items_to_delete.each do |e| lut[e] += 1 end
result = []
each do |e|
if lut[e] > 0
lut[e] -= 1
else
result << e
@apeiros
apeiros / print_resource_usage.rb
Created July 2, 2012 18:45
Prints memory and cpu footprint of the server (uses ps in a subshell)
module Kernel
# Prints memory and cpu footprint of the server (uses ps in a subshell,
# portability is therefore limited)
def print_resource_usage
ps_out = `ps -o vsz,rss,%cpu,%mem -p #{$$}`
vsz, rss, cpu, pmem = ps_out.scan(/\d+(?:[.,]\d+)?/).map { |e| e.gsub(/,/,'.').to_f } # ps on 10.5.1 outputs ',' instead of '.' for MEM%
virtual, real = (vsz-rss).div(1024), rss.div(1024)
$stdout.printf "%dMB real, %dMB virtual, %.1f%% CPU, %.1f%% MEM\n", real, virtual, cpu, pmem
end
module_function :print_resource_usage
@apeiros
apeiros / gist:3029233
Created July 1, 2012 18:53
Failure of `extend self`
require 'ostruct'
module Normalizer
extend self
def trim(str)
str.strip
end
end
obj = OpenStruct.new
Moved to https://github.com/apeiros/available
@apeiros
apeiros / capabilities.rb
Created June 29, 2012 22:38
Require files and provide an interface to check what's available
Moved to https://github.com/apeiros/available
@apeiros
apeiros / inheritable_attributes.rb
Created June 19, 2012 17:15
Inheritable class level instance variables
module InheritableAttributes
def self.extended(klass)
klass.instance_variable_set(:@inheritable_attributes, [])
end
attr_reader :inheritable_attributes
def inheritable_attribute(*args)
@inheritable_attributes.concat(args)
singleton_class.send :attr_accessor, *args unless args.empty?
@apeiros
apeiros / remplate.rb
Created March 19, 2012 14:13
Controller-less rails template
class Remplate < AbstractController::Base
include AbstractController::Rendering
include AbstractController::Layouts
include AbstractController::Helpers
include AbstractController::Translation
include AbstractController::AssetPaths
include Rails.application.routes.url_helpers
self.view_paths = "app/views" # TODO: get it from Rails.application if available
@apeiros
apeiros / gist:1791790
Created February 10, 2012 19:03
Enumerable#last, halfway scalable
module Enumerable
unless method_defined?(:last) then
def last(n=nil)
reverse_each_method = method(:reverse_each)
has_reverse_each = reverse_each_method && reverse_each_method.owner != Enumerable # native reverse_each needed
if n then
return_value = []
if has_reverse_each then
reverse_each { |val|
return_value.unshift(val)