Skip to content

Instantly share code, notes, and snippets.

@joakimk
joakimk / code.rb
Created March 21, 2013 14:51
How to handle serialized associations with minimapper that can be validated.
class Address
include Minimapper::Entity
attribute :street
attribute :postalcode
attribute :city
attribute :country
validates :street, presence: true
end
@joakimk
joakimk / base_env.sh
Created February 7, 2013 12:57
Using unicorn with preload_app. Init script, unicorn config and base environment. (not used in production yet). ERB templates.
#!/bin/sh
UNICORN_CONF="/data/<%= @app_name %>/shared/config/unicorn.rb"
UNICORN_EXEC="bundle exec unicorn"
UNICORN_PID="/var/run/<%= @app_name %>/unicorn.pid"
UNICORN_MAX_RELOAD_WAIT_TIME=60000 # 60 seconds
RAILS_ENV="<%= @rails_env %>"; export RAILS_ENV
RACK_ENV="<%= @rails_env %>"; export RACK_ENV
APP_ROOT=/data/<%= @app_name %>/current
@joakimk
joakimk / functional_core_imperative_shell.rb
Last active October 14, 2015 00:27
An example of "functional core, imperative shell" as far as I understand it. For more info about this concept, see https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell.
# Written for this gist, but inspired by production code (I haven't run this code).
# Imperative shell
class Timer
def initialize(record)
@record = record
end
def start
save_changes clock.start
@joakimk
joakimk / Example.md
Created November 29, 2012 10:52
More sane backtraces

Example image

@joakimk
joakimk / cpu_test.md
Last active March 26, 2024 20:25
Single core cpu performance test

Single core cpu performance.

echo 'int main() { double i = 0; for(i = 0; i < 5000000000; i++) { 20%7 * i; } }' > /tmp/test.c && gcc /tmp/test.c -o /tmp/test && time /tmp/test && rm /tmp/test.c && rm /tmp/test

NOTE: It's not 100% reliable since it differs depending on compiler version.

Platform                   CPU                   Time
MacBookPro15,1 (Mid 2018)  i9 2.4 ghz            11.1 
iMac15,1 (Late 2014)       i7 4.0 ghz            11.7

PC (Early 2016, i5 4690k) i5 3.5 ghz 12.0

@joakimk
joakimk / 0_how_to_run.sh
Last active October 11, 2015 18:58
Runnable minimapper example (you just need some version of ruby installed)
# Copy and paste this into a terminal:
gem install minimapper
gem install activemodel # required for Minimapper::Entity[::Rails] (you can use Minimapper::Entity::Core without it)
curl https://gist.github.com/joakimk/3904952/raw/56b592e6eaa5a0206bf9207b0ee53aa0114a53eb/minimapper_example.rb > minimapper_example.rb
ruby minimapper_example.rb
@joakimk
joakimk / Rakefile
Created October 2, 2012 11:24
Try to run no-rails rake tasks first and fallback to rails when none is found.
#!/usr/bin/env rake
# If the code below fails, uncomment this. There are no known issues now, but we had one.
#require File.expand_path('../config/application', __FILE__)
#Auctionet::Application.load_tasks
#require File.expand_path('../lib/tasks/no_rails', __FILE__)
#__END__
# Load all non-rails tasks.
path = File.expand_path('../lib/tasks/no_rails', __FILE__)
@joakimk
joakimk / gist:3738997
Created September 17, 2012 18:43
Single core cloud computing performance
# Very simple CPU test. Not ideal.
# Setup (where possible):
# Ubuntu 11.04
# apt-get install ruby # 1.8.7
# ci:jocke $ ruby -v
# ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]
ruby -e 't=Time.now;100000000.times { 22%5 };puts Time.now-t'
@joakimk
joakimk / example.rb
Created August 29, 2012 17:12
A simple form object built on top of virtus.
class Filter
include FormObject
# Proc because Date.yesterday changes every day :)
attribute :from, Date, default: Proc.new { Date.yesterday }
attribute :to, Date, default: Proc.new { 1.month.from_now - 1.day }
end
# in controller
@filter = Filter.new(params[:filter])
@joakimk
joakimk / script-turbux_rspec.rb
Created July 19, 2012 12:35
Command to run the specs the correct way when triggered from the turbux vim plugin.
#!/usr/bin/env ruby
#
# Command to run the specs the correct way when triggered from the turbux vim plugin.
#
# - non-rails unit tests should be run without bundler env and have extra options.
# - rails tests should run with drb and retry if it exists right away (sometimes unreliable).
rails_spec = ARGV.first.start_with?("spec/") || ARGV.first.include?('/spec/')
if rails_spec
command = "rspec --drb"