Skip to content

Instantly share code, notes, and snippets.

View ismasan's full-sized avatar

Ismael Celis ismasan

View GitHub Profile
# Turn a line-based Enumerator into an Enumerator
# that parses each CSV line and yields them as Hash rows
# Usage
# csv = CSVStream.new(line_stream, headers: { product_name: 0, price: 2 })
#
# With a bit more work it could detect CSV headers automatically, from the first line.
require 'csv'
class CSVStream
include Enumerable
@ismasan
ismasan / bootic_cart.js
Created September 5, 2012 21:50
Generic JS shopping cart jQuery pluging
///////////////////////////////////////////////////////
// Generic JS shopping cart - (c) Bootic.net
// Written by Ismael Celis @ismasan
// Free for use to any online shop running on Bootic
///////////////////////////////////////////////////////
;(function ($) {
$.fn.booticAddToCart = function (fn) {
fn = fn || $.noop;
$(this).submit(function () {
puts "START"
chan = Channel(Nil).new
spawn do
sleep 6
chan.send nil
end
select
when chan.receive
puts "CHAN"
# simple railway-style pipelines for Crystal
class Success(T)
def initialize(@value : T)
end
def then(callable : T -> Success(T) | Failure(T))
callable.call(@value)
end
@ismasan
ismasan / css_grid_draggable.html
Last active April 27, 2022 02:07
Example for reordering CSS grids layout
<html>
<head>
<title>grid</title>
<style>
body {padding: 0; margin: 0;}
.container {
display: grid;
grid-template-rows: 200px repeat(4, 100px);
grid-template-columns: repeat(4, 1fr);
grid-template-areas: "header header header header"

As I see it, there's a few options, each with its own tradeoffs.

1. Compute heavy operation on write

Run the expensive query/calculation when you write relevant data, synchronously, and store it in a way that's optimized for fast reads (ie. in a cache somewhere, or just a JSON blob in the database).

PROS:

  • optimized for fast reads, data is kept up to date if you make sure to compute it in every write operation that might affect the outcome.
@ismasan
ismasan / purge.vcl
Created January 27, 2011 18:30
Varnish VCL example for purging wildcard URLs
# This goes in vcl_recv
# It gives you:
# curl -X PURGE http://some.example.com/.*
# curl -X PURGE http://some.example.com/blog/.*
# curl -X PURGE http://some.example.com/blog/2011/bar.html
# curl -X PURGE http://another.example.com/.*
#
if (req.request == "PURGE") {
# Wildcard, per-domain purging
purge("req.http.host == " req.http.host " && req.url ~ " req.url "$");
# frozen_string_literal: true
require 'faraday'
require 'parametric/types'
require 'digest'
include Parametric
# A quick helper to benchmark function calls
def bench(&block)
# https://vimeo.com/113707214
class Result
attr_reader :value
def initialize(v)
@value = v
end
class << self
def success(v)
# frozen_string_literal: true
class Worker
def self.wrap(callable)
if callable.respond_to?(:setup)
callable
elsif callable.respond_to?(:call)
new(callable)
else
raise ArgumentError, "worker must implement #setup, #run, #shutdown"