Skip to content

Instantly share code, notes, and snippets.

@estum
estum / abstract_singleton.rb
Created September 7, 2016 05:53
Abstract Singleton Module Class
class AbstractSingleton < Module
def self.base_class
self < AbstractSingleton ? superclass : self
end
module SingletonInstanceMethods
::Singleton.instance_methods.each do |name|
define_method(name, ::Singleton.instance_method(name))
end
end
require "erb"
module PostgreSQLTriggers
def create_trigger(name, **options, &block)
builder = TriggerBuilder.new(name, **options &block)
builder.each_trigger do |trigger|
reversible do |dir|
dir.up do
say "Create trigger #{trigger.name} on #{trigger.table}"
trigger.up(self)
@estum
estum / itunes-current_track-toggle_disliked.applescript
Last active May 26, 2017 13:52
iTunes: Toggle disliked of the current track
on run {input, parameters}
tell application "iTunes"
set theTrack to get current track
if theTrack's disliked then
set disliked of theTrack to false
display notification "Now is not disliked" with title ¬
"iTunes" subtitle theTrack's artist & " - " & theTrack's name ¬
sound name "Tink"
else
next track
@estum
estum / itunes-current_track-toggle_loved.applescript
Created May 26, 2017 13:51
iTunes: Toggle loved of the current track
on run {input, parameters}
tell application "iTunes"
set theTrack to get current track
if theTrack's loved then
set loved of theTrack to false
display notification "Now is not loved" with title ¬
"iTunes" subtitle theTrack's artist & " - " & theTrack's name ¬
sound name "Tink"
else
set loved of theTrack to true
@estum
estum / yieldable.rb
Created July 27, 2017 08:10
yieldable.rb: Memoized #to_proc for any callable object to use as a block argument with the "&" operator.
# This module can be used to easy make callable object or class
# to be used as a block argument with the <tt>&</tt> operator. It just
# implements the +to_proc+ method.
#
# == Examples:
#
# class Foo
# extend Yieldable
#
# def self.call(key, value)
@estum
estum / .md
Created August 14, 2019 11:09 — forked from joepie91/.md
Running a Node.js application using nvm as a systemd service

Read this first!

Hi there! Since this post was originally written, nvm has gained some new tools, and some people have suggested alternative (and potentially better) approaches for modern systems. Make sure to have a look at the comments to this article, before following this guide!


The original article

Trickier than it seems.

@estum
estum / APN.md
Created December 15, 2019 09:33
Getting Your APNs Certificate

Getting Your APNs Certificate

These instructions come from another great gem, apn_on_rails.

Once you have the certificate from Apple for your application, export your key and the apple certificate as p12 files. Here is a quick walkthrough on how to do this:

  1. Click the disclosure arrow next to your certificate in Keychain Access and select the certificate and the key.
  2. Right click and choose Export 2 items….
  3. Choose the p12 format from the drop down and name it cert.p12.
@estum
estum / joint.rb
Last active December 3, 2021 08:44
ActiveRecord's JOIN clause string wrapper class
# frozen_string_literal: true
class Joint < String
_patterns = [[/LATERAL$/, 'ON true'], [/^(?:NATURAL|CROSS)/, '']].deep_freeze!
_exception = "expected one non-blank argument of `on' or `using' unless type matches one of: #{_patterns.map(&:first).inspect}"
FALLBACK_BOOL_EXP = -> (type) do
case type
when _patterns[0][0]; _patterns[0][1]
when _patterns[1][0]; _patterns[1][1]
else raise ArgumentError, _exception
@estum
estum / recursive_shift_rows_with_modifier.sql
Last active June 5, 2020 20:13
PostgreSQL: recursively shift rows in order using modifier
-- +--------+-------+-------+
-- | init | mod | exp |
-- |--------+-------+-------|
-- | 6 | -1 | 5 |
-- | 5 | 0 | 4 |
-- | 4 | 1 | 6 |
-- | 3 | 0 | 3 |
-- | 2 | -1 | 1 |
-- | 1 | 0 | 2 |
-- +--------+-------+-------+
@estum
estum / version_storage.rb
Created June 5, 2020 20:08
Shrine.rb plugin to separate storages per versions
# frozen_string_literal: true
class Shrine
protected :_store, :_delete
module Plugins
# Separate storages per versions
#
# Usage:
#