Skip to content

Instantly share code, notes, and snippets.

View ibanez270dx's full-sized avatar
:shipit:
shippin' it

Jeff Miller ibanez270dx

:shipit:
shippin' it
View GitHub Profile
@ibanez270dx
ibanez270dx / example.rb
Last active July 14, 2016 03:46
TracerPoint
# Setup ActiveRecord
require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'benchmark.db')
ActiveRecord::Migration.class_eval { create_table :users } unless ActiveRecord::Base.connection.table_exists? 'users'
class User < ActiveRecord::Base;end
# Use TracerPoint
user = User.new
trace = tracerpoint do
puts user.present?
def tracerpoint
calls = []
trace = TracePoint.new(:call, :c_call) do |tp|
calls << [tp.defined_class, tp.method_id, tp.lineno]
end
trace.enable
yield
trace.disable
{
calls: calls.group_by(&:itself).map {|k, v| {k => v.length}}.sort_by {|h| -h.values.first},
@ibanez270dx
ibanez270dx / filestack.rb
Last active June 4, 2016 21:19
Helper for displaying an image using filestack.com and their conversion options
# Usage:
# <%= filestack_image_tag post.filestack_url, resize: { width: 100, height: 200, fit: crop }, oil_paint: { amount: 7 } %>
#
# => https://process.filestackapi.com/[FILESTACK API KEY]/resize=width:72,height:72,fit:crop/oil_paint=amount:7/https://cdn.filestackcontent.com/abc23...
def filestack_image_tag(url, task_options={}, image_tag_options={})
base_path = "https://process.filestackapi.com/#{Rails.application.config.filestack_api_key}"
task_path = task_options.collect do |task, options|
"#{task.to_s}=#{options.collect{|k,v| "#{k}:#{v}"}.join(',')}"
end.join('/')
@ibanez270dx
ibanez270dx / setup.md
Last active June 16, 2016 17:06
Server Setup

Update and upgrade the system

sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade && sudo apt-get autoremove
sudo reboot

Configure timezone

sudo dpkg-reconfigure tzdata
sudo apt-get install -y ntp
@ibanez270dx
ibanez270dx / snippet1.rb
Created June 19, 2016 09:34
Snippets from my blog post "Better Conditional Validations in Rails"
class Person < ActiveRecord::Base
validates :surname, presence: true, if: "name.nil?"
end
This file has been truncated, but you can view the full file.
A
a
aa
aal
aalii
aam
Aani
aardvark
aardwolf
Aaron
@ibanez270dx
ibanez270dx / confirmation.rb
Last active March 2, 2018 03:26
Capistrano Deploy Confirmation
# /lib/capistrano/tasks/confirmation.cap
namespace :deploy do
task :confirmation do
color = case fetch(:stage)
when :production then 31
when :staging then 33
else 34
end
@ibanez270dx
ibanez270dx / colors-syntax.less
Last active May 7, 2018 06:08
Afterglow theme overrides
// afterglow-syntax/styles/colors.less
// Add this to colors.scss in afterglow ui and syntax packages
@white : rgb(255, 255, 255);
@ghost : rgb(250, 250, 255);
@snow : rgb(249, 249, 254);
@vapor : rgb(246, 246, 251);
@white-smoke : rgb(245, 245, 250);
@silver : rgb(232, 232, 237);
@smoke : rgb(230, 225, 225);
@ibanez270dx
ibanez270dx / config.yml
Last active June 17, 2018 21:45
CircleCI 2.0 w/ Ruby + RSpec, MySQL, Redis, ElasticSearch
version: 2
jobs:
build:
parallelism: 2 # however many CPUs you need/pay for
#############################################
# Container Setup
#############################################
docker:
- image: circleci/ruby:2.5.0
@ibanez270dx
ibanez270dx / benchmark.rb
Last active September 8, 2018 20:54
Return time taken in milliseconds to process a block of code, along with its result.
module Benchmark
# Similar to Benchmark.realtime, but returns the result of the block along with time taken in milliseconds.
# Ex: Benchmark.perform{ sleep(3); "hello" } => [3003.2030000002123, "hello"]
#
# Benchmark.realtime | https://ruby-doc.org/stdlib-2.5.0/libdoc/benchmark/rdoc/Benchmark.html#method-c-realtime
# Process.clock_gettime | http://ruby-doc.org/core-2.5.0/Process.html#method-c-clock_gettime
def self.perform
t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)