Skip to content

Instantly share code, notes, and snippets.

View dux's full-sized avatar

Dino Reić dux

  • Trifolium
  • London, Zagreb, Berlin
View GitHub Profile
@dux
dux / app.rb
Last active April 4, 2024 08:01
Sinatra/puma optimized ruby image resizer
#!/usr/bin/ruby
require 'sinatra'
require 'digest'
ROOT = Dir.getwd
error = nil
for dir in ['cache','cache/originals', 'cache/resized', 'cache/pages', 'cache/croped']
dir = "#{ROOT}/#{dir}"
@dux
dux / decorator_clone.rb
Last active October 20, 2023 13:51
Simple ruby decorators with example
class DecoratorClone
def initialize(model)
@model = model
end
def method_missing(m, *args)
raise NameError, "Decorator method '#{m}' not found" unless @model.respond_to?(m)
@model.send(m, *args)
end
end
@dux
dux / paginate.rb
Last active January 6, 2023 18:21
Trivial and fast (does not execute count or any other unneeded requests) Ruby / Rails / Sinatra / Lux pagination
# @list = Paginate.set User, size: 40, name: :upage
# Paginate.render @list
# using: https://github.com/dux/html-tag/
module Paginate
extend self
def set rset, opts={}
opts[:size] ||= 20
opts[:name] ||= :page
@dux
dux / _
Last active January 6, 2023 18:20
Capistrano replacement via common rake tasks
you will need
* hash_wia gem https://github.com/dux/hash_wia
* https://github.com/amazing-print/amazing_print
# to fully deploy application
rake remote:deploy
@dux
dux / _problem.md
Last active January 6, 2023 18:18
Ruby Sequel switch databases on the fly
  1. I wanted to keep global user data in one database and org data in separate database
  2. I want to use Sequel models in the same way as I was using a single databases
  3. I need simple interface to define that model data resides in Organization and not Global DB

Only possible solution I found is by patching Sequel native methods. If anyone knows better way to do it, like injecting DB connection just before dataset executes a query, please comment and explain.

I tried to use "sharding" and "arbitrary_servers", but that is not what I want. I have 1000nds of orgs, and 1000nds of DBs. I need connections to be fuly dynamic and I want to define request context once when request starts in Thread.current.

@dux
dux / pull-request-prepare.rb
Last active March 2, 2022 19:06
prepares github pull request with full clean view of the selected files
#!/usr/bin/env ruby
# prepares github pull request with full clean view of the selected files
# to run it
# * delete some files after commit that is pushed, and leave them unstaged
# * pull-request-prepare.rb will
# * create branch-review-start with all marked files deleted
# * create branch-review-end with all marked files re-added
# * leave starting branch untouched
# * open github url for pull request
@dux
dux / Caddyfile
Last active February 15, 2022 17:22
linux services systemd
# https://caddy.community/t/serving-tens-of-thousands-of-domains-over-https-with-caddy/11179
{
admin off
# on_demand_tls {
# ask http://localhost:3000
# interval 2m
# burst 5
# }
@dux
dux / custom_element.js
Last active February 1, 2022 02:11
Creates custom DOM element and passes props. Bare bones custom nodes
// https://gist.github.com/dux/89c59570228d745128aafef98164de1f
// micro custom dom elements, no shadow dom
// exposes props: root, props and state
window.CustomElement = {
// we need to find custom node in exact name
// CustomElement.find(domNode, 'foo-bar')
find: (node, uid) => {
while(node) {
@dux
dux / haversine.sql
Created January 21, 2022 17:26 — forked from carlzulauf/haversine.sql
PostgreSQL function for haversine distance calculation, in miles
-- Haversine Formula based geodistance in miles (constant is diameter of Earth in miles)
-- Based on a similar PostgreSQL function found here: https://gist.github.com/831833
-- Updated to use distance formulas found here: http://www.codecodex.com/wiki/Calculate_distance_between_two_points_on_a_globe
CREATE OR REPLACE FUNCTION public.geodistance(alat double precision, alng double precision, blat double precision, blng double precision)
RETURNS double precision AS
$BODY$
SELECT asin(
sqrt(
sin(radians($3-$1)/2)^2 +
sin(radians($4-$2)/2)^2 *
@dux
dux / _threaded.rb
Last active July 31, 2021 22:02
Simple threaded/promisse-like runner for Ruby
class Thread::Simple
attr_accessor :que, :size, :named
def initialize size: 5, sleep: 0.01
@sync = Mutex.new
@sleep = sleep
@size = size
@que = []
@threds = []
@named = {}