Blog

RSS Feed

Add a Little Safety to your Elixir Structs with TypedStruct

<p>I’m working on an Elixir app at the moment and really enjoying it, but some of my recent dabbling in the type-safe worlds of <a href="https://elm-lang.org">Elm</a> and <a href="https://crystal-lang.org">Crystal</a> have left me desiring a bit more structure in my code. The app I’m building involves a multi-step data transformation and so I have a data structure to properly represent this process. But since Elixir is a dynamically typed language, you can’t, for example, have a non-nillable field in a struct. The Elixir/Erlang ecosystem does, however, have a type-checking syntax called <a href="https://hexdocs.pm/elixir/typespecs.html">Type Specs</a>, along with a tool, <a href="http://erlang.org/doc/man/dialyzer.html">Dialyzer</a>,...</p>

Read More...

Upcoming Phoenix Authentication Solution

<p>I’m in the middle of writing an application in <a href="https://elixir-lang.org">Elixir</a> and <a href="https://www.phoenixframework.org">Phoenix</a>, so when I saw a link to <a href="https://dashbit.co/blog/a-new-authentication-solution-for-phoenix">an article by José Valim</a> on an upcoming authentication solution for Phoenix, my initial reaction (before reading it) was negative. José is the author of the <a href="https://github.com/plataformatec/devise">Devise framework</a> for Ruby-on-Rails, so I assumed it was going to be the same idea, but for Elixir and Phoenix. I’ve implemented Devise in a handful of Rails apps, and each and every time I ended up ripping it out and writing my own auth solution (often based on <a href="http://railscasts.com/episodes/250-authentication-from-scratch-revised">this Railscasts tutorial</a>). The reason is that while...</p>

Read More...

JavaScript Sprinkles with AlpineJS

<p>I’m a big of <a href="https://reactjs.org">ReactJS</a>, but sometimes it is way too heavy for what I need. The majority of the applications I create are server-rendered and just need some “sprinkles” of JavaScript. In the past, I’ve leaned on <a href="https://jquery.com">jQuery</a> for this, but I’m not a fan of the imperative nature of it.</p> <p>Recently, I stumbled upon AlpineJS, which is a super minimalist framework that lets you write declarative sprinkles of JavaScript without resorting to something heavier like React or <a href="https://vuejs.org">Vue</a>. It comes in at just 6.4kb (compressed), less than a quarter of the size of Vue, React, or jQuery...</p>

Read More...

Enable Spell Checking in Vim for Markdown and Git Commit Messages

<p>I had a ridiculous typo in a Git commit message recently, so I decided to explore spell checking in Vim. As it turns out, it’s extremely easy.</p> <p>I only wanted it enabled for Git commit messages and markdown, so I added the following to my vimrc / init.vim:</p> <div class="highlight"><pre class="highlight viml"><code><span class="c">" Spell-check Markdown files and Git Commit Messages</span> autocmd <span class="nb">FileType</span> markdown <span class="k">setlocal</span> <span class="nb">spell</span> autocmd <span class="nb">FileType</span> gitcommit <span class="k">setlocal</span> <span class="nb">spell</span> </code></pre></div> <p>By doing this, it highlights potential misspellings in red underline. You can see a list of potential corrections by moving the cursor over the word in normal mode and...</p>

Read More...

Dark Mode and other site updates

