Skip to content

Instantly share code, notes, and snippets.

View johncarney's full-sized avatar

John Carney johncarney

View GitHub Profile

The arel_extensions gem has a couple of bugs...

  1. DateSub doesn't work:
(table[:column] - 10.hours).to_sql
# => NoMethodError: undefined method `to_sql' for nil:NilClass
  1. For MySQL, DateAdd generates a double-minus when given a negative duration:
@johncarney
johncarney / yaml-with-lines.rb
Last active June 8, 2022 13:15
A Psych-based YAML parser that captures line numbers
#!/usr/bin/env ruby
# Custom Psych parser that captures line number information from a YAML file.
#
# For a project I'm working on I need to be able to determine which line(s) in a YAML
# file a particular value comes from. There are a few bits of advice on the internet
# about this, the best of them that I've found involves monkey-patching, which is a
# fairly low bar for "best" in my opinion. I found it on Stack Overflow:
#
# https://stackoverflow.com/questions/29462856/loading-yaml-with-line-number-for-each-key
@johncarney
johncarney / null_order_arel_extension.rb
Last active November 9, 2017 18:50
Possible null ordering solutions
# Examples
#
# MyModel.order(MyModel.arel_table[:name].asc.nulls_last)
# MyModel.order(MyModel.arel_table[:name].desc.nulls_first)
module Arel
module NullOrder
def nulls_last
Arel::Nodes::NullsLast.new self
end
@johncarney
johncarney / rails-locales-conflicts.rb
Last active January 3, 2017 02:58
Lists conflicting translations from your Rails locale files. Note that it will only detect conflicts between different locale files. Conflicts within a single locale file will not be detected.
#!/usr/bin/env ruby
# Usage: ruby rails-locales-conflicts.rb [<rails root>]
require "pathname"
require "yaml"
class Translations
def initialize
@translations = Hash.new do |h, k|
@johncarney
johncarney / prefix.rb
Created November 13, 2016 10:13
Find the longest common prefix of a bunch of arrays, or a bunch of strings
def common_array_prefix(*arrays)
arrays.reduce do |a, b|
index = a.zip(b).index { |x, y| x != y }
a[0...index]
end
end
def common_string_prefix(*strings)
common_array_prefix(*strings.map(&:chars)).join("")
end
@johncarney
johncarney / reek-to-markdown.rb
Created May 19, 2016 01:35
Convert output of reek to GitHub-flavoured Markdown
#!/usr/bin/env ruby
# Converts the output from reek to GitHub-flavoured Markdown.
#
# Usage:
# Basically pipe the output from reek through this script.
#
# reek --sort-by smelliness app lib | ruby reek-to-markdown.rb
def scan_line(line, pattern)
@johncarney
johncarney / _readme.md
Last active February 19, 2016 06:58
Calculating with functions
@johncarney
johncarney / flex_struct.rb
Last active August 29, 2015 14:24
FlexStruct: Wrapper around Ruby's Struct with a more flexible initializer.
module FlexStruct
def initialize(*args, **options)
super(*args, *options.values_at(*members[args.length..-1]))
end
def self.new(*args, &block)
Struct.new(*args, &block).include self
end
end
@johncarney
johncarney / keybase.md
Created January 9, 2015 06:12
Keybase

Keybase proof

I hereby claim:

  • I am johncarney on github.
  • I am johncarney (https://keybase.io/johncarney) on keybase.
  • I have a public key whose fingerprint is 1D0F 663E BE56 A61B 6A17 C152 2ECF 52DC 5C72 1574

To claim this, I am signing this object:

@johncarney
johncarney / newsyslog-for-your-project.conf
Last active March 1, 2024 22:14
Example newsyslog configuration for rotating Rails log files on Mac OS X.
# logfilename [owner:group] mode count size when flags [/pid_file] [sig_num]
/Users/your-username/path-your-rails-project/log/*.log your-username:staff 644 4 * $D0 GJ
# NOTES
#
# Place file in /etc/newsyslog.d
# '$D0' under 'when' tells newsyslog to rotate logs daily at midnight.
# Alternatively you could use '24' for 'when', which would specify "every 24 hours"
# '*' under 'size' specifies that logs should be rotated regardless of their size.
# 'G' under 'flags' tells newsyslog that the 'logfilename' is a pattern and it should rotate all log files matching the pattern.