Skip to content

Instantly share code, notes, and snippets.

View virtualstaticvoid's full-sized avatar

Chris Stefano virtualstaticvoid

View GitHub Profile
@virtualstaticvoid
virtualstaticvoid / operation.rb
Created December 17, 2014 08:01
Operation[params] vs Operation.(params) vs Operation(params)
# gem code
module Trailblazer
class Operation
def self.inherited(klass)
qualified_name = klass.name
name_parts = qualified_name.split('::')
method_name = name_parts.last
@virtualstaticvoid
virtualstaticvoid / A Vagrant - InfluxDb + Nginx + Grafana
Last active October 30, 2017 07:09
Vagrant - InfluxDb + Nginx + Grafana
Vagrant - InfluxDb + Nginx + Grafana
@virtualstaticvoid
virtualstaticvoid / rename_episode.py
Created September 25, 2014 11:01
gPodder Rename Episode Extension
# -*- coding: utf-8 -*-
# Rename files based on the episode title
# Copyright (c) 2014-09-25 Chris Stefano <virtualstaticvoid@gmail.com>
# Licensed under the same terms as gPodder itself
# Source code copied and modified from https://github.com/gpodder/gpodder/blob/master/share/gpodder/extensions/rename_download.py
import os
import gpodder
from gpodder import util
@virtualstaticvoid
virtualstaticvoid / git-save
Created September 12, 2014 15:14
Git script to quickly add and commit all modified files
#!/bin/bash
comment=${1:-"Autosaved at $(date +%Y)-$(date +%m)-$(date +%d) $(date +%H:%M)"}
# make sure we're at the root of git repo
#if [ ! -d .git ]; then
# echo "Error: must run this script from the root of a git repository"
# exit 1
#fi
@virtualstaticvoid
virtualstaticvoid / git-reset-modes
Created September 12, 2014 15:12
Git script for resetting the file modes
#!/bin/bash
# make sure we're at the root of git repo
if [ ! -d .git ]; then
echo "Error: must run this script from the root of a git repository"
exit 1
fi
git diff --numstat \
| awk '{if (($1 == "-" && $2 == "-") || ($1 == "0" && $2 == "0")) { $1=$2=""; print }}' \
@virtualstaticvoid
virtualstaticvoid / lambda_coalesce.rb
Last active November 14, 2018 20:21
Ruby Lambda Coalesce
#
# Given a bit of program logic:
#
# x2 = y1 * x1 || y2 / exchange_rate || x2 * exchange_rate / y1 || another
#
# Where y1, x1, y2 and x2 are function calls, which may yield nils
#
def coalesce(*args)
@virtualstaticvoid
virtualstaticvoid / default_values.rb
Created March 19, 2014 16:04
Rails 4 Concern for Default Attribute Values
#
#
# Rails 4 concern for specifying default attribute values on models
#
# E.g. The following user has defaults defined for `active` and `manager` attributes
#
# class User < ActiveRecord::Base
# include Concerns::DefaultValues
#
# default_value :active, true
@virtualstaticvoid
virtualstaticvoid / to_utc_ticks.rb
Created January 31, 2014 15:37
Adds `to_utc_ticks` to `Date`
class Date
def to_utc_ticks
Time.utc(self.year, self.month, self.day).to_i * 1000
end
end
@virtualstaticvoid
virtualstaticvoid / color_support.rb
Created January 31, 2014 15:27
Random color generation
# Thanks to http://www.paulirish.com/2009/random-hex-color-code-snippets for the idea
module ColorSupport
# yields a random color
def self.random_color
color_for((rand() * ((0xFFFFFF + 1) << 0)).to_i.to_s(16))
end
# yields a random color based on the given color
# credit: http://stackoverflow.com/a/43235
@virtualstaticvoid
virtualstaticvoid / active_relation_extensions.rb
Last active May 22, 2019 17:51
A better `find_each` and `find_in_batches` for ActiveRecord, which preserves the original order of the query
module ActiveRelationExtensions
def find_each_with_order(options = {})
find_in_batches_with_order(options) do |records|
records.each { |record| yield record }
end
end
# NOTE: any limit() on the query is overridden with the batch size
def find_in_batches_with_order(options = {})