Skip to content

Instantly share code, notes, and snippets.

def consolidate(*issue_uris)
result = Nokogiri::XML.parse '<issues />'
issue_uris.each do |uri|
fetch_doc(uri).css('issue').each do |issue|
if issue.comments.content.to_i > 0 then
issue.comments.unlink
comments_doc = fetch_doc("#{@comments_base}/#{issue.number.content}")
comments_doc.comments.parent = issue
end
@rgarner
rgarner / gist:856304
Created March 5, 2011 11:35
dm-many-to-many weirdness
class Calendar
include DataMapper::Resource
has n, :events
property :id, Serial
property :slug, Slug
property :title, String
property :published, Boolean
property :created_at, DateTime
end
@rgarner
rgarner / rss_aware_atom_feed_helper.rb
Created June 14, 2011 08:02
RSS-aware ATOM feed helper
##
# RSS-aware patched helper. See http://www.zephyros-systems.co.uk/blog/?p=179
#
require 'set'
module ActionView
# = Action View Atom Feed Helpers
module Helpers #:nodoc:
module AtomFeedHelper
# Adds easy defaults to writing Atom feeds with the Builder template engine (this does not work on ERb or any other
@rgarner
rgarner / gist:1108931
Created July 27, 2011 08:36
Rubymine .iml conflict-causing behaviour
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
- <orderEntry type="library" scope="PROVIDED" name="rack (v1.2.3, Ruby SDK 1.9.2) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="riddle (v1.2.2, Ruby SDK 1.9.2) [gem]" level="application" />
+ <orderEntry type="library" scope="PROVIDED" name="rack (v1.2.3, Ruby SDK 1.9.2) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="polyglot (v0.3.1, Ruby SDK 1.9.2) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="activesupport (v3.0.3, Ruby SDK 1.9.2) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.1.2, Ruby SDK 1.9.2) [gem]" level="application" />
@@ -137,15 +137,15 @@
<orderEntry type="library" scope="PROVIDED" name="i18n (v0.6.0, Ruby SDK 1.9.2) [gem]" level="application" />
@rgarner
rgarner / gist:1108964
Created July 27, 2011 09:00
Aliasing HTML to XHTML by default
module Mime
remove_const('HTML')
XHTML = Mime::Type.new "application/xhtml+xml", :xhtml
Mime::Type.register_alias "application/xhtml+xml", :html
LOOKUP['text/html'] = LOOKUP[:html] = XHTML
LOOKUP['application/xhtml+xml'] = LOOKUP[:xhtml] = XHTML
end
@rgarner
rgarner / test_unit.rb
Created October 27, 2011 07:41
Test::Unit example
require "test/unit"
class ObjectiveFailure
def valid?
false
end
def children
['ted']
end
@rgarner
rgarner / my_spec.rb
Created October 27, 2011 07:41
RSpec example
require 'spec_helper'
class ObjectiveFailure
def valid?
false
end
def children
['ted']
end
@rgarner
rgarner / ico.js.coffee
Created June 26, 2012 09:02
Self-evident vs. Script-y code
#
# This is a little script to pop up an #ico cookie consent banner with a clickable #ico-ok element in it.
# I wrote the "self-evident" version first. I expanded it out to the script-y version and added comments.
#
# Questions:
#
# * Which is easier to read?
# * Which is easier to maintain?
# * Why?
@rgarner
rgarner / gist:4748962
Created February 10, 2013 09:26
BBC, fact-checking, and C#

In http://www.bbc.co.uk/news/technology-21371609 you say "Most [hacks] were written using basic coding languages such as Visual Basic and C#."

What makes a language "basic"? Obviously Visual Basic is literally a BASIC derivative, but every variation of Visual Basic is at least Turing-complete. C#, however, has native syntactical support for concepts such as lambda calculus and asynchronous behaviour. It is in use in many thousands of development shops. Some of your own software is written in it. Your apparent implication by inclusion in a story about child hackers is that it is a toy language. Had you talked to any development staff about it (possibly even your own!) even briefly you might have discovered that this is not the case.

I would be interested to know on what basis C# has earned its status in your eyes as a "basic" language.

@rgarner
rgarner / bluri.rb
Created July 24, 2013 11:23
BLURI - URI canonicalisation for Business Link, as was. Has some BL-specific bits, but otherwise good for querystring-as-hash sorting/deleting. Has own spec. One careful owner. Requires `addressable` gem, because, well, `URI` is a bit broken.
require 'addressable/uri'
module URI
##
# Extends a hash with query string reordering/deleting capabilities
module QueryHash
def ordered_query_string(*args)
unmentioned_keys = keys.reject { |key| args.include?(key.to_s) || args.include?(key.to_sym) }
(
args.uniq.collect { |key| render_value(key, self[key]) }.reject { |i| i.nil? } +