Skip to content

Instantly share code, notes, and snippets.

@joshnuss
joshnuss / migration.exs
Last active December 31, 2015 11:59
ecto migrations w/ create/alter macros
defmodule MigrationRunner do
use GenServer.Behaviour
@server_name :migration_runner
@full_name {:local, @server_name}
def start_link do
:gen_server.start_link(@full_name, __MODULE__, nil, [])
end
@joshnuss
joshnuss / attribute_key_writer.rb
Created February 11, 2014 05:47
Set model relationship using key instead of object
module AttributeKeyWriter
extend ActiveSupport::Concern
module ClassMethods
protected
# Allow writing an attribute by key/code
#
# Example:
# belongs_to :currency
# attr_key_writer(:currency, key: "code")
@joshnuss
joshnuss / white_noise.rb
Last active August 29, 2015 14:01
White Noise Generator
=begin
White Noise Generator
http://en.wikipedia.org/wiki/White_noise
Generates a .wav file with random noise.
Copy to phone, and run on loop.
`gem install wavefile`
=end
@joshnuss
joshnuss / .vimrc
Last active August 29, 2015 14:02
Running Elixir tests with vim
" put this .vimrc in your project's root folder
" type ,t to run all tests
map <Leader>t :!mix test<CR>
" type ,e to run current test
map <Leader>e :!mix test %<CR>
@joshnuss
joshnuss / card_type.exs
Created July 15, 2014 14:37
Find credit card type using pattern matching
# usage:
#
# iex> CreditCard.type(%{number: "4242424242424242"})
# => :visa
defmodule CreditCard do
defstruct [:number, :expiration, :cvc]
# find the type of a credit card
# attempt map for first character, first 2 characters, etc (up to 4 characters)
@joshnuss
joshnuss / xml_builder.exs
Last active August 29, 2015 14:04
XML Builder for Elixir
# XML Builder - inspired by jimweirich's ruby builder
defmodule Builder do
def xml(elements) when is_list(elements),
do: Enum.map_join(elements, &xml/1)
def xml(element_name) when is_atom(element_name) or is_bitstring(element_name),
do: xml({element_name})
def xml({element_name}),
do: "<#{element_name}/>"
@joshnuss
joshnuss / proxy.exs
Last active November 28, 2019 16:39
A module for defining GenServer proxies
# defines a generic proxy
defmodule GenProxy do
defmacro __using__(_) do
quote location: :keep do
use GenServer
def handle_call(msg, from={_process, ref}, state) do
case proxy_call(msg, from, state) do
{:forward, server, new_state} ->
:erlang.send(server, {:"$gen_call", {self, ref}, msg}, [:noconnect])
@joshnuss
joshnuss / reservoir.exs
Last active August 29, 2015 14:04 — forked from alco/reservoir.exs
defmodule Enum2 do
import Enum
def sample(collection),
do: hd(sample(collection, 1))
# http://en.wikipedia.org/wiki/Reservoir_sampling
def sample(collection, count) do
results = take(collection, count)
// Example Angular.js form built with components (kind of like formtastic)
// `field`: supports text, number, email, checkbox, select, textarea, etc..
// generates label and appropriate field type, withcheckmark/error icon when field is valid/invalid
.well
h1 Contact
super-form(name="contactForm" submit="send()")
field(model="message.email" type="email" required autofocus)
field(model="message.name" type="text" required)
field(model="message.topic" type="select" required options="topic as topic for topic in topics")
@joshnuss
joshnuss / exampleController.coffee
Created September 12, 2014 18:37
Angular Flash Service
angular.module('Foo').controller 'ExampleCtrl', ($scope, Flash) ->
$scope.doSomething = ->
Flash.info "Hello There!"