Skip to content

Instantly share code, notes, and snippets.

View jerel's full-sized avatar

Jerel Unruh jerel

View GitHub Profile
@jerel
jerel / genserver_test.ex
Last active May 16, 2019 20:01
Test code that a GenServer calls by injecting a mock client that sends a message to a named test process
defmodule Example.Server do
use GenServer
defmodule Data do
defstruct client: Example.Client
end
def init(%{client: client}), do: {:ok, %Data{client: client}, 500}
def init(_), do: {:ok, %Data{}, 500}
### Keybase proof
I hereby claim:
* I am jerel on github.
* I am jerel (https://keybase.io/jerel) on keybase.
* I have a public key ASCJUGUw8Ek_glkidrXcFXY-GmF5awcOn7IwVuDINk34sAo
To claim this, I am signing this object:
@jerel
jerel / stream_csv.py
Created June 9, 2017 15:29
Easily export a massive dataset from Django to the browser as a streaming CSV file
import csv
from StringIO import StringIO
from django.http import StreamingHttpResponse
class StreamCSV(object):
def __init__(self):
self._buffer = StringIO()
self._writer = csv.writer(self._buffer)
@jerel
jerel / seed.ex
Created December 19, 2016 15:48
Run seeds in an Elixir Distillery app
defmodule :release_tasks do
def seed do
:ok = Application.load(:myapp)
[:postgrex, :ecto, :logger]
|> Enum.each(&Application.ensure_all_started/1)
Myapp.Repo.start_link
@jerel
jerel / soft-delete.ex
Last active November 1, 2016 18:30
In Elixir soft delete records immediately followed by a real delete a day later. (untested example)
defmodule User do
def all do
from(u in UserSchema, where: is_nil(u.deleted_at)) |> Repo.all
end
def delete(user_id) do
user = Repo.get!(UserSchema, user_id)
changeset = Ecto.Changeset.change(user, deleted_at: Ecto.DateTime.utc)
Repo.update(changeset)
@jerel
jerel / work.sh
Created October 6, 2016 14:56
A bash function I use to force me to do pushups every 30 minutes throughout the work day.
work() {
while true; do
sleep 1800 #30 min
espeak "$1 pushups"
xinput set-prop 14 'Device Enabled' 0 #disable keyboard
xinput set-prop 16 'Device Enabled' 0 #disable mouse
xset dpms force off #disable monitors
sleep 15
xinput set-prop 14 'Device Enabled' 1
xinput set-prop 16 'Device Enabled' 1
@jerel
jerel / application.controller.js
Last active January 28, 2016 19:08
Ember unloadRecord doesn't remove observers
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle'
});
@jerel
jerel / elixir.exs
Last active October 5, 2015 18:30
This is a rough list of things that might initially be confusing to a non-rubyist trying to learn Elixir
# implicit return is fairly obvious but foreign to most languages
def foo(arg) do
arg
end
# foo/2 is the notation for a function that takes two arguments
# optional parenthesis
def foo arg, arg2 do
@jerel
jerel / clear.sh
Created April 13, 2015 19:13
Bash script to clear watchman roots to solve Error: watch EMFILE
watchman watch-list | tr -d '[:space:]' | grep -Po '(?<="roots":\[).*?(?=\])' | grep -Po '(?<=").*?(?="?,)' | while read path; do watchman watch-del "$path"; done
@jerel
jerel / page.js
Last active November 27, 2019 23:24
Making the default Twitter tweet widgets work in an EmberJS application. This adds the twitter widget script after the tweets are in the DOM.
// Method 1
// add the twitter widget library to the bottom of app/index.html (after your Ember app)
<script src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
// initialize it after the content is in the DOM
import Ember from 'ember';
export default Ember.View.extend({
didInsertElement: function() {
window.twttr.widgets.load();