Skip to content

Instantly share code, notes, and snippets.

@IgnusG
IgnusG / map_through_associations.rb
Last active August 25, 2016 16:05
Will map through all associations defined on a rails model and collect the result of a shared method
klas = item_type.constantize
has_many_associations = klas.reflections.collect { |_, c| c.class_name if c.macro == :has_many }.compact
has_one_associations = klas.reflections.collect { |_, c| c.class_name if c.macro == :has_one }.compact
instance = klas.find item_id
@result = instance.some_method
has_one_associations.each do |assoc_string|
assoc_string = assoc_string.underscore
@IgnusG
IgnusG / context-loader.js
Last active September 6, 2016 19:20
Modifies context of imports. Should be called before whatever compiles the imports
const fs = require('fs');
const requirePatterns = [
/(import.*from\W*)(?:(')(.*)(?!\/)'|(")(.*)(?!\/)")(.*)/g,
/(require\W*\()(?:(')(.*)(?!\/)'|(")(.*)(?!\/)")(.*)/g
];
const extension = '.ts';
function decodeOption(key, value) {
switch (key) {

Closing & Opening

Action Keyboard Shortcut
Close Ctrl + F4
Close Others Alt + Shift + F4
Close All Ctrl + Alt + Shift + F4
Close Unmodified Alt + F4
Reopen Ctrl + Shift + T
function isMultipleOf(x, say) {
return (i) => i % x == 0 ? say() : '';
}
const thenSay = (say) => () => say;
// The order matters. Any function with the signature (any)->(int)->str is valid
const whatShouldISayWhen = [
isMultipleOf(3, thenSay('Fizz')),
isMultipleOf(5, thenSay('Buzz'))
];
function detectSeparator(input) {
const availableSeparators = [ ';', ',', '|' ];
const separatorCount = availableSeparators.map((separator) => occurrences(input, separator));
return R.transpose([ availableSeparators, separatorCount ])
.reduce((maxSeparator, separator) => {
return separator[1] > maxSeparator[1] ? separator : maxSeparator;
}, [ availableSeparators[0], -1 ])[0];
}
@IgnusG
IgnusG / order_by_array.rb
Last active February 23, 2018 19:02
Orders one array according to another array
objects = []
order = %w(first second)
objects
.group_by(&:order_by_this) # Create grouping according to field
.each { |_, h| h.sort_by!(&:sort_each_by_this).reverse! } # Each hash sorted individually
.values_at(*order) # Sorting according to order
.compact # Reject empty hash keys (no elements for order item)
.flatten(1) # Remove hash table
.concat(objects.reject { |o| order.include? o.order_by_this }) # Append all unidentified at the end
@IgnusG
IgnusG / git-superfixup.pl
Last active April 13, 2022 12:59 — forked from oktal3700/git-superfixup.pl
Perl script for automating the process of creating fixup! commits for use with git rebase -i --autosquash
#!/usr/bin/perl
# Scan unstaged changes in git tracked files, identify which commits they could
# be applied to as fixups, and automatically produce the appropriate "fixup!"
# commits for use with "git rebase -i --autosquash".
#
# Copyright (C) 2016, 2017 by Mat Sutcliffe
# This program is free software; you can redistribute it and/or modify it under
# the GNU General Public License as published by the Free Software Foundation;
# either version 2 of the License, or (at your option) any later version.
@IgnusG
IgnusG / books.md
Last active July 12, 2019 17:41
Books to Read
  • The Art of War
  • How to talk TED
  • Debt: years
  • Start with Why?
  • Leaders eat last
  • Aufm Weg zur finanzieller Freiheit
  • Adversaries into allies (Bob Burg)
@IgnusG
IgnusG / parks&rec.md
Created July 31, 2018 14:55
My favorite quotes from Parks and Rec

Best Quotes

  • Which one citizen described as "Disturbingly Enticing"
  • That's my husband. ... Who's he carying?
  • Either you hire her back or I quit! ... You don't work for me
  • You couldn't remember your phone number. But you gave me your phone
  • You're trying to lure this young lady into your van? ... Yeah?! But she's being really difficult about it
  • Greg, your service is crap.
  • You forgot to paint a painting son
  • You can wait at that table and someone will be here sometime. ... But aren't you here now? ... No