Skip to content

Instantly share code, notes, and snippets.

View sobstel's full-sized avatar

sobstel

  • 11:30 (UTC +02:00)
View GitHub Profile
@sobstel
sobstel / bulk_upsert.rb
Last active July 11, 2022 17:00
Bulk upsert (Rails/PostgreSQL)
class Data < ApplicationRecord
class << self
def bulk_upsert(values)
transaction do
values.uniq { |v| v[:key] }.each_slice(1000) do |values_slice|
connection.exec_query <<-SQL.squish
INSERT INTO #{table_name}(#{upsert_columns.join(', ')})
VALUES #{upsert_values_statement(values_slice)}
ON CONFLICT(key)
DO UPDATE SET
@sobstel
sobstel / manual_preloading.rb
Created March 29, 2018 18:36
Rails manual association preloading
# taken from https://mrbrdo.wordpress.com/2013/09/25/manually-preloading-associations-in-rails-using-custom-scopessql/
# collection association e.g. has_many
owners = People.all
association_name = :photos
owners.each do |owner|
records = Array(whatever_you_want)
@sobstel
sobstel / mapDuplicates.test.ts
Created April 4, 2021 10:02
Remeda mapDuplicates
import mapDuplicates from "./mapDuplicates";
describe("mapDuplicates", () => {
it("addresses duplicate surnames", () => {
const surnames = ["González", "González", "Martínez", "González"];
expect(
mapDuplicates(surnames, (name, index) => `${name} ${index}`)
).toEqual(["González 0", "González 1", "Martínez", "González 3"]);
});
@sobstel
sobstel / unmount_animation_example.js
Created March 8, 2021 09:57
react-native-animatable unmount animation with hooks
// https://github.com/oblador/react-native-animatable/issues/132#issuecomment-786870602
const MyComponent = (props) => {
const ref = React.useRef();
React.useEffect(() => {
ref?.current?.fadeIn();
return () => ref?.current?.fadeOut(); // as you know, this is the same as unmount ;)
}, [props.id]); // track some prop that changes
@sobstel
sobstel / nonblock_socket_connect.php
Last active January 21, 2021 17:55
Non-blocking socket connection
<?php
function nonblock_socket_connect($ip, $port)
{
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!socket_set_nonblock($socket)) {
socket_error($socket);
}
$connected = @socket_connect($socket, $ip, $port);
@sobstel
sobstel / deploy.yml
Created October 1, 2020 16:27
Deploy Ruby AWS lambda with github action
on:
push:
branches:
- "master"
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
@sobstel
sobstel / win10vm_apple_keyboard.txt
Last active November 2, 2020 09:51
win10vm_apple_keyboard
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetScrollLockState, AlwaysOff
;following section remaps alt-arrow and command-arrow
;keys to mimic OSX behaviour
#Up::SendInput {Lctrl down}{Home}{Lctrl up}
@sobstel
sobstel / nokogiri_install
Created January 3, 2017 17:30
nokogiri -> ERROR: cannot discover where libxml2 is located on your system
# `ERROR: Error installing nokogiri:
# ERROR: Failed to build gem native extension.
#
# current directory: /usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/nokogiri-1.7.0/ext/nokogiri
# /usr/local/var/rbenv/versions/2.3.1/bin/ruby -r ./siteconf20170103-68488-r71c9j.rb extconf.rb --with-xml=/usr/local/Cellar/libxml2/ --use-system-libraries
# checking if the C compiler accepts ... yes
# checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
# Building nokogiri using system libraries.
# ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.
# *** extconf.rb failed ***
@sobstel
sobstel / page-speed-report.md
Last active May 7, 2020 11:30
Google page speed report 2018

Google page speed report 2018

  • Time spent on mobile vs desktop 
    • 2015 -> Google started getting more search queries from mobile than desktop
    • 2018 -> Time spent on mobile 2-3x bigger than on desktop (and growing...)
  • Users reaction
    • 46% dislike waiting for slow pages to load 
    • 53% abandon a site that loads in more than 3 seconds
  • As of July 2018, page speed will become an important ranking factor for mobile searches 
  • Page speed will also influence ad rank and the CPC that you pay 
@sobstel
sobstel / preg_replace_callback_match_counter.php
Created April 10, 2013 21:21
preg_replace_callback with match counter
<?php
$counter = 0;
$result = preg_replace_callback(
'#\?#',
function ($match) use (&$counter) {
$counter += 1;
return $counter;
},
'a: ?, b: ?, c: ?, d: ?. e: ?'