Skip to content

Instantly share code, notes, and snippets.

View kenkeiter's full-sized avatar
🦊

Ken Keiter kenkeiter

🦊
View GitHub Profile
@kenkeiter
kenkeiter / vmware_driver.rb
Created May 9, 2011 15:06
Loose implementation of DC driver for Spherical
require 'deltacloud/base_driver'
require 'spherical'
module Deltacloud::Drivers::VMware
class VMwareDriver < Deltacloud::BaseDriver
# Configure hardware profile options
define_hardware_profile 'default' do
@kenkeiter
kenkeiter / image_placement.html
Created July 19, 2011 20:53
Place images atop other images
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>HTML5 boilerplate—all you really need…</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript" charset="utf-8">
@kenkeiter
kenkeiter / text-extents.js
Created August 28, 2011 19:14
JQuery zoom text to extents, supporting multiple inline spans.
;(function($){
$.fn.textExtents = function(options){
var spans = $('span:visible', this);
var heightMax = parseInt($(this).css('max-height'));
var widthMax = parseInt($(this).css('max-width'));
var fontSize = options.maxFontSizePx;
do{
var textHeight = 0;
var textWidth = 0;
$(spans).each(function(){
@kenkeiter
kenkeiter / text_extents.js
Created July 12, 2012 22:30
Resize text to fit a min/max.
;(function($){
$.fn.textExtents = function(options){
var spans = $('span:visible', this);
var heightMax = parseInt($(this).css('max-height'));
var widthMax = parseInt($(this).css('max-width'));
var fontSize = parseInt($(this).css('font-size')); /* max font size */
do{
var textHeight = 0;
var textWidth = 0;
$(spans).each(function(){
@kenkeiter
kenkeiter / guards.rb
Last active August 3, 2018 19:34
Guard-like things in Ruby
class Proc
def call_with_vars(vars, *args)
Struct.new(*vars.keys).new(*vars.values).instance_exec(*args, &self)
end
end
module Touchy
class GuardError < Exception; end
class GuardSignatureError < GuardError; end
@kenkeiter
kenkeiter / grapeish.rb
Last active December 10, 2015 02:38
Fix grape's incredible brokenness.
require 'sinatra/base'
require 'sinatra/namespace'
# You may need multi_json
class API < Sinatra::Base
# Respond to all errors with JSON
error do
content_type :json
e = env['sinatra.error']
@kenkeiter
kenkeiter / Nested resources.coffee
Created March 15, 2013 20:49
Coffee script nested resources example (may or may not be working -- kind of pseudo-code).
class User extends Spine.Model
url: ->
'/' + @username
collections: ->
unless @collection_klass?
@collection_klass = _.clone(Collection)
@collection_klass.parent = this
@collection_klass
@kenkeiter
kenkeiter / config.ru
Last active December 17, 2015 06:18
If you've tried to mount RESTful resources at '/' with Padrino, you've probably run into issues with its router, and had to :map each action (or develop some idiom to generate the mappings for you). Padrino's router is pretty opinionated; when you're using named controllers, you'll want to make use of `:parent` directives -- which you can't, if …
# You'll need to add 'rack-rewrite' to your Gemfile.
require 'rack/rewrite'
require File.expand_path("../config/boot.rb", __FILE__)
# lazy load the application, instantiating routes.
application = Padrino.application
# Determine which routes NOT to redirect to your root resource.
reserved_routes = YourProject::App.routes.map{|r|
r.path_for_generation.scan /[a-zA-Z0-9]+/i
@kenkeiter
kenkeiter / pyvirtualenv.rb
Created July 6, 2014 01:58
A quick Ruby wrapper for manipulating Python's virtual environments.
require 'open3'
class VirtualEnvError < Exception; end
class PythonVirtualEnv
def self.exists?(path)
File.directory?(path) and File.exists?(File.join(path, 'bin/activate'))
end
@kenkeiter
kenkeiter / rabl_patch.rb
Created July 17, 2014 02:18
If you're using Rabl with DataMapper, lazy collection queries won't be evaluated correctly, and you'll end up SELECTing each record from the database. This patch fixes that issue (albeit, in an ugly way).
module Rabl
class Engine
def collection(data, options={})
self.object(data_object(data))
end
end
module Helpers
def data_name(data_token)