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 / 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
@dux
dux / profile.bash
Last active October 27, 2017 01:18
my osx ~/.profile
alias ls="command ls -G"
alias cp="cp -iv"
alias mv="mv -iv"
alias mkdir="mkdir -pv"
alias less="less -FSRXc"
mexe() { touch $1 && chmod +x $1 && subl $1; } # make executable and edit
ffl() { find . -name "*$@*"; } # find file local
fff() { mdfind -name $1; } # find file global
ff() { find . -type d -name "*$@*"; } # find folder
@dux
dux / demo.rb
Last active November 10, 2017 11:00
Simple thread poll
require 'colorize'
# require 'thread/pool'
# pool = Thread.pool(3)
pool = ThreadPool.new 3
1.upto(10).to_a.each { |num|
pool.process do
sleep_for = (rand()*10).to_i
text = "#{num} sleep #{sleep_for}"
@dux
dux / hash_with_indifferent_access.rb
Created October 8, 2017 10:19
HashWithIndifferentAccess plain ruby object, no Rails needed
# access hash keys with strings or symbols
# h = HashWithIndifferentAccess.new a: 1
# h[:a] -> 1
# h['a'] -> 1
class HashWithIndifferentAccess
def initialize data=nil
@data = {}
merge! data if data
end
@dux
dux / _remote.rb
Last active August 9, 2017 14:18
simple remote control of single production vps server
#!/usr/bin/env ruby
require 'thor'
require 'dotenv'
require 'awesome_print'
require 'colorize'
Dotenv.load
def Thor.define name, desc, &block
@dux
dux / copy_text_to_clipboard.js
Last active July 13, 2017 11:18
Copy ant text to clipboard in any modern browser
window.copyToClipboard = function(text) {
// IE specific
if (window.clipboardData && window.clipboardData.setData) {
return clipboardData.setData("Text", text);
}
// all other modern
target = document.createElement("textarea");
target.style.position = "absolute";
target.style.left = "-9999px";
@dux
dux / gemify.rb
Created April 24, 2017 23:26
checks, builds, installes localy, publishes rubygems
#!/usr/bin/env ruby
require 'rubygems'
require 'colorize'
require 'awesome_print'
require 'slop'
require 'irb'
require 'yaml'
module Sys
@dux
dux / class_attributes.rb
Last active November 13, 2017 23:10
define class attributes the functional way
# frozen_string_literal: true
# ClassAttributes.define klass, :layout, 'default_value_optional'
# klass.layout -> get value
# klass.layout = value -> set value
# class A
# ClassAttributes.define self, :layout, 'default'
# class_attribute :layout, 'default'
# end