Skip to content

Instantly share code, notes, and snippets.

@calopez
calopez / async.rb
Created September 14, 2022 17:52 — forked from paul/async.rb
Implementations of useful step adapters for dry-transaction
# frozen_string_literal: true
module Tesseract
module Transaction
module Steps
# Executes the step in a background job. Argument is either an ActiveJob
# or another Transaction (or anything that implements `#perform_later`.
#
# If the provided transaction implements a `validate` step, then that
# validator will be called on the input before the job is enqueued. This
@calopez
calopez / rails-jsonb-queries
Created December 6, 2018 17:50 — forked from mankind/rails-jsonb-queries
Rails-5 postgresql-9.6 jsonb queries
http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query
http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails
#payload: [{"kind"=>"person"}]
Segment.where("payload @> ?", [{kind: "person"}].to_json)
#data: {"interest"=>["music", "movies", "programming"]}
Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json)
Segment.where("data #>> '{interest, 1}' = 'movies' ")
Segment.where("jsonb_array_length(data->'interest') > 1")

Keybase proof

I hereby claim:

  • I am calopez on github.
  • I am karloslopez (https://keybase.io/karloslopez) on keybase.
  • I have a public key ASAgUhfQW56OTgbSiCtz4tBVkUyGxO2ErCmbjpoQL-d7JQo

To claim this, I am signing this object:

@calopez
calopez / closure-table-operations.sql
Created May 20, 2018 19:39 — forked from kentoj/closure-table-operations.sql
Closure Table operations SQL fragments
-- Retrieve descendants
-- ====================
-- retrieve descendants of #4
SELECT c.*
FROM Comments AS c
JOIN TreePaths AS t ON c.comment_id = t.descendant
WHERE t.ancestor = 4;
-- Retrieve ancestors
@calopez
calopez / gist:e2401d2132a8be76cce871fbe643411a
Created November 3, 2017 17:07 — forked from giannisp/gist:ebaca117ac9e44231421f04e7796d5ca
Upgrade PostgreSQL 9.6.5 to 10.0 using Homebrew (macOS)
After automatically updating Postgres to 10.0 via Homebrew, the pg_ctl start command didn't work.
The error was "The data directory was initialized by PostgreSQL version 9.6, which is not compatible with this version 10.0."
Database files have to be updated before starting the server, here are the steps that had to be followed:
# need to have both 9.6.x and latest 10.0 installed, and keep 10.0 as default
brew unlink postgresql
brew install postgresql@9.6
brew unlink postgresql@9.6
brew link postgresql
%% Based on code from
%% Erlang Programming
%% Francecso Cesarini and Simon Thompson
%% O'Reilly, 2008
%% http://oreilly.com/catalog/9780596518189/
%% http://www.erlangprogramming.org/
%% (c) Francesco Cesarini and Simon Thompson
-module(frequency).
-export([start/0,allocate/0,deallocate/1,stop/0]).
%% Based on code from
%% Erlang Programming
%% Francecso Cesarini and Simon Thompson
%% O'Reilly, 2008
%% http://oreilly.com/catalog/9780596518189/
%% http://www.erlangprogramming.org/
%% (c) Francesco Cesarini and Simon Thompson
-module(frequency).
-export([init/0]).
-module(palin).
-export([
palin/1,
nopunct/1,
palindrome/1,
client/0,
server/0
]).
% palindrome problem
-module(sort).
-export([qsort/1, test/0]).
-include_lib("eunit/include/eunit.hrl").
qsort([]) -> [];
qsort([X1|Xs]) -> qsort(Xs, X1).
qsort(L, Pivot) -> qsort(left(L, Pivot)) ++ [Pivot] ++ qsort(right(L, Pivot)).
right(L, Pivot) -> [X || X <- L, X > Pivot].
-module(hof).
-export([
compose/1,
iterate/1,
twice/1,
test/0
]).
-include_lib("eunit/include/eunit.hrl").
compose([Fi|Fs]) -> fun(Arg) -> Fi(compose(Fs, Arg)) end.