Skip to content

Instantly share code, notes, and snippets.

View krisleech's full-sized avatar

Kris Leech krisleech

View GitHub Profile
@krisleech
krisleech / RUBY_DOCKERFILE.md
Created September 26, 2019 08:50
Dockerfile for Ruby application

Ruby 2.6 on Ubuntu with Bundler 2.x

# Dockerfile

FROM ubuntu

RUN apt-get update
RUN apt-get install -y build-essential wget gcc automake autoconf autogen libtool make cmake bison unzip flex libqt4-dev zlib1g zlib1g-dev libqtwebkit-dev libreadline6 libreadline-dev  libreadline6-dev libssl-dev libfontconfig1-dev chrpath mysql-client libxml2-dev libxslt1-dev libxslt-dev libmysqlclient-dev libsqlite3-dev git git-core xvfb socat libyaml-dev libgdbm-dev libncurses5-dev libffi-dev
@krisleech
krisleech / VERSIONED_CLASSES.md
Created September 25, 2019 08:35
Mimicking ActiveRecord::Migration[5.2] class versions

In Rails migrations now need a version number:

class ActiveRecord::Migration[5.2]
  # ...
end

I haven't looked at the code in Rails for this, but one way it could be done would be as such:

@krisleech
krisleech / MODULE_BUILDER.md
Last active June 3, 2020 18:58
Module builder in Ruby

plain #new method

module Foo  
  class Bar < Module
    def initialize(options = {})
      super()
      puts options
    end
  end
@krisleech
krisleech / RUBY_INT_UNITS.md
Last active September 11, 2019 15:34
Integers with units in Ruby

Often we have Integer's but don't have a clue what the unit is unless we encode it in a varible name. How can we prevent adding two numbers which are of different or incompatible units...

class IntegerWithUnit < SimpleDelegator
  attr_reader :unit
  
  def initialize(integer, unit)   
    @unit = unit.to_sym.freeze
    super(integer)
 end 
@krisleech
krisleech / COMMAND_STDOUT.md
Last active September 11, 2019 11:54
inject messages to stdout for command

Lets say we have some object which performs a command:

class ReportAbnormalTempratures
  def self.call; new.call; end
  
  def call
    Temprature.greater_than(Tolerance.today).each do |temprature|     
      # do something...      
    end
@krisleech
krisleech / RAW_REPORTS.md
Created September 9, 2019 10:17
RAW SQL ACTIVERECORD REPORTS

We want to query out database, but not generate ActiveRecord models (which takes additional resources).

Since this report displays a LOT of rows of data we are aiming for speed.

sql = Recruitment::Patient.order(reference: :desc).select(patient_attrs).to_sql

results = ActiveRecord::Base.connection.execute(sql).map do |row|
  Hash[patient_attrs.zip(row)].transform_values(&method(:format_value))
end
@krisleech
krisleech / RESOLVE_CONFIG.md
Last active September 10, 2019 14:15
Resolving different types of configuration

These are the possible configurations we want to support:

config = :invited_on

config = [:invited_on, :started_on]

config = lambda { |s| s.started_on }

config = ResolveValue.new # object which `responds_to?(:call)`
@krisleech
krisleech / RUBY_CSV.md
Last active September 4, 2019 15:00
CSV example
header, *rows = CSV.read(Rails.root.join('my-data.csv'))

rows.map! { |row| Hash[header.zip(row)] } # convert to Array of Hash

rows.map do |row|
  row.transform_values(&method(:format_value))
end

def format_value(value)
@krisleech
krisleech / RUBY_TRICKS.md
Created August 30, 2019 08:38
Ruby Tricks

Merge two hashes with double splat:

{ **{a: 1 }, b: 2 }
@krisleech
krisleech / RELEASE_WISPER.md
Last active November 25, 2019 09:48
Releasing Wisper gem

Setup

Ensure commits are always signed: git config commit.gpgsign true

The gem signing private key should exist at ~/.ssh/gem-private_key.pem (see gemspec).

CHANGELOG

Double check the CHANGELOG reflects all commits since last version and all authors are included.