Skip to content

Instantly share code, notes, and snippets.

View mettledrum's full-sized avatar

Andrew Hoyle mettledrum

View GitHub Profile
@mettledrum
mettledrum / gist:9441272
Last active August 29, 2015 13:57
Rails eager_load controller example
def scoring
@heat = Heat.find(params[:heat_id])
@judge = current_user
@event = Event.find(params[:event_id])
# {st_id => [team#, [team, member, names]]}
@team_info = ScheduledTeam.eager_load(
heat: {
score_system: :score_terms,
scheduled_teams: {
registered_team: {
@mettledrum
mettledrum / recursive_change.py
Last active August 29, 2015 13:59
The classic change-finding recursive algorithm
# gets combinations of coinage for your change
# makes sure not to repeat combos by using skip_list
# generates partial solutions using partial_list
# when value is reached, partial_list is stuffed into final_list
def spare_a_dime_brotha(money, global_skip_list, global_partial_list, final_list):
# make copies local to the scope
partial_list = global_partial_list[:]
skip_list = global_skip_list[:]
@mettledrum
mettledrum / numbers_to_English.rb
Last active August 29, 2015 14:01
Just a simple class to convert integers LT | 1 quadrillion | into their American English equivalent.
# Andrew Hoyle
# converts a +/- integer to its American English equivalent
# includes basic exceptions
# PRE: less than | 1 quintillion | and +/- integer
# allows only numbers, commas, and '+' or '-' in idx[0]
# POST: American English translation of number
class Worder
# AM. ENGLISH
# mixes in logic to make a model understand how to fetch its relationships
# based on the item_id/item_item_type field combination
module VariadicItem
extend ActiveSupport::Concern
included do
belongs_to :item_type
end
# get the item associated with the current polymorphic record into which this module has been mixed
@mettledrum
mettledrum / _comment_forest.html.erb
Last active August 29, 2015 14:05
_partials for recursively displaying nested comments for a posting
<% @comments.each do |comment_hash| %>
<%= render 'comment_tree', comment_hash: comment_hash %>
<% end %>
@mettledrum
mettledrum / PDFify.go
Created May 25, 2015 21:33
use suffix to PDF-ify any ol' endpoint
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/terryh/gopdf"
@mettledrum
mettledrum / agreement.html
Created May 30, 2015 16:13
wkhtmltopdf usage for PDF rendering
<h1>{{.Stuff}} is really neato!</h2>
@mettledrum
mettledrum / fiddle.response.json
Last active April 28, 2016 16:44
beer json response
{
"who": "mettledrum",
"age": 31,
"fav_beers":
[
{
"name": "hot pepper",
"abv": 5.2,
"is_good": false
},
@mettledrum
mettledrum / main.go
Created November 22, 2017 23:03
hard workin go routines
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
@mettledrum
mettledrum / main.go
Last active November 27, 2017 23:51
workers that run until error
package main
import (
"errors"
"fmt"
"sync"
"time"
)
// what the consumer does