Skip to content

Instantly share code, notes, and snippets.

mjwillson / merb_restful_rangeable_collections.rb
Created Mar 6, 2009
RESTful way of exposing a collection resource in merb in a pageable / sub-range-fetchable way. Supports HTTP Content-Range
View merb_restful_rangeable_collections.rb
# Drop me a line if you wanna see this as a proper merb plugin.
class Merb::Controller
ITEM_RANGE = /^items=(\d+)-(\d+)$/
RANGE = /^(\d+)-(\d+)$/
# Displays a collection resource (using Merb's display method) while supporting requests for sub-ranges of items in a RESTful fashion.
# This supports a subset of the HTTP/1.1 spec for content ranges, using a custom range unit 'items'. eg:
# GET /collection HTTP/1.1
# Range: items 10-20
mjwillson / yaml_fixtures.rb
Created Nov 20, 2008
Some general-purpose fixture-loading code which supports references and respects dependencies. Quite short as it hooks into yaml.rb to do most of the work
View yaml_fixtures.rb
# This is a mixin for Test::Unit::TestCase, for providing tests with a simple facility to load fixtures from
# YAML files. It aims to do as much as possible in a generic way with the YAML library rather than couple
# tightly to a database library.
# Fixtures can use arbitrary YAML to represent ruby objects, using !ruby/object:Some::ClassName where needed.
# you will typically need to implement yaml_initialize on the relevant class, in order for this to
# work in the way you desire (we do so here for Sequel::Model; others including ActiveRecord shouldn't be hard)
# Data within one fixture can refer to other fixtures by file and name, using the syntax: !fixture file/name.
# this is achieved by adding a special YAML domain type.
mjwillson / equivalence_relation.rb
Created Nov 13, 2008
Simple Ruby class for an extensionally-specified Equivalence Relation with fast lookup
View equivalence_relation.rb
require 'set'
class EquivalenceRelation
def initialize
@equivalence_class_by_member = {}
def declare_equivalent(*items)
combined_equivalence_class = {|item| equivalence_class_of(item)}.inject( {|accum,set| accum.union(set)}
combined_equivalence_class.each {|member| @equivalence_class_by_member[member] = combined_equivalence_class}
mjwillson /
Last active Aug 29, 2015
Decorate a generator function (or other iterator-returning function) as a multi-shot iterable. A fix for many Python gotchas relating to use of one-shot iterators
class iterable(object):
"""Decorates a generator function (or any other iterator-returning
function) as something which implements the iterable protocol and
can be safely passed to other code which may iterate over it
multiple times.
def foo():