Skip to content

Instantly share code, notes, and snippets.

Kris Leech krisleech

Block or report user

Report or block krisleech

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View rescue_arguments.rb
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rspec"
end
BoomError = Class.new(StandardError)
RSpec.describe 'rescue arguments' do
@krisleech
krisleech / worker.rb
Created May 14, 2019
Worker (sidekiq) module to report exceptions only when last retry has failed
View worker.rb
# app/models/my_worker.rb
class MyWorker
include Carbon::Worker
BoomError = Class.new(StandardError)
retry_without_notification BoomError
def call(attributes)
raise BoomError
@krisleech
krisleech / notes.rb
Created Apr 25, 2019
ActiveRecord locking
View notes.rb
# Prevent other processes/threads from updating rows, uses "FOR UPDATE" lock by default:
Recruitment::CpmsTotal.transaction do
totals = Recruitment::CpmsTotal.not_sent.lock.each # [1]
sleep(40)
end
# [1] each forces query to happen, #all on its own does not.
@krisleech
krisleech / all.rb
Created Apr 3, 2019
inline ActiveRecord and Bundler
View all.rb
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "activerecord"
gem "sqlite3"
end
require "active_record"
require "logger"
@krisleech
krisleech / bg.md
Last active Mar 29, 2019
Blood Glucose on low carb
View bg.md

Tuesday

Time BG (mmol/l)
13:43 4.4
15:00 4.8
16:44 4.2
19:30 6.9
22:25 5.1
@krisleech
krisleech / example.rb
Last active Jan 16, 2019
Where to put `if`, shallow or deeper?
View example.rb
# Lets say we have a Book model and we want to fetch some data about the book from an API and store it in the model.
# A Book needs an identifier before it can be fetched from the API. Not all books have an identifier, e.g. it may not have been entered yet.
# Do we silently return in FetchFromApi when the book has no identifer:
class FetchFromApi
def call(book)
return unless record.identifier.present?
# do API call and update record
@krisleech
krisleech / app.rb
Created Jan 14, 2019
Inline Ruby project with ActiveRecord
View app.rb
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'active_record'
gem 'sqlite3'
end
View example.rb
if study.require_participant_type?
packed_permutations << Recruitment::ParticipantType.pluck(:identifier).append(nil)
else
packed_permutations << [].append(nil)
# or packed_permutations << [nil] (same, but less symmetry)
end
# or
packed_permutations << (study.require_participant_type? ? Recruitment::ParticipantType.pluck(:identifier) : []).append(nil)
@krisleech
krisleech / HOWTO.md
Created Oct 29, 2018
Callbacks for Service
View HOWTO.md
# in engine

class CreateStudy
  extend ActiveModel::Callbacks
  define_model_callbacks :create

  def call(form)
    if form.valid?
      @study = Study.new(form.study_attributes)
@krisleech
krisleech / HOWTO.md
Last active May 5, 2019
ActiveModel comforming Form object based on dry-type / dry-validation
View HOWTO.md
require 'dry-validation'
require 'dry-struct'
require 'active_model/errors'

require_relative '../create_study'

module MyApp
  module Types
    include Dry::Types.module
You can’t perform that action at this time.