Skip to content

Instantly share code, notes, and snippets.

@paulcc
paulcc / sketch.rb
Created June 29, 2012 16:04 — forked from mattwynne/sketch.rb
pseudo-haskell
class Valid a where
valid :: a -> OkF Errors
class Valid a => Persist m a where
save :: a -> m (OkF a) -- or errors
data User = User {name :: String}
data Organisation = Organisation {name :: String, users :: [User]}
@paulcc
paulcc / gist:907556
Created April 7, 2011 10:57
another block exercise
class Configuration
attr_accessor :tail_logs, :max_connections, :admin_password
class AppServerConfig
attr_accessor :port, :admin_password
end
def app_server
yield(@app_server ||= AppServerConfig.new) if block_given?
@app_server
@paulcc
paulcc / gist:907466
Created April 7, 2011 10:01
scottish ruby conf ex - counter
def counter(s = 0, i = 1)
c = s
lambda { o = c; c += i; o }
end
x = counter(10,2)
4.times { puts x.call }
y = counter(2,3)
6.times { puts y.call }
@paulcc
paulcc / meta_search_hacks.rb
Created December 6, 2010 11:31
Import this to get searchlogic-style scopes from metasearch and rails 3
# by Paul Callaghan, Nov 2010.
# this uses method-missing to trigger generation and execution of the corresponding relation
# eg Product.name_equals("yay")
# TODO: M_S doesn't check for unexpected values, eg match failures in
# matches_attribute_method (so we trap exceptions)
module MetaSearchHacks
def self.build_rel(base_or_scope, method, *args)
require "csv"
require 'action_controller/test_process'
file = `ls upload/*.csv | grep -v and-r`.chomp
info = CSV.read(ARGV.first || file)
info.shift
# TODO: set this list by eliminating the expected cols
properties = %w[Colour Material Size Weight Warranty] + ["Pole Diameter", "Fits Pole Size"]