Skip to content

Instantly share code, notes, and snippets.

@jonstefansson
jonstefansson / reflect_on.rb
Created January 10, 2019 23:21
A function to get information about an Active Record model in a Rails console session
# Load this in a Rails console session with:
# play ~/Desktop/reflect_on.rb
def reflect_on(clazz)
result = {
column_names: clazz.column_names.sort,
associations: clazz.reflect_on_all_associations.map(&:name).sort,
class_methods: clazz.methods().find_all do |m_sym|
m = clazz.method(m_sym)
m.source_location && m.source_location[0].include?('/app/')
end.sort,
@jonstefansson
jonstefansson / flatten_schema.py
Created January 7, 2019 22:01
Flatten BigQuery JSON schema
import click
import json
@click.command()
@click.argument("input_source", type=click.File("rt"))
def flatten(input_source):
"""
Creates a flattened representation of a BigQuery JSON schema.
@jonstefansson
jonstefansson / nested_jq_filter.sh
Created May 11, 2017 23:05
A jq filter that converts an array objects with a nested array into CSV-formatted output with one row for each of the nested arrays and the common from the parent object duplicated.
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# A jq filter that converts an array objects with a nested array into
# CSV-formatted output with one row for each of the nested arrays and the
# common from the parent object duplicated.
# -----------------------------------------------------------------------------
jq -r '.Items[] | {"Sku": .Sku} + (.Locations[]|{"WarehouseCode": .WarehouseCode, "LocationCode": .LocationCode, "Quantity": .Quantity}) | ["\(.Sku)", "\(.WarehouseCode)", "\(.LocationCode)", .Quantity] | @csv' <<-EOF
{
"Items": [
{