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 / _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 = {}
@dux
dux / demo.bash
Last active November 4, 2018 17:10
CLI demo builder, useful for asciinema.org - demo > https://asciinema.org/a/IanaNAq9CDKvUg7dweqxzhoAV
# asciinema rec --overwrite tmp/demo.case
# bash demo.bash
type_and_run () {
printf '\033[01;37m@app $\033[0m '
for ((i=0; i<${#1}; i++)); do echo "after `jot -r 1 20 60`" | tclsh; printf "${1:$i:1}"; done;
echo;
$1;
echo;
}
# Defines class variable
def Object.class_attribute name, default=nil, &block
raise ArgumentError.new('name must be symbol') unless name.class == Symbol
ivar = "@cattr_#{name}"
instance_variable_set ivar, block || default
define_singleton_method(name) do |arg=:_undefined|
# define and set if argument given
@dux
dux / folder_model.rb
Last active October 28, 2018 16:24
Foder and Generic models
# frozen_string_literal: true
class FolderModel
class << self
def find(key)
new(key)
end
def all
@dux
dux / class_callbacks.rb
Last active October 29, 2018 14:14
Simple and effective class callbacks
# Rails style callbacks
# for controllers, execute from AppController to MainController
# class_callback :before
# before do
# ...
# end
# before :method_name
# instance = new
# Object.class_callback :before, instance
@dux
dux / _set_media_body_class.coffee
Last active October 28, 2018 16:29
Sets media classes to HTML body tag. Example for mobile <body class="mobile not-tablet not-desktop">
# call just after body tag or $ -> window.MediaBodyClass.init()
# sets body class for
# mobile to "mobile not-tablet not-desktop"
# tablet to "not-mobile tablet not-desktop"
# desktop to "not-mobile not-tablet desktop"
@MediaBodyClass =
sizes: [['mobile'], ['tablet', 767], ['desktop', 1023]]
init: ->
@dux
dux / example.rb
Last active June 10, 2018 09:35
Html menu builder helper
menu = HtmlMenu.new request.path
menu.add 'Home', '/'
menu.add 'People', '/people', lambda { |path| path.index('peor') }
menu.add 'Jobs', '/jbos'
menu.to_a -> [["Home", "/", false], ["People", "/people", true], ["Jobs", "/jbos", false]]
menu.to_h -> [{:name=>"Home", :path=>"/", :active=>true}, {:name=>"People", :path=>"/people", :active=>false}, {:name=>"Jobs", :path=>"/jbos", :active=>false}]
@dux
dux / create_read_only_user_postgres
Created March 1, 2018 14:57
Create read only user on Postgre DB
CREATE USER readonly WITH ENCRYPTED PASSWORD 'readonly';
GRANT USAGE ON SCHEMA public to readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;
-- repeat code below for each database:
GRANT CONNECT ON DATABASE foo to readonly;
\c foo
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO readonly; --- this grants privileges on new tables generated in new database "foo"
GRANT USAGE ON SCHEMA public to readonly;
@dux
dux / pub_sub.rb
Last active November 29, 2017 03:16
Postgres LISTEN and NOTIFY with payload example
require 'sequel'
require 'securerandom'
# DB = Sequel.postgres(...)
ch_name = :test_1
Thread.new do
DB.listen(ch_name, loop: true) do |channel, pid, payload|
p [channel, pid, payload]
@dux
dux / _tag_builder.rb
Last active January 14, 2019 11:55
HTML tag builder for ruby with many render options
# tag.ul do |n|
# 1.upto(3) do |num|
# n.li do |n|
# n.i 'arrow' # <i class="arrow"></i>
# n._arrow # <div class="arrow"></div>
# n.span 123 # <span>123</span>
# n.span { 123 } # <span>123</span>
# n.('foo') # <div class="foo"></div>
# n._foo(bar: baz) { 123 } # <div class="foo" bar="baz">123</div>
# end