Skip to content

Instantly share code, notes, and snippets.

View moxley's full-sized avatar

Moxley Stratton moxley

View GitHub Profile
@moxley
moxley / process_container_args.sh
Created September 11, 2015 17:15
Bash function to process script args to pass to a docker sub command
# ./script --env=RAILS_ENV=production --rm -- bundle exec rails c
# DOCKER_ARGS:
# --env=RAILS_ENV=production
# --rm
# PROCESS_CMD: bundle exec rails c
function process_container_args {
DOCKER_ARGS=()
PROCESS_CMD=()
_state=DOCKER_ARGS
@moxley
moxley / shell_command.rb
Last active December 25, 2016 16:58
Runs shell command with similar IO stream behavior as a shell script: Stdout and stderr of command are captured and piped to caller's stdout and stderr.
require "open4"
require 'stringio'
class ShellCommand
class CommandError < StandardError
attr_reader :stderr_string
def initialize(message, stderr_string)
super(message)
@stderr_string = stderr_string
{
"query": {
"bool": {
"must": [
{
"dis_max": {
"queries": [
{
"bool": {
"should": [
@moxley
moxley / ruby_object_space_schema.sql
Last active August 29, 2015 14:19
PostgreSQL schema for a Ruby ObjectSpace.dump_all dump file
DROP TABLE IF EXISTS allocations;
CREATE TABLE allocations (
id SERIAL PRIMARY KEY,
dump_version varchar(255),
address varchar(255),
root varchar(30),
object_type varchar(30),
node_type varchar(30),
ivars integer,
class_address varchar(30),
@moxley
moxley / heap_to_pg.rb
Last active August 29, 2015 14:19
Convert a Ruby ObjectSpace.dump_all to a data dump, for import into PostgreSQL
require 'json'
require 'active_support/all'
class CreatePostgresDump
attr_accessor :filename,
:dump_version,
:append
COLS = %w(dump_version address root object_type node_type ivars class_address shared bytesize capacity value encoding memsize size length flags generation file line embedded struct is_frozen ref_addresses)
@moxley
moxley / gist:ab02712f82fb15850775
Created April 15, 2015 18:11
Send memory usage metrics for a given process to statsd
# To run:
# 1. cd into a bundler project directory
# 2. bundle exec ruby log_process.rb
require 'statsd'
require 'active_support/all'
METRICS = Statsd.new('127.0.0.1',
8127,
tags: ["service:console",
@moxley
moxley / gist:923ade42593e71dd804d
Last active August 29, 2015 14:13
Geo Area search for Elastcisearch
def search(query_text = 'Portland, OR')
query_int = Search::Query.new
client = query_int.client
query_parsed = client.indices.analyze(text: query_text)
fragments = query_parsed['tokens'].map { |q| q['token'] }
field_matchers = [
['State.name', :prefix],
['State.code', :term],
[1] website » class MyNull < ActiveRecord::Base
» end
=> nil
[2] website » my_null = MyNull.new
=> #<MyNull:0x007fc89cedd430> {
:id => nil,
:first_name => "foo",
:last_name => "bar"
}
[3] website » my_null.save!
househappy_test=# create table my_nulls (id integer not null, first_name character varying(255) default 'foo', last_name character varying(255) default 'bar');
CREATE TABLE
househappy_test=# create sequence my_nulls_id_seq start with 1 increment by 1 no minvalue no maxvalue cache 1;
CREATE SEQUENCE
househappy_test=# alter table only my_nulls alter column id set default nextval('my_nulls_id_seq'::regclass);
ALTER TABLE
househappy_test=# insert into my_nulls (first_name) values (null);
INSERT 0 1
househappy_test=# select * from my_nulls where first_name is null;
id | first_name | last_name
@moxley
moxley / in_memory_arel_spec.rb
Last active August 29, 2015 14:05
In-memory Arel
# require 'spec_helper'
class InMemoryArel
attr_accessor :collection
def initialize(collection)
self.collection = collection
end
def all