Skip to content

Instantly share code, notes, and snippets.

View NikitaAvvakumov's full-sized avatar

Nikita Avvakumov NikitaAvvakumov

View GitHub Profile
@NikitaAvvakumov
NikitaAvvakumov / for_pattern_match.ex
Created October 30, 2017 10:10
Elixir pattern matching in `for` loop
items = [%{id: 1, deleted: false}, %{id: 2, deleted: true}, %{id: 3, deleted: false}]
for item <- items do
IO.puts Map.fetch!(item, :id)
end
# 1
# 2
# 3
# [:ok, :ok, :ok]
@NikitaAvvakumov
NikitaAvvakumov / code.js
Last active September 30, 2020 06:23
Google Apps Script - send spreadsheet changes to an external API
// provided `onEdit` trigger cannot use `UrlFetchApp.fetch`.
// create a custom-named function, then add it as a new trigger in "Edit" > "Current Project Triggers"
// with events "From spreadsheet" > "On edit"
function customOnEdit(e) {
const range = e.range;
const row = range.getRow();
const sheet = e.source.getActiveSheet();
const data = {
vendor: sheet.getRange(row, 1).getValue(),
# macOS
for f in `ag -l <target> [path]`; do sed -i '' -e 's/<target>/<replacement>/' $f; done
# Linux
for f in `ag -l <target> [path]`; do sed -i'' -e 's/<target>/<replacement>/' $f; done
from h in Webhook, where: fragment("?->>? = ?", h.body, "title", ^"some title") # note `=` NOT `==`
from h in Webhook, where: fragment("?->>? = ?::text", h.body, "title", ^"some title") # can specify type if needed
from h in Webhook, where: fragment("?->>'title' ILIKE ?", h.body, ^"some title")
# Ruby's `=` can be a part of a method name, but is not a method on its own. Consider:
class MyClass
def initialize(value)
@a = value
end
def a=(new_value) # `=` is part of a method name
@a = new_value # `=` is an operator
end
# from http://youtu.be/fGFM_UrSp70
def slow(&block)
block.call
end
def fast
yield # 5x faster than passing a block as an argument
end
@NikitaAvvakumov
NikitaAvvakumov / add_father_deceased_to_orphan_spec.rb
Created March 10, 2015 14:41
Migration spec for adding father_deceased to Orphan and populating it with opposite values of existing father_alive column
#TODO Remove this brittle spec after it proves that the migration works
require 'rails_helper'
migration_file_name = '20150307173710_add_father_deceased_to_orphans.rb'
migration_file_path = Rails.root.join 'db', 'migrate', migration_file_name
version = migration_file_name.split('_').first
require migration_file_path
@NikitaAvvakumov
NikitaAvvakumov / add_status_enums_to_orphan_spec.rb
Created March 6, 2015 13:11
Migration spec for adding enums to a model and converting from existing associations
#TODO Remove this brittle spec after it proves that the migration works
require 'rails_helper'
migration_file_name = '20150226095809_add_status_enums_to_orphan.rb'
migration_file_path = Rails.root.join 'db', 'migrate', migration_file_name
version = migration_file_name.split('_').first
require migration_file_path
@NikitaAvvakumov
NikitaAvvakumov / indents.rb
Created November 10, 2014 09:17
OSRA style guide - indents
# no indentation
def send_mail(source)
Mailer.deliver(to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', body: source.text)
end
# single indent
def send_mail(source)
Mailer.deliver(
to: 'bob@example.com',
from: 'us@example.com',
@NikitaAvvakumov
NikitaAvvakumov / orphan_osra_num.rb
Created October 3, 2014 09:32
Orphan OSRA number
# Solution 1
class Partner < ActiveRecord::Base
belongs_to :province
has_many :orphan_lists
delegate :code, to: :province, prefix: true
end
class Orphan < ActiveRecord::Base