Skip to content

Instantly share code, notes, and snippets.

@ericsaboia
ericsaboia / rails_rename.rb
Created February 24, 2011 17:12
Implementação para renomear projetos Rails
class RailsRename
class << self
def args()
if (ARGV.size != 2)
raise ArgumentError, "Você deve passar dois parametros: path_da_app novo_nome_app"
end
ARGV[0] += '/' unless ARGV[0].end_with? '/'
ARGV[0].match(/.*(^|\/)(.*)\/$/)
module ActiveRecord
class Base
def self.build
instance = self.new
self.column_names.grep(/_id/).each { |coluna|
coluna.match(/(.*)_id$/)
instance.send($1+"=", $1.camelize.constantize.new)
}
@ericsaboia
ericsaboia / ruby_utf8.rb
Created May 4, 2011 19:41
Puts "utf8 encoding" in all rb files of specified path
if (ARGV.size != 1)
raise ArgumentError, "you must inform a param with path of ruby files"
end
files_path = Dir["#{ARGV[0]}**/**{.rb}"]
files_path.each do |file_path|
data = File.new(file_path).read
utf8 = "# encoding: utf-8"
File.open(file_path, 'w') { |f| f.write(utf8 + "\n\n" + data) } unless data.include? utf8
@ericsaboia
ericsaboia / gist:994611
Created May 27, 2011 03:54
Changelog ActiveRecord:Base Rails 3.1
# ActiveRecord::Base#dup and ActiveRecord::Base#clone semantics have changed to closer match normal Ruby dup and clone semantics.
# Calling ActiveRecord::Base#clone will result in a shallow copy of the record, including copying the frozen state. No callbacks will be called.
# Calling ActiveRecord::Base#dup will duplicate the record, including calling after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return true for new_record?, have a nil id field, and is saveable.
@ericsaboia
ericsaboia / gist:994614
Created May 27, 2011 03:58
Changelog ActiveRecord:Base Rails 3.1
# ActiveRecord::Base#dup and ActiveRecord::Base#clone semantics have changed to closer match normal Ruby dup and clone semantics.
# Calling ActiveRecord::Base#clone will result in a shallow copy of the record, including copying the frozen state. No callbacks will be called.
# Calling ActiveRecord::Base#dup will duplicate the record, including calling after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return true for new_record?, have a nil id field, and is saveable.
@ericsaboia
ericsaboia / gist:995341
Created May 27, 2011 14:22
Hack for speedup rspec tests with heavy factories
#new way:
describe Model do
before(:all) do
@model_factory = Factory.build(:factory_model)
end
before(:each) do
@model = @model_factory.clone
end
@ericsaboia
ericsaboia / .gitconfig
Created June 2, 2011 16:35
Adicionando cores no terminal para os comandos do git
[core]
quotepath = false
[color]
branch = auto
diff = auto
status = auto
[color "branch"]
current = yellow reverse
@ericsaboia
ericsaboia / gist:1063653
Created July 4, 2011 17:29
TCPSocket Client
socket = TCPSocket.new('localhost', 8081)
socket.write(configs)
all_data = []
while true
partial_data = socket.recv(1012)
if partial_data.length == 0
break
end
@ericsaboia
ericsaboia / eventmachine.rb
Created September 10, 2011 18:07
Eventmachine server example
require 'eventmachine'
module EchoServer
def post_init
puts "-- someone connected to the echo server!"
end
def receive_data data
send_data ">>>you sent: #{data}"
close_connection if data =~ /quit/i
@ericsaboia
ericsaboia / report_server.rb
Created September 21, 2011 12:24
Eventmachine Server
# encoding: utf-8
require 'rubygems'
require 'eventmachine'
require 'java'
require 'lib/report'
require 'lib/filler'
module ReportServer