Skip to content

Instantly share code, notes, and snippets.

View leandro's full-sized avatar
🏠
Working from home

Leandro Camargo leandro

🏠
Working from home
View GitHub Profile
@leandro
leandro / vimrc
Created June 20, 2012 13:36
vimrc
source /Users/leandro/.vim/gist.vim
source /Users/leandro/.vim/plugin/matchit.vim
filetype indent on " Automatically detect file types.
set nocompatible " We don't want vi compatibility.
" Add recently accessed projects menu (project plugin)
set viminfo^=!
set ts=2 sw=2 et
@leandro
leandro / skype-search
Created October 17, 2012 17:39
Search through your whole skype history for any given term
require 'digest/sha2'
# Usage: ruby jenkins-log-parser.rb jenkins-output.txt
# ps.: run chamod +x on this file
log_file = $*.shift
class Digester
@digester = Digest::SHA256.new
def self.digest(msg)
@leandro
leandro / AR Model schema printer
Last active December 20, 2015 15:58
Rails Model's table info generator (indexes included)
def table_info(model)
puts "# Table name: #{model.table_name}", "#", "# Columns:"
cols = model.columns.inject([]) {|arr, e| arr << [e.name || '-', e.type || '-', e.sql_type || '-', e.limit || '-', e.default || '-'].map(&:to_s)}
col_sizes = cols.inject([]) {|arr, c| arr << c.map {|e| e.size }}
max_sizes = 4.times.inject([]) {|arr, i| arr << col_sizes.max_by {|e| e[i] }[i] }
cols.each { |c| puts "# #{c[0].ljust(max_sizes[0] + 2)}:#{c[1].ljust(max_sizes[1] + 3)}#{c[2].ljust(max_sizes[2] + 2)}#{c[3].ljust(max_sizes[3] + 2)}#{c[4]}" }
puts "#", "# Indexes:"
indexes = ActiveRecord::Base.connection.indexes(model.table_name).map {|e| [e.name, "(#{e.columns.join(", ")})"]}
module QueryTracer
# Borrowed some ANSI color sequences from elsewere
CLEAR = ActiveSupport::LogSubscriber::CLEAR
BOLD = ActiveSupport::LogSubscriber::BOLD
def self.start!
# Tap into notifications framework. Subscribe to sql.active_record messages
ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
QueryTracer.publish(*args)
@leandro
leandro / Gemfile
Last active August 29, 2015 14:21
Self sustainable standalone ruby script
source "https://rubygems.org"
# Declare your gem's dependencies in breakout_elasticsearch.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec
# jquery-rails is used by the dummy application
gem "jquery-rails"
@leandro
leandro / config__routes.rb
Created May 20, 2015 23:50
Reproducing issue #20204 in rails/rails (4.2.1)
Rails.application.routes.draw do
resources :fruits do
mount TestEngine::Engine => '/test', as: :test_1
end
resources :vegetables do
mount TestEngine::Engine => '/test', as: :test_2
end
end
@leandro
leandro / compass-custom
Last active August 29, 2015 14:22
Making a compass executable that runs specific versions of compass
#!/usr/bin/env ruby_executable_hooks
#
# This file was generated by RubyGems.
#
# The application 'compass' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
@leandro
leandro / run_with_timeout.rb
Last active September 9, 2015 16:39
Right way to make process calls and use Timeout.timeout (example).
def run_with_timeout(cmd, limit)
Open3.popen3(cmd) do |_, stdout, stderr, wait_thr|
output, pid = [], wait_thr.pid
begin
Timeout.timeout(limit) do
output = [stdout.read, stderr.read]
Process.wait(pid)
end
rescue Errno::ECHILD
rescue Timeout::Error
@leandro
leandro / dig_and_import.js
Created November 26, 2015 16:48
Simple port of Ruby 2.3' Hash#dig into Javascript's Underscore lib
(function() {
'use strict';
_.mixin({
// Similar to `_.dig` but uses `window` as the starting point object.
import: function(namespace, initialValue) {
return _.dig(window, namespace, true, initialValue);
},
// Fetch or create the nested objects referenced in @namespace@ (String or Array)