<p>Inspired by iOS’s new dark mode (and halloween!), I decided to a dark mode to the website using the new <code>prefers-color-scheme</code> media query. It was surprisingly easy to do. After I’d changed the </p> <p>This took care of 90% of it:</p> <div class="highlight"><pre class="highlight css"><code><span class="k">@media</span> <span class="p">(</span><span class="n">prefers-color-scheme</span><span class="p">:</span> <span class="n">dark</span><span class="p">)</span> <span class="p">{</span> <span class="nt">html</span> <span class="p">{</span> <span class="nl">background-color</span><span class="p">:</span> <span class="m">#19222b</span><span class="p">;</span> <span class="nl">color</span><span class="p">:</span> <span class="m">#eee</span><span class="p">;</span> <span class="p">}</span> <span class="nc">.page-wrapper__inner</span> <span class="p">{</span> <span class="nl">background-color</span><span class="p">:</span> <span class="m">#222f3b</span><span class="p">;</span> <span class="nl">color</span><span class="p">:</span> <span class="m">#eee</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> </code></pre></div> <p>Using a straight black-and-white background and foreground combo looks too harsh on the eyes, so you want to find a gray-ish background. Mine is a bluish-grayish...</p>

Read More...

Quickly Convert 4 Spaces (or Tabs) to 2 spaces in Vim

<p>Here&rsquo;s a handy Vim command that I find myself searching for often. It could easily be made into a function in your vimrc.</p> <div class="highlight"><pre class="highlight viml"><code><span class="c">" 4 spaces to 2 spaces</span> %s;^\<span class="p">(</span>\s\<span class="p">+</span>\<span class="p">)</span>;\<span class="p">=</span><span class="nb">repeat</span><span class="p">(</span><span class="s1">' '</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="nb">submatch</span><span class="p">(</span><span class="m">0</span><span class="p">))</span>/<span class="m">2</span><span class="p">)</span>;<span class="k">g</span> <span class="c">" Tab to 2 spaces</span> %s<span class="sr">/\t/</span> /<span class="k">g</span> </code></pre></div>

Get the Previous Expression Value in IEx

<p>When using Elixir, I&rsquo;ve long missed the special <code>_</code> helper from <a href="https://en.wikipedia.org/wiki/Interactive_Ruby_Shell">irb</a> that returns the result of the previous expression. Sometimes I actually type it out of pure habit.</p> <p>Little did I know that Elixir has the same thing, except better.</p> <p>IEx.Helpers has a function <code>v(n \\ -1)</code> that returns the value of the nth expression in the session history. So <code>v</code> alone returns the previous expression and <code>v(-2)</code> returns the one before it.</p> <div class="highlight"><pre class="highlight elixir"><code><span class="n">iex</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">&gt;</span> <span class="s2">"abc"</span> <span class="n">iex</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">&gt;</span> <span class="s2">"zyx"</span> <span class="n">iex</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">v</span> <span class="s2">"zyx"</span> <span class="n">iex</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">v</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span> <span class="s2">"abc"</span> </code></pre></div>

Ruby Symbol and String Array Shorthand

<p>Ruby has some handy Array shorthands, but I always forget which is which, so I thought I should write them down.</p> <h3 id="symbol-array-shorthand">Symbol Array Shorthand:</h3> <div class="highlight"><pre class="highlight ruby"><code><span class="sx">%i{foo bar}</span> <span class="c1"># =&gt; [:foo, :bar]</span> </code></pre></div> <h3 id="string-array-shorthand">String Array Shorthand:</h3> <div class="highlight"><pre class="highlight ruby"><code><span class="sx">%w{foo bar}</span> <span class="c1"># =&gt; ["foo", "bar"]</span> </code></pre></div> <p>Both also work with square brackets <code>%w[a b c]</code> or parenthesis <code>%i(a b c)</code></p>

Running Elixir tests in VSCode

<p>In Vim-land, I use the <a href="https://www.github.com/janko-m/vim-test">vim-test plugin</a> for quickly executing tests from a command line shortcut<sup id="fnref1"><a href="#fn1">1</a></sup>. I wanted to reproduce this behavior in <a href="https://code.visualstudio.com">Visual Studio Code</a>, but I couldn’t find an extension that worked in multiple languages (namely, Ruby, Elixir, Javascript, and Elm). I’m mostly just using VSCode for Elixir, but I still liked the idea of finding a more general purpose solution.</p> <p>So instead I used VSCode’s support for <a href="https://code.visualstudio.com/docs/editor/tasks">Tasks</a> to build the functionality myself. So in my project’s <code>tasks.json</code> file, I have the following 3 tasks for running all tests, a single...</p>

Read More...

SSH Tunnels into Production Rails Database

<p>Today I needed to access a production database for my Rails app directly using a GUI application on my mac, so I figured out that this can be done by creating an SSH tunnel like so:</p> <div class="highlight"><pre class="highlight shell"><code>ssh <span class="nt">-Ng</span> <span class="nt">-L</span> &lt;local-port&gt;:&lt;remote-host&gt;:&lt;remote-port&gt; &lt;user&gt;@&lt;remote-host&gt; ssh <span class="nt">-Ng</span> <span class="nt">-L</span> 3307:100.64.26.11:3307 adam@100.64.26.11 </code></pre></div> <p>And then, in your Rails app, update the database.yml as if you were connecting to a local database, but specify the proper database name.</p> <div class="highlight"><pre class="highlight yaml"><code><span class="na">development</span><span class="pi">:</span> <span class="na">adapter</span><span class="pi">:</span> <span class="s">mysql2</span> <span class="na">database</span><span class="pi">:</span> <span class="s">myapp_production</span> <span class="na">host</span><span class="pi">:</span> <span class="s">127.0.0.1</span> <span class="na">port</span><span class="pi">:</span> <span class="m">3307</span> <span class="na">username</span><span class="pi">:</span> <span class="s">root</span> <span class="na">password</span></code></pre></div>

Read More...

Page 1 of 3 Next page ⟶