Skip to content

Instantly share code, notes, and snippets.

View walerian777's full-sized avatar
🥒
Senior Fermentation Engineer

Walerian Sobczak walerian777

🥒
Senior Fermentation Engineer
View GitHub Profile
@walerian777
walerian777 / csv_dump.rb
Created January 28, 2019 13:04
Dump ActiveRecord::Relation to CSV
def csv_dump(relation:, file_path: nil)
file_path ||= "#{relation.table.name}.csv"
CSV.open(file_path, 'wb') do |csv|
csv << relation.klass.attribute_names
relation.each do |record|
csv << record.attributes.values
end
end
end
@walerian777
walerian777 / Fetcher.ts
Last active September 2, 2022 02:24
Data fetching it typescript
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'
interface HeadersObject {
[key : string] : string
}
interface FetcherResponse {
data : object,
headers : HeadersObject
}
@walerian777
walerian777 / christmas_draw.rb
Created November 11, 2017 15:49
A simple script for christmas draw. Pass an array of names to find out who is preparing a gift for whom.
require 'digest'
class ChristmasDraw
attr_accessor :names
def initialize(names)
@names = names
end
def call

Keybase proof

I hereby claim:

  • I am walerian777 on github.
  • I am walerian (https://keybase.io/walerian) on keybase.
  • I have a public key whose fingerprint is 0BA8 BCC6 458A D0E9 6AD1 E5C4 F91A 4E1F 0952 255B

To claim this, I am signing this object:

@walerian777
walerian777 / password_generator.coffee
Created January 3, 2017 10:47
A function which generates a password containing at least one lowercase letter, one uppercase letter and a digit.
_ = require('lodash')
PasswordGenerator = (length = 20) ->
_.map(_.shuffle(_.range(length)), (n) ->
switch n % 3
when 0 # A digit
Math.floor(Math.random() * 9).toString()
when 1 # A lowercase letter
String.fromCharCode(Math.floor(Math.random() * 26) + 97)
when 2 # An uppercase letter
@walerian777
walerian777 / transfer_tables.sql
Created November 7, 2016 15:51
PostgreSQL: Transfering scoped tables
-- This function transfers all rows from tables which contains `_scope_column_name` AND `_update_column_name`,
-- whose value in a column called `_scope_column_name` is equal to `_scope_column_value`,
-- by updating the value of `_update_column_name` with `_update_column_value`.
-- Finally, it updates `_update_column_name` with `_update_column_value` in the record from `_scope_table_name` table,
-- where primary key is equal to `_scope_column_value`.
CREATE OR REPLACE FUNCTION transfer_tables(_scope_table_name regclass, _scope_column_name text, _scope_column_value uuid, _update_column_name text, _update_column_value uuid)
RETURNS void AS
$func$
DECLARE
@walerian777
walerian777 / copy_row_from_table.sql
Created November 7, 2016 15:48
PostgreSQL: Copying scoped tables
-- This function inserts a copy of an existing row (with primary key equal to `_id`) into a table called `_tbl`.
-- Returns a `row_id` which is a primary key of the new row.
CREATE OR REPLACE FUNCTION copy_row_from_table(_tbl regclass, _id uuid, OUT row_id uuid) AS
$func$
BEGIN
EXECUTE (
SELECT format('INSERT INTO %1$s(%2$s) SELECT %2$s FROM %1$s WHERE id = $1 RETURNING id',
_tbl, string_agg(quote_ident(attname), ', '))
FROM pg_attribute