/blog/index.xml

Creating Elixir Protocol with defprotocol

Creating Elixir Protocol with defprotocol

Elixir has a very flexible feature called Protocols. These can be used for helper utilities modules.

defprotocol Double do
  def double(input)
end

defimpl Double, for: Integer do
  def double(int) do
    int * 2
  end
end

defimpl Double, for: List do
  def double(list) do
    list ++ list
  end
end

Hoping I can do more on how this can be used in a phoenix app.

Read More

Elixir's get nexted key and value in nested Map

Elixir’s get nexted key and value in nested Map

Although it is not super intuitive since it does not belong to the Map module, Elixir has a built in get_in function which is used to retrieve a nested value in nested maps using an array of keys.

movie_map = %{ movie: %{ title: "Deadpool"} }
title = get_in(movie_map, [:movie, :title])
=> nil
# When value is not present, returns nil
description = get_in(movie_map, [:movie, :description])
=> nil
director_name = get_in(movie_map, [:movie, :cast, :director, :first_name])
=> nil

Read More

Chaining Elixir's "with" context

Chaining Elixir’s “with” context

Elixir has a powerful withconstruct that isn’t availabe in ruby. This is an elegant pattern for a language. It can be used to pull out individual values or it can be chained.

Individual value example (yes, a bit contrived):

def divide(values) do
  with {:ok, dividend} <- Map.fetch(values, :dividend),
       {:ok, divisor} <- Map.fetch(values, :divisor) do
    {:ok, dividend / divisor}
  end
end

Read More

Building a Baby Age Calculuator with Phoenix/Elixir LiveView

In this article I share how I was able to create a Baby Age Calculator using Phoenix/Elixir LiveView.

If you just want to know how old your child is, jump over to babyweeks.herokuapp.com. If you want to download the source code, goto github.com/monkseal/babyweeks-phx-liveview

Building a Baby Age Calculuator with Phoenix/Elixir LiveView

LiveView - Phoenix/Elixir’s Javascript killer?

As described here and here — “Phoenix LiveView is an exciting new library which enables rich, real-time user experiences with server-rendered HTML. LiveView powered applications are stateful on the server with bidrectional communication via WebSockets, offering a vastly simplified programming model compared to JavaScript alternatives.”

If you don’t want to write Javascript to have a real-time web application, Phoenix LiveView provides an interesting alternative with server-rendered HTML.

Being one to avoid bold pronouments, I won’t commit myself to say that LiveView will deal a death blow to javascript in your app. For one, you may already have a lot Javascript in your app. Maybe some if you even like. However, if you are interested in managing state on the server, LiveView is quite compelling.

Read More

6 things missing from Rails

… That you will probably end up creating yourself …

When should you throw everything out and start over

After over 12 years of working with Ruby on Rails on multiple different codebases, I’ve some common themes. This is my list of patterns or types of objects you will see in Rails applications.

1. Query Objects.

ActiveRecord provides the scope macro which allows us to define queries related to that model. However, for larger, more complex queries that span multiple models, scopes can often lead to a patchwork of distributed logic. To address this, I prefer to build stand alone a Query object. I usually place this in a separate folder: `app/queries’. The following is an example of query object:

A simple base query object:

module Kenglish
  QueryResult = Struct.new(:data, :meta)

  class Query
    class << self
      def result(*args)
        new(*args).result
      end
    end

    attr_reader :params, :relation

    def initialize(params, relation)
      @params = params
      @relation = relation
    end

    def build_result(data, meta)
      QueryResult.new(data, meta)
    end
  end
end

Read More

When should you throw everything out and start over?

… TLDR; Almost never

When should you throw everything out and start over

I have worked with a few engineering teams and managers that expressed something like this:

Our current system is so buggy and problematic, if we could just throw the whole thing out and start over we wouldn’t have all these issues!

This sentiment expresses a common human desire for a fresh start, a do-over. Our legal system accommodates this through personal and corporate bankruptcies. For a brick and mortar business like a Salon or a Restaurant, this might take the form of moving locations or wall-to-wall demolition. Why can’t we do with this our software projects?

Read More

My Favorite Podcasts of 2018

Driving, walking, doing dishes, I always seem to be listening to something. Most of these are non-tech but here’s what I’ve listened to the most last year: EconTalk One of the first podcasts I ever subscribed to, this year the host, Russ Roberts, has brought on a wide range of guests. Revolutions I found this one on vacation when I was looking for something long to listen to. I started from the beginning and I’m still on the episodes from 2017!

Read More