Skip to content

Instantly share code, notes, and snippets.

View guilherme's full-sized avatar
🤔

Guilherme Campos guilherme

🤔
View GitHub Profile
@guilherme
guilherme / inheritance.rb
Created July 3, 2012 14:42
ar: reflections inheritance
require 'active_record'
class ModelA < ActiveRecord::Base
end
class SubModelA < ModelA
end
p SubModelA.reflections.keys
@guilherme
guilherme / inheritance_complex.rb
Created July 3, 2012 15:00
ar: reflections inheritance more complex test case
require 'active_record'
class ReferencedModelA < ActiveRecord::Base; end
class ReferencedModelB < ActiveRecord::Base; end
class ReferencedModelC < ActiveRecord::Base; end
class ModelA < ActiveRecord::Base
has_many :referenced_model_a
end
@guilherme
guilherme / arquivo.js
Created July 6, 2012 03:15
arquivo alvo do test do sed
before debug
// debug-start
var x = 'hello'
console.log(x);
// debug-end
after debug
@guilherme
guilherme / gist:3131788
Created July 17, 2012 20:18
color terminal
# ~/.bash_profile
export CLICOLOR=1
export LSCOLORS=gxfxcxdxbxegedabagacad
parse_git_branch() {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ →\ \1/'
}
export PS1='\[\e[1;37m\][\[\e[1;35m\]\u\[\e[1;37m\]@\[\e[1;32m\]\h\[\e[1;37m\]:\[\e[1;36m\]\w\[\e[1;31m\]$(parse_git_branch)\[\e[1;37m\]]$ \[\e[0m\]'
@guilherme
guilherme / pthread_mutex.c
Created September 26, 2012 23:51
pthread_mutex
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
void funcao1(int *id);
void funcao2(int *id);
@guilherme
guilherme / money.rb
Created October 1, 2012 23:13
money monkey patch
module ActiveRecord
class Base
def self.money_attributes(*attrs)
(attrs || []).each do |attr|
attr = attr.to_s
define_method attr do
read_attribute(attr).tap do |value|
value = value.to_money if value
end
end
@guilherme
guilherme / gist:4121922
Created November 20, 2012 23:14
thOOughtz #1
# "O que é mais interessante? Adicionar callbacks(ActiveRecord like) ou criar uma classe encapsulando o comportamento 'extra' desejado."
# "What is more interesting? Add callbacks(ActiveRecord like) or create a class that encapsulate the desired 'extra' behavior."
class B
before :method, :before_method
...
before :method, :before_methodN
after :method, :after_method
...
after :method, :after_methodN
@guilherme
guilherme / gist:4136536
Created November 23, 2012 17:19
Exemplo de NAT
echo "Ativando: IP FORWARDING"
echo
echo "1" > /proc/sys/net/ipv4/ip_forward
##################
### PREROUTING ###
# vamos supor:
def convert_to_f(argument = nil)
argument.tap do |attribute|
attribute = attribute.to_f if attribute
end
end
convert_to_f
# esperado: nil
# obtido: nil
class A < ActiveRecord::Base
def update_attributes_changing_type(attrs)
object = if attrs.keys.include?('type') || attrs.keys.include?(:type)
self.becomes((attrs['type'] || attrs[:type]).constantize)
else
self
end
object.class.instance_variable_set('@finder_needs_type_condition', :false)
object.update_attributes(attrs)
object.class.instance_variable_set('@finder_needs_type_condition', nil)