Skip to content

Instantly share code, notes, and snippets.

@joakimk
joakimk / talks_and_articles.md
Last active October 6, 2016 08:09
A collection of good talks and articles by topic. Elixir, Elm, Ruby, ...

Building a little list so I can more easily recommend the best talks and be able re-watch them myself.

Elm

@joakimk
joakimk / Update.elm
Last active September 22, 2016 19:19
One attempt at simple adding or updating a record in a list in Elm
import UpdateList exposing (addOrUpdateById)
update msg model =
case msg of
NewOrUpdatedItem item ->
({model | items = model.items |> addOrUpdateById item}, Cmd.none)
@joakimk
joakimk / socket.js
Last active August 22, 2016 06:45
An example of auto reload on deploy in an Elixir Phoenix app
import {Socket} from "phoenix"
let socket = new Socket("/socket", { params: {} })
socket.connect()
let channel = socket.channel("updates", {})
channel.join()
// Reload the app if it's out of date when it joins the websocket
let revision = null
@joakimk
joakimk / rspec_retries.rb
Last active June 10, 2016 09:10 — forked from seancribbs/rspec_retries.rb
A simple way to retry examples in RSpec using filters and around
# Based on https://gist.github.com/1718985
# Usage:
# 1) Put in spec/support/rspec_retries.rb
# 2) Call from spec_helper.rb: "RSpec::Retries.setup(config)"
# 3) Change around filter to suit your needs.
# Example output:
# ..............
#
@joakimk
joakimk / usage.rb
Created June 9, 2011 13:37
Example of how to setup a server in vmware that installs from a pxe install server
# config = ..
datacenter = VmwareDataCenter.new(config)
datacenter.create_vm(:name => "ServerName", :cpus => 1, :memory => 512, :disk => 5000,
:datastore => "VolumeName", :install_identifier_mac => "some mac addr",
:install_pxe_network_name => "network to look for pxe server on")
# server cookbook https://github.com/joakimk/cookbooks/tree/master/pxe_install_server
@joakimk
joakimk / doc.md
Last active December 25, 2015 03:59
Using rails constant missing features (ActiveSupport::Dependencies) outside of rails

Using ActiveSupport::Dependencies outside of rails seems just as fast as doing manual requires. I've found this out by replacing hundreds of requires in a unit tested code base with ActiveSupport::Dependencies without any change to the time it takes to run the test suite.

Using ActiveSupport::Dependencies also allows you to have circular depedencies like class Foo::Bar in foo/bar.rb and class Foo in foo.rb that points to Foo::Bar in it's class definition (like in a rails validation).

The classical way of solving this without ActiveSupport::Dependencies is to do:

# foo/bar.rb
class Foo
 class Bar
@joakimk
joakimk / gist:6029651
Last active December 19, 2015 22:48
The functional core, imperative shell pattern as I see it. I use this quite a bit. Makes the code simpler to understand and test.
# Functional core
# Tested with unit tests. Mostly simple state based tests (like: invoice.gross_amount.should == 200).
class BuildInvoice
def build
# build invoice in memory..
end
end
# Imperative shell
# Usually just tested with a single test to ensure it stores data in the database
@joakimk
joakimk / README.md
Last active December 17, 2015 18:59
Extensions to minimapper's ActiveRecord mappers that haven't yet been included in minimapper or minimapper-extras (they are only tested as part of the project that uses them).

Association loading

# Load data from the DB
user = user_mapper.find(params[:id], include: { profile: :group })

# Use data in memory (you can't do lazy-loads with minimapper, which is probably
# a very good thing. Lets you avoid N+1 by default).
user.profile.group.name
@joakimk
joakimk / note.md
Created December 17, 2015 09:22
Fix for Oculus Rift DK2 positional tracking lean in out inverted

Writing this down in case it's useful for someone.

The reason why my oculus had inverted positional tracking (when I lean in, it moved me backwards in the 3d scene) was that my camera cable was broken. Ensure the little blue light is on when using the rift.

@joakimk
joakimk / code.rb
Created March 21, 2013 14:51
How to handle serialized associations with minimapper that can be validated.
class Address
include Minimapper::Entity
attribute :street
attribute :postalcode
attribute :city
attribute :country
validates :street, presence: true
end