Skip to content

Instantly share code, notes, and snippets.

@ravinggenius
Created December 13, 2013 01:18
Show Gist options
  • Save ravinggenius/7938515 to your computer and use it in GitHub Desktop.
Save ravinggenius/7938515 to your computer and use it in GitHub Desktop.
# USAGE
Foo = class (Struct.new(:name, :address)) {
compare_by(:name)
order_by(:address, :name)
}
HelperMacros = class {
compare_by = -> (accessors...) {
@.@.== = -> (other) {
accessors.all?(-> (message) {
@.send(message) == other.send(message)
})
}
}
order_by = -> (accessors...) {
@.@.<=> = -> (other) {
accessors.fold(0, -> (memo, message) {
if (memo.zero?()) {
result = @.send(message) <=> other.send(message)
result <=> memo
} else {
memo
}
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment