Skip to content

Instantly share code, notes, and snippets.

@zdennis
zdennis / browser_logging.rb
Created June 26, 2018 19:11
Chromedriver browser logging for Capybara
# browser_logging.rb will print out the browser log from Chrome
# when running specs with "js: true". This is so we can easily debug
# issues where there are JS errors or other console warnings/error(s), etc.
#
# Output file is: Rails.root/log/browser.log
#
# Note: Nothing will be printed out for non-JS examples.
RSpec.configure do |config|
browser_log = File.new(Rails.root.join("log/browser.log").to_s, "w")
browser_log.sync = true
@zdennis
zdennis / Rakefile
Created March 17, 2014 20:50
Add a Rakefile for generating a bridgesupport file whenever the project is built in XCode. This is for RubyMotion when building out a third-party library.
DIR = File.dirname(__FILE__)
BRIDGESUPPORT_FILE = "BridgeSupport.bridgesupport"
namespace :gen do
desc "Generates the bridgesupport file for RubyMotion"
task :bridgesupport do
Dir.chdir(DIR) do
SHELL_COMMAND=<<-EOS.gsub(/^\s*/, '')
@zdennis
zdennis / print-top-5-contributors-per-file.rb
Last active January 25, 2022 22:36
Git top contributors by file
#!/usr/bin/env ruby
#
# Prints out top 5 contributors per file (as determined by # of commits) on every file in the repository
#
files = `git ls-files`.lines.map(&:chomp)
stats_by_file = {}
files.each_with_index do |file, index|
@zdennis
zdennis / olive-branch-transformations-snippet.rb
Created December 1, 2017 20:30
OliveBranch transformation snippet for blog post
camelCasedPayload = OliveBranch::Transformations.transform(
{ key.to_sym => payload },
OliveBranch::Transformations.method(:camelize)
)
@zdennis
zdennis / struct_that_disallows_nil_values.rb
Created March 31, 2021 21:20
Struct That Disallows nil
module StructThatDisallowsNil
def self.new(*args)
Struct.new(*args).tap do |klass|
klass.prepend Module.new {
def initialize(*)
super
members.each do |member|
fail "#{member} cannot be nil" if send(member).nil?
end
end
DisableSettingInverse.disabled = true
child = Child.new(parent_id: 12345)
parent = Parent.new
parent.children = [child]
child.parent_id # => 12345
DisableSettingInverse.disabled = false
@zdennis
zdennis / foo.sh
Created April 8, 2020 15:53
Bash: checking shell errexit shell option inside function, disabling it, and then restoring it.
#!/usr/bin/env bash
# Show how to check, save, restore the errexit shell option that is commonly set
# using "set -e". This is useful for functions that care about non-zero exit statuses
# and want to do something because of it such as a retry function.
set -eu
echo "Shell options: $-"
@zdennis
zdennis / rails-current
Created April 7, 2020 17:19
script/rails-current and script/rails-next dual booting command runners
#!/usr/bin/env ruby
require "optparse"
SCRIPT_NAME = File.basename(__FILE__)
ARGS_TO_FORWARD = ARGV.dup
ARGV.delete_if { |arg| arg !~ /-h|--help/ }
OptionParser.new do |opts|
@zdennis
zdennis / config.rb
Created July 3, 2012 23:49
Make Middleman compile coffee-script using bare mode.
#
# Add the following to Middleman's config.rb:
#
CoffeeScript.class_eval do
class << self
def compile_with_bare(*args)
options = args.pop if args.last.is_a?(Hash)
(options ||= {})[:bare] = true
args << options
@zdennis
zdennis / unmerged-commits
Created February 17, 2020 17:12
look for unmerged commits for a rails5/integration branch
#!/usr/bin/env bash
# A simple front-end for running git unmerged in a project-specific way.
#
# ## Options
#
# -l for local comparing against local branches. The default is to compare against remote branches.
#
INCLUDE_BRANCH_CONVENTION="rails5/"