Skip to content

Instantly share code, notes, and snippets.

View joshnuss's full-sized avatar
🤘

Joshua Nussbaum joshnuss

🤘
View GitHub Profile
@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 / 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!"
@joshnuss
joshnuss / output.md
Last active August 29, 2015 14:11
Elixir OpenSCAD Generator

Example's Output

@joshnuss
joshnuss / infinite-example.md
Last active August 29, 2015 14:22
Infinite Scrolling with Angular.js

Infinite Scrolling with Angular.js

Use a simple directive to trigger reloading when nearing bottom of page.

Template

Example of triggering next page when user scroll 300px from bottom of page.

<section ng-controller='ContactsCtrl'>