This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These aren't 100% parallel (they're from different apps with slightly different auth rules) | |
# but they're close enough to demonstrate what `with` can do, I think. | |
# Version 1: using `cond` | |
# Pro: very linear. just go down the list and pick the first truthy result. | |
# Con: Using `cond` maybe doesn't sufficiently emphasize how important the ordering is | |
# here. If you naively swapped the order of two conditions, it could wreck the security. | |
# Basically, in this version, I'm relying on the dev's caution and willingness to read | |
# through my extensive comments. | |
def load_current_user(conn, _opts) do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Sample do | |
# | |
# VERSION 1 | |
# The naive approach: just nest the if/else statements. Pretty readable. | |
# | |
def custom_block(project, label) do | |
# ... validations ... | |
if project do | |
if block = Enum.find(project.custom_blocks, & &1.label == label) do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rid your codebase of haml templates. | |
# Requires Calliope as a dependency in your Mix project. | |
# Usage: `mix run haml.exs path/to/my/haml/file.html.haml | |
# It will spit out the produced eex, which you can copy & paste into place. | |
path = System.argv() |> Enum.at(0) | |
{:ok, haml} = File.read(path) | |
IO.puts "" | |
IO.puts Calliope.render(haml) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Parse and condense `git log` output to include the hash, date, author, message, | |
# and change statistics all on one line. Run the script with the following command | |
# (ie. alias this to `gl` or something short): | |
# git log --pretty=format:"%h %ai (%an) | %s" --shortstat | ruby ~/path/to/git-log-hoc.rb | less -RS | |
# | |
# Sample output (real output is colorized so you can tell the stat numbers apart): | |
# df79691 2016-11-18 (Topher Hunt) | Add Category and its assoc. to Video [14 111 21] | |
# 4f2bf68 2016-11-04 (Topher Hunt) | Video actions scoped to logged-in user [3 27 15] | |
# d6d0ee6 2016-11-04 (Topher Hunt) | Reorganize & refactor auth for Videos [8 64 32] | |
# 5733dc3 2016-11-03 (Topher Hunt) | Authenticate current user [3 36] |