Skip to content

Instantly share code, notes, and snippets.

View matthewd's full-sized avatar

Matthew Draper matthewd

View GitHub Profile
@matthewd
matthewd / db-switch.rb
Created April 1, 2021 05:50
bin/db-switch
#!/usr/bin/env ruby
require(Dir.pwd + "/config/environment")
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
ActiveRecord::Base.establish_connection(db_config.config)
context = ActiveRecord::Base.connection.migration_context
missing_migrations = []
class Range
def overlap(other)
[self.begin, other.begin].max .. [self.end, other.end].min
end
end
def musical_era_alive_in(start_life, end_life)
[
['Medieval', 476..1400],
['Renaissance', 1400..1600],
@matthewd
matthewd / -
Created February 18, 2016 21:22
commit 604fd2cb227d92bed1b738e71feb4ff5360f4491
Author: Matthew Draper <matthew@trebex.net>
Date: Fri Feb 19 07:50:42 2016 +1030
Use a semaphore to signal message availability
The Event hack was too much of a hack: on actually thinking about it,
there's a rather obvious race.
diff --git a/actioncable/test/client_test.rb b/actioncable/test/client_test.rb
@matthewd
matthewd / -
Created February 18, 2016 21:20
diff --git a/actioncable/test/client_test.rb b/actioncable/test/client_test.rb
index d30c381..2662921 100644
--- a/actioncable/test/client_test.rb
+++ b/actioncable/test/client_test.rb
@@ -54,7 +54,7 @@ def initialize(port)
@ws = Faye::WebSocket::Client.new("ws://127.0.0.1:#{port}/")
@messages = Queue.new
@closed = Concurrent::Event.new
- @has_messages = Concurrent::Event.new
+ @has_messages = Concurrent::Semaphore.new(0)
@matthewd
matthewd / -
Created January 22, 2016 21:10
diff --git a/lib/sprockets/manifest.rb b/lib/sprockets/manifest.rb
index e514ebb..9797575 100644
--- a/lib/sprockets/manifest.rb
+++ b/lib/sprockets/manifest.rb
@@ -165,6 +165,8 @@ def compile(*args)
concurrent_compressors = []
concurrent_writers = []
+ pool = Concurrent::FixedThreadPool.new(5)
+
@matthewd
matthewd / fallout.rb
Last active April 4, 2019 00:35 — forked from tenderlove/fallout.rb
hack fallout terminals
##
# Program to help you hack terminals in Fallout
#
# Usage:
#
# Run this program with a '/' separated list of possible words, and
# (once you've made a guess) another '/' separated list of words and
# scores where the word and score are separated by a ':'
#
# In this example, the words shown in my terminal were:
diff --git a/spec/helpers/application_helper/toolbar_builder_spec.rb b/spec/helpers/application_helper/toolbar_builder_spec.rb
index 546ce2b..81ce5f3 100644
--- a/spec/helpers/application_helper/toolbar_builder_spec.rb
+++ b/spec/helpers/application_helper/toolbar_builder_spec.rb
@@ -3,11 +3,14 @@
describe ApplicationHelper do
before do
controller.send(:extend, ApplicationHelper)
- self.class.send(:include, ApplicationHelper)
end
diff --git a/vmdb/lib/acts_as_ar_model.rb b/vmdb/lib/acts_as_ar_model.rb
index e23a20f..d6838a8 100644
--- a/vmdb/lib/acts_as_ar_model.rb
+++ b/vmdb/lib/acts_as_ar_model.rb
@@ -1,16 +1,3 @@
-class ActsAsArModelColumn < ActiveRecord::ConnectionAdapters::Column
- attr_reader :options
-
- def initialize(name, options)
- type = options.kind_of?(Symbol) ? options : options[:type]
@matthewd
matthewd / post-checkout.sh
Created June 14, 2014 07:16
Per-branch Gemfile.lock
#!/bin/bash
if [ "$3" = 1 ]; then
old_branch="`git name-rev --name-only --no-undefined "$1" | cut -d'~' -f1`"
new_branch="`git name-rev --name-only --no-undefined "$2" | cut -d'~' -f1`"
if [ "$old_branch" != "$new_branch" ]; then
mv -f "Gemfile.lock" ".Gemfile/lock.$old_branch"
if [ -f ".Gemfile/lock.$new_branch" ]; then
mv -f ".Gemfile/lock.$new_branch" "Gemfile.lock"
@matthewd
matthewd / gist:b74b0de5244b2a95952b
Last active August 29, 2015 14:00
gsub forwarding with $1
class X < String
def evil_call(meth, args, block, &wrap_block)
block.binding.eval(<<-END, __FILE__, __LINE__+1).call(to_str, meth, args, wrap_block)
lambda do |str, meth, args, block|
str.send(meth, *args, &block)
end
END
end
def gsub(*args, &block)