Skip to content

Instantly share code, notes, and snippets.

View brand-it's full-sized avatar

Brandt Lareau brand-it

View GitHub Profile
@brand-it
brand-it / .irbrc
Last active November 7, 2023 06:39
.irbrc with a bunch of stuff I found useful. This is all sort of hacky but that kinda of the point. Do what you want take what you need. no need to be perfect
# frozen_string_literal: true
MAX_COLUMNS_FOR_TABLE = 15 unless defined?(MAX_COLUMNS_FOR_TABLE)
DEFAULT_EDITOR = 'code' unless defined?(DEFAULT_EDITOR)
class PrettyString < String
# https://no-color.org/
NO_COLOR = ENV.key?('NO_COLOR') || `tput colors`.chomp.to_i < 8 unless defined?(NO_COLOR)
unless defined?(ANSI_COLORS)
ANSI_COLORS = {
red: 31,
@brand-it
brand-it / have_scope.rb
Created April 7, 2023 17:08
A simple tool to make it easier to test scopes on models using rspec
# frozen_string_literal: true
# A quick and easy way to test scopes sql queries by checking only the sql output
#
# is_expected.to have_scope(:user, 1).include('users.id = 1')
# is_expected.to have_scope(:user, 1).match_array([user])
# is_expected.to have_scope(:user, 1).eq([user])
module RSpec
module Matchers
class HaveScope
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'uri'
require 'json'
require 'net/http'
require 'optparse'
require 'forwardable'
COMMAND_NAME = File.basename(__FILE__)
class SnowflakeId < ApplicationService
MAC_ADDRESS_PATTERN = /(?:(?:(?:[a-f0-9]{2}:){5})|(?:(?:[a-f0-9]{2}-){5}))[a-f0-9]{2}/i
MAC_ADDRESS = lambda {
address = RUBY_PLATFORM =~ /darwin/ ? `ifconfig` : `cat /sys/class/net/*/address`
address.match(MAC_ADDRESS_PATTERN).to_s.delete(':')
}.call
NODE_ID = -> { (MAC_ADDRESS.to_i(16) % 1023).to_s(2).rjust(10, '0') }.call
SEQUENCE_STARTED_AT = 1671750375309
COUNTER_MAX = 4096
@brand-it
brand-it / install
Last active September 7, 2022 16:25
A simple tool to run rubocop on files you have changed
echo "Creating $(git rev-parse --git-dir)/hooks/pre-commit.d"
mkdir $(git rev-parse --git-dir)/hooks/pre-commit.d
echo "Downloading rubocop hook into $(git rev-parse --git-dir)/hooks/pre-commit.d/rubocop"
wget -O $(git rev-parse --git-dir)/hooks/pre-commit.d/rubocop https://gist.githubusercontent.com/brand-it/93d04c839238283e3e756811b9c9cee0/raw/616bf812b58bcefc52d564ed8abacbf30966193c/pre-commit.d_rubocop
echo "Changing rubocop into a executable $(git rev-parse --git-dir)/hooks/pre-commit.d/rubocop"
chmod 755 $(git rev-parse --git-dir)/hooks/pre-commit.d/rubocop
echo "adding hooks_directory variable to $(git rev-parse --git-dir)/hooks/pre-commit if it does not already exist"
grep -qxF 'hooks_directory=$(git rev-parse --git-dir)/hooks/pre-commit.d' $(git rev-parse --git-dir)/hooks/pre-commit || echo 'hooks_directory=$(git rev-parse --git-dir)/hooks/pre-commit.d' >> $(git rev-parse --git-dir)/hooks/pre-commit
echo "adding $hooks_directory/rubocop variable to $(git rev-parse --git-dir)/hooks/pre-commit if it do
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'net/http'
require 'open-uri'
require 'tempfile'
require File.expand_path('ruby_bash', __dir__).to_s
options = {}
def format_as_table(header, data)
total_columns = data.first.size
data.insert(0, header) if header
column_widths = total_columns.times.map { |col| data.map { |row| row[col].size }.max }
if header
data.insert(1, Array.new(total_columns, '-').zip(column_widths).map { |a, b| a * b })
end
data.map do |row|
row.zip(column_widths).map do |cell, width|
cell.to_s.ljust(width)
@brand-it
brand-it / default_ruby_version.txt
Last active April 5, 2022 00:10
I use this to full my Alfred Workflows
3.1
@brand-it
brand-it / simple_gql_ruby.rb
Last active March 24, 2022 21:36
Simple tool for making GQL request with vanilla ruby
require 'uri'
require 'json'
require 'net/http'
class Gql
class Base
Response = Struct.new(:http) do
def success?
http.is_a?(Net::HTTPSuccess)
@brand-it
brand-it / pretty_string.rb
Created March 12, 2022 00:52
I don't know who created this origionaly I but I know some one did. I did some googling to find the original but could not.
# PrettyString.new('something').blue
# PrettyString.new('something').red
# PrettyString.new('something').green
# PrettyString.new('something').yellow
# PrettyString.new('something').magenta
# PrettyString.new('something').white
class PrettyString < String
# https://no-color.org/
NO_COLOR = ENV.key?('NO_COLOR') || `tput colors`.chomp.to_i < 8
ANSI_COLORS = {