Skip to content

Instantly share code, notes, and snippets.

# stolen from http://github.com/cschneid/irclogger/blob/master/lib/partials.rb
# and made a lot more robust by me
# this implementation uses erb by default. if you want to use any other template mechanism
# then replace `erb` on line 13 and line 17 with `haml` or whatever
module Sinatra::Partials
def partial(template, *args)
template_array = template.to_s.split('/')
template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
options = args.last.is_a?(Hash) ? args.pop : {}
options.merge!(:layout => false)
@jspillers
jspillers / HerokuResqueAutoScale config shortcut
Created April 8, 2011 20:35
Allows each resque worker class to be given auto scaling via config/initializers/resque.rb
# my app had quite a few different worker classes so
# i decided to extend them all from one place:
# config/initializers/resque.rb
if Rails.env == "production" || Rails.env == "staging"
resque_klasses = [
SomeJob,
AnotherJob,
VeryHardWork,
PrettyEasyJobHere
@jspillers
jspillers / input.png
Created June 29, 2011 13:32
Solution for codebrawl PNG pixelation challenge
input.png
#var $context, $input, $shadow, current_tags, tag_name, template, view;
initialize: () ->
template = $('script.tag_mustache').html()
$context = $('span.tagging_container')
$hidden_input = $context.find('.tags_value')
if $context
$shadow = $context.find('.input_shadow')
$input = $context.find('.tag_input')
@jspillers
jspillers / backbone.rails.js
Created December 1, 2011 23:52 — forked from tmarek/backbone.rails.js
Makes Backbone.js play nicely with default Rails setup
//
// Backbone.Rails.js
//
// Makes Backbone.js play nicely with the default Rails setup, i.e.,
// no need to set
// ActiveRecord::Base.include_root_in_json = false
// and build all of your models directly from `params` rather than
// `params[:model]`.
//
// Load this file after backbone.js and before your application JS.
@jspillers
jspillers / backbone_sync_hack.coffee
Created December 2, 2011 00:17 — forked from jumski/backbone_sync_hack.coffee
BackboneJS sync hack to namespace params when saving/updating model
###
Usage:
* add model.name property that will be used as a namespace in the json request
* put this code before your Backbone app code
* use toJSON() as usual (so there is no namespacing in your templates)
* your model's data will be sent under model.name key when calling save()
###
# save reference to Backbone.sync
Backbone.oldSync = Backbone.sync
@jspillers
jspillers / run_spec_multiple.rb
Created January 19, 2012 16:48
run specs multiple times with truncated output
#!/usr/bin/env ruby
ARGV[0].to_i.times do |i|
puts "run #{i+1}"
result = `rake SPEC=#{ARGV[1]}`
puts result.match(/(Finished in \d+.\d+ seconds)/)[1]
puts result.match(/(\d+ examples, \d+ failures?)/)[1]
if result.match(/Failed examples:/)
puts ''
puts 'Failed examples:'
require 'rest_client'
def import(service_url, uri, data)
JSON.parse(
RestClient.post(
"#{service_url}#{uri}",
data.to_json,
content_type: :json,
accept: :json
)
>> hsh = { :foo => ->(x){ {x: x } }}
=> {:foo=>#<Proc:0x007ff2c20320c0@(irb):9 (lambda)>}
>> hsh[:foo]
=> #<Proc:0x007ff2c20320c0@(irb):9 (lambda)>
>> hsh[:foo].call('qwerasdf')
=> {:x=>"qwerasdf"}
@jspillers
jspillers / Deformer.cs
Created April 12, 2017 14:00
one method from a c# class
private void FlattenVolume() {
float flattenToY = Mathf.Lerp(deformerCollider.bounds.min.y, deformerCollider.bounds.max.y, flattenTo);
float flattenHeight = Coord.WorldHeightToTerrainHeight(flattenToY, terrain);
foreach (Vertex v in this.localHeightMap.localHeightmapVertices) {
if (v.insideDeformerCollider || (!v.insideDeformerCollider && this.flattenArea)) {
if (v.areaZ < this.terrainArea.terrainHeights.GetLength(0) || v.areaX < this.terrainArea.terrainHeights.GetLength(1)) {
this.terrainArea.terrainHeights[v.areaZ, v.areaX] = flattenHeight;
}
}