Skip to content

Instantly share code, notes, and snippets.

View johnmcdowall's full-sized avatar
💭
😺

John McDowall johnmcdowall

💭
😺
View GitHub Profile
Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Security.Principal
Public Module DebuggingMacros
static int GenerateCryptographiclyStrongSeed() {
var randomBytes = new byte[4];
// Generate 4 random bytes.
var rng = new RNGCryptoServiceProvider();
rng.GetBytes(randomBytes);
// Convert 4 bytes into a 32-bit integer value.
return (randomBytes[0] & 0x7f) << 24 |
randomBytes[1] << 16 |
/// <summary>
/// Helper to avoid the tag soup that comes from rendering Lists of items.
/// Currently supports any number of item detail arguments.
/// <example>
/// Html.RenderList(Model.BoardPosts,
/// "<table><caption>Threads</caption><tbody><tr><th>Thread Name</th><th>Subject</th><th>Posts</th><th>Last Updated</th></tr>{0}</tbody></table>",
/// "<tr><td><a href=\"/Group/ShowForumThread/{3}/{4}\">{0}</a></td><td>{1}</td><td>{2}</td><td>{5}</td></tr>",
/// "<tr><td colspan=\"4\">There are no Threads in this forum yet.</td></tr>",
/// x =>x.Name,
/// x =>x.Subject,
@johnmcdowall
johnmcdowall / UkTrainTimesParse
Created November 27, 2010 15:43
Parses the UK National Rail Enquiries JSON data feed for live departures
echo "Trains from HYN to GLQ" && curl --silent \
--user-agent "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7" \
--referer "http://ojp.nationalrail.co.uk/en/s/ldbboard/dep/HYN/GLQ/To" \
"http://ojp.nationalrail.co.uk/en/s/ldb/liveTrainsJson?departing=true&liveTrainsFrom=Hyndland&liveTrainsTo=Glasgow+Queen+Street&serviceId=" \
| sed -e 's/[{"&;/"}]/''/g' | sed -e 's/ltbrgt/-''/g' | \
awk '{n=split($0,a,","); for (i=6; i<=n-2; i++) { printf("%s %-25s %-20s\n",a[i+0],a[i+1],a[i+2]); i+=5}}'
@johnmcdowall
johnmcdowall / setup-statsd.sh
Created June 13, 2011 21:17 — forked from collegeman/setup-statsd.sh
Turn an Ubuntu 10.04 Vagrant into a StatsD/Graphite server
cd ~
# download the Node source, compile and install it
git clone https://github.com/joyent/node.git
cd node
./configure
make
sudo make install
# install the Node package manager for later use
@johnmcdowall
johnmcdowall / gist:1086574
Created July 16, 2011 17:33
Gruber's Ultimate URL RegExp for Ruby
/(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i
See http://daringfireball.net/2010/07/improved_regex_for_matching_urls
Be sure to add:
# coding: utf-8
At the very top of your Ruby file where you use the above expression.

Proposal for Improving Mass Assignment

For a while, I have felt that the following is the correct way to improve the mass assignment problem without increasing the burden on new users. Now that the problem with the Rails default has been brought up again, it's a good time to revisit it.

Sign Allowed Fields

When creating a form with form_for, include a signed token including all of the fields that were created at form creation time. Only these fields are allowed.

To allow new known fields to be added via JS, we could add:

def r(this)
require this
puts "#{this} is now loaded."
rescue LoadError
puts "The gem '#{this}' is missing."
puts "Should I install it? [y/n]"
if gets =~ /yes|y/i
puts "Installing #{this}, hold on."
if `gem install #{this}` =~ /Successfully/
Gem.clear_paths
@johnmcdowall
johnmcdowall / gist:3139498
Created July 18, 2012 22:50
Generate a dynamic CSS class on a body tag with Haml
# Generate a dynamic HTML body tag for a passed Haml block. Uses the controller name and
# action name to generate a class with a dash between to two, e.g. home-index
#
# css_class - A CSS class name to use on the body tag. (Optional)
# block - The HAML content block with which to surround with a BODY tag.
#
# Example
#
# = dynamic_haml_body_tag do
# = yield
@johnmcdowall
johnmcdowall / gist:3362788
Created August 15, 2012 19:21
Paul Arden
http://www.paularden.com/editorials.html
Here are some of Paul Arden's other pieces of advice:
If people constantly reject your ideas or what you have
to offer, resign. You can't keep fighting and losing,
that makes you a problem. If you are good and right for
the job, your resignation will not be accepted. You'll
be re-signed, on your terms. If they accept your
resignation, you were in the wrong job, and it is better
for you to move on. It takes courage, but it is the right move.