Skip to content

Instantly share code, notes, and snippets.

View reggieb's full-sized avatar

Rob Nichols reggieb

View GitHub Profile
@reggieb
reggieb / Preferences.sublime-settings
Created February 14, 2023 10:23
Sublime settings
{
"ignored_packages":
[
"Vintage",
],
// Controls where trailing white space is removed on save.
// - "none": Do not remove any trailing white space on save.
// - "all": Remove all trailing white space on save.
@reggieb
reggieb / fixture.rb
Last active December 6, 2022 10:35
Use fixtures along side FactoryBot
FixtureError = Class.new(StandardError)
#
# Wrapper for fixtures so they can be loaded with a FactoryBot type syntax
# Usage:
# fixture :user, :admin
#
# This will try to find a model with an id that matches the fixture label
def fixture(model_name, label)
model = model_name.to_s.classify.constantize
id = ActiveRecord::FixtureSet.identify(label)
@reggieb
reggieb / sort_by_array_position.rb
Last active September 1, 2021 12:31
Sort by array position.
STATUSES = [:foo, :bar, :this, :that]
scope :by_status, -> do
status_strings = STATUSES.join(',')
joins(
"JOIN unnest('{#{status_strings}}'::text[]) WITH ORDINALITY t(status, order_by_status) USING (status)"
).order('order_by_status')
end
@reggieb
reggieb / markdown_parsing.rb
Last active June 18, 2020 14:46
An experiment to try parsing markdown using parslet gem.
require 'parslet'
class Store
class << self
def headings
@headings ||= []
end
def links
@links ||= []
@reggieb
reggieb / csv_play.rb
Last active March 30, 2020 07:20
Create a CSV file from an array of hashes
require 'csv'
# This is the source data - basically an array of hashes
data = [
{a: 1, b: 2},
{a: 3, b: 4}
]
# This is a path to the file we are going to create.
# __dir__ is the directory of the current file - that is, this file's directory
@reggieb
reggieb / x_builder.rb
Last active October 11, 2019 09:48
XBuilder - A simple tool to build complicated XML objects from nested components
require "active_support/core_ext"
class XBuilder
attr_reader :data, :namespace, :root
def initialize(data)
@namespace = data.delete(:_namespace)
@root = data.delete(:_root)
@data = data
end
@reggieb
reggieb / salary_pattern_generator.rb
Created June 4, 2019 13:20
Salaray Pattern Generator: Tool for creating sets of data for salaries paid in different patterns (weekly, two weekly, four weekly and monthly)
require 'active_support'
require 'active_support/core_ext/integer/time'
class SalaryPatternGenerator
def self.generate(*args)
new(*args).generate
end
attr_reader :period, :salary, :day_offset, :salary_offset, :start_at
@reggieb
reggieb / identify_applicant.rb
Created November 6, 2018 14:46
Example of service to gather data from TrueLayer with token
module TrueLayer
class IdentifyApplicant
TRUE_LAYER_URL = 'https://api.truelayer.com'.freeze
attr_reader :token, :error
def initialize(token)
@token = token
end
@reggieb
reggieb / application.html.erb
Last active September 20, 2018 10:41
Add generic page titles
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title><%= page_title %></title>
@reggieb
reggieb / rspec_spy_example.rb
Created July 10, 2018 08:51
Using spys in rspec to mock connections to services
# Set up the spy object
let(:chargebee_site) { spy(ChargebeeSite) }
# Hook it into the work flow
before do
allow(ChargebeeSite).to receive(:new).and_return(chargebee_site)
end
# use `have_received` to test connection
it 'creates a chargebee coupon' do