Skip to content

Instantly share code, notes, and snippets.

View stevenharman's full-sized avatar

Steven Harman stevenharman

View GitHub Profile
@stevenharman
stevenharman / hash_merge_block.rb
Created January 30, 2014 22:14
Ruby's Hash#merge with an optional block to resolve key conflicts.
# via @avdi's RubyTapas Episode 170: http://www.rubytapas.com/episodes/170-Hash-Merge
headers = <<END
Accept: */*
Set-Cookie: foo=42
Set-Cookie: bar=23
END
def parse_headers(headers)
headers.lines.reduce({}) { |result, line|
@stevenharman
stevenharman / bootstrap.sh
Created July 25, 2014 18:50
A bin/bootstrap script to install and configure Dnsmasq as a local DNS server for *.dev TLDs.
#!/bin/sh
brew_install_if_needed() {
package=$1
brew info $package | grep "Not installed" > /dev/null 2>&1
if [[ "$?" -eq "0" ]]; then
brew install $package
fi
}
@stevenharman
stevenharman / nerd.rb
Last active August 29, 2015 14:04
Implementing the null object pattern on an ActiveRecord association. This is really a special case being represented with an object which acts like a null object.
class Nerd < ActiveRecord::Base
belongs_to :team, inverse_of: :nerds
def team
super || NullTeam.new
end
end
@stevenharman
stevenharman / dig-rustbyexample.log
Created December 13, 2014 15:33
rustbyexample.com DNS records, via Dig.
☺ $ dig rustbyexample.com +nostats +nocomments +nocmd
; <<>> DiG 9.8.3-P1 <<>> rustbyexample.com +nostats +nocomments +nocmd
;; global options: +cmd
;rustbyexample.com. IN A
rustbyexample.com. 3142 IN A 103.245.222.133
@stevenharman
stevenharman / same-videos.sh
Created July 6, 2015 23:59
Compare a directory of large binary files (1080p HD video, in this case) to another set of those files to make sure they're byte-for-byte identical.
# Back story: I uploaded a dozen or so HD (1080p) movies to my Synology while across the country.
# Now I want to make sure the uploads all came through without corruption.
for video in *.mp4; do
cmp "./${video}" "/Volumes/video/Movies/${video}"
done
using System.Reflection;
using FluentNHibernate.AutoMap;
using FluentNHibernate.Framework;
using MyApplication.Core.Domain;
namespace MyApplication.Core.Persistence
{
public class MyCustomPersistenceModel : AutoPersistenceModel
{
public MyCustomPersistenceModel()
using System.Reflection;
using FluentNHibernate.AutoMap;
using FluentNHibernate.Framework;
using MyApplication.Core.Domain;
namespace MyApplication.Core.Persistence
{
public class MyCustomPersistenceModel : AutoPersistenceModel
{
public MyCustomPersistenceModel()
using System;
using System.Reflection;
using FluentNHibernate.AutoMap;
using FluentNHibernate.Framework;
using MyApplication.Core.Domain;
namespace MyApplication.Core.Persistence
{
public class MyApplicationPersistenceModel : AutoPersistenceModel
{
namespace MyApplication.UnitTests.Core.Persistence
{
[Category("MyApplicationPersistenceModel")]
public class when_using_the_plan_my_night_persistence_model
: Specification, with_auto_persistence_model
{
private MyApplicationPersistenceModel _model;
public override void establish_context()
{
public class FindGoodStuffQuery : INamedQuery<Idea>
{
public IQueryable<Stuff> Apply(IQueryable<Stuff> queryable)
{
return queryable
.OrderByDescending(s => s.Widgets.Count())
.ThenByDescending(s => s.CreatedAt);
}
}