Skip to content

Instantly share code, notes, and snippets.

@char0n
char0n / api_data_structure.ex
Last active August 1, 2022 16:03
Model example how to use Ecto to validate nested JSON data in you API payloads
defmodule ApiDataStructure do
defmodule User do
use Ecto.Schema
import Ecto.Changeset
alias ApiDataStructure.Profile
embedded_schema do
field :username, :string
@jamesgmarks
jamesgmarks / gist:56502e46e29a9576b0f5afea3a0f595c
Last active March 12, 2024 19:30
MySQL/MariaDB BIN_TO_UUID and UUID_TO_BIN Polyfill
DELIMITER //
CREATE FUNCTION BIN_TO_UUID(b BINARY(16))
RETURNS CHAR(36)
BEGIN
DECLARE hexStr CHAR(32);
SET hexStr = HEX(b);
RETURN LOWER(CONCAT(
SUBSTR(hexStr, 1, 8), '-',
SUBSTR(hexStr, 9, 4), '-',
@srevenant
srevenant / time.ex
Last active October 10, 2019 19:41
Time bits for use in Elixir. Sometime, it's easier to just fall back to posix time... LMK if you find better ways to do this!
defmodule Utils.Time do
import Utils.Types, only: [str_to_int!: 1, str_to_float: 1]
import Utils.Enum, only: [enum_rx: 2]
# note for future
# https://hexdocs.pm/nimble_parsec/NimbleParsec.html
def iso_time_range(input) when is_binary(input) do
case String.split(input, "/") do
[stime, etime] ->
@mark-d-holmberg
mark-d-holmberg / rails-5.2.3-ruby-2.6.4-gem-engine
Created September 10, 2019 20:51
Create a Rails 5.2.3 on Ruby 2.6.4 Gem engine running API JSON mode only
rails plugin new elemental -d mysql --mountable -T --dummy-path=spec/dummy --skip-action-mailer -C --skip-turbolinks --skip-action-cable --skip-spring --skip-active-storage --skip-coffee --skip-yarn --api -p
/* bring in configs, and backends, like db */
/* const config = require('config') */
let healthy = true
setInterval(() => {
// check db pools, maybe run a query, etc.
try {
if (db.check_health()) { // but do this with a timelimit that is less than check interval seconds
healthy = true
@srevenant
srevenant / .bashrc
Last active September 7, 2018 18:36
fast-forward git bash helpers
# When working on a project with multiple developers master will get ahead of your work.
# To come in sync should you rebase? merge? what is a fast-forward? Lots of discussion
# is available on this, here are a few:
# https://hackernoon.com/git-merge-vs-rebase-whats-the-diff-76413c117333
# https://www.atlassian.com/git/tutorials/merging-vs-rebasing
#
# I prefer a merge/fast-forward, rather than rebase. These helpers assist in that process.
# LMK your thoughts and if you know of a better way! Enjoy!
@tkfu
tkfu / srd_5e_monsters.json
Last active May 2, 2024 15:04
A JSON file with all the D&D 5e SRD monster data
[
{
"name": "Aboleth",
"meta": "Large aberration, lawful evil",
"Armor Class": "17 (Natural Armor)",
"Hit Points": "135 (18d10 + 36)",
"Speed": "10 ft., swim 40 ft. ",
"STR": "21",
"STR_mod": "(+5)",
"DEX": "9",
@elderbas
elderbas / class_controller.ex
Last active April 5, 2024 03:35
Elixir — Inserting Multiple Changesets Into Database - create batch
def create_batch(conn, %{"people" => people_params}) do
changesets = Enum.map(people_params, fn class ->
Person.changeset(%Person{}, person)
end)
result = changesets
|> Enum.with_index()
|> Enum.reduce(Ecto.Multi.new(), fn ({changeset, index}, multi) ->
Ecto.Multi.insert(multi, Integer.to_string(index), changeset)
end)
@austra
austra / Largest Binary Gap
Last active December 14, 2017 21:23
Largest Binary Gap
I read about this problem and thought I would try it. Later I found it is a question here too:
https://codility.com/programmers/lessons/1-iterations/binary_gap/
My first instinct was, "I know regex can do this, but I hate regex" (Which is also a good reason to get better at regex).
I couldn't quite hoop the syntax so I moved on. Here is what I came up with:
def max_binary_gap(n)
max_gap = 0
arr = n.to_s(2).chars
while arr.size > 2 do
@takeit
takeit / INSTALL.md
Last active March 23, 2024 19:03
Write to NTFS on macOS Sierra (osxfuse + ntfs-3g)
  1. Install osxfuse:
brew cask install osxfuse
  1. Reboot your Mac.

  2. Install ntfs-3g: