Skip to content

Instantly share code, notes, and snippets.

@kurko
kurko / transform_to_utf.php
Created October 16, 2009 17:28
Tranforme todos os arquivos de um diretório e suas subpastas de ISO-8859-1 para UTF-8. Cuidado: não rode este script em arquivo que não sejam ISO-8859-1, senão você vai perder seus arquivos. Para se precaver contra isto, é necessário implementar uma verif
<?php
/*
* NÃO USE ESTE SCRIPT SOBRE ARQUIVOS UTF-8! VOCẼ PERDERÁ TUDO.
*
* Não use este arquivo sem fazer Backup dos arquivos antes.
*
* Alterações necessárias:
* - verificar se o arquivo atual é ISO-8859-1 realmente.
* - verificar se é um arquivo de texto (transformar imagens vai inutilizá-las
*/
@kurko
kurko / calc_latitude_longitude_distance.php
Created October 26, 2009 11:45
Calcula a distância entre dois pontos com latitude e longitude.
function calcDist($lat_A, $long_A, $lat_B, $long_B) {
$distance = sin(deg2rad($lat_A))
* sin(deg2rad($lat_B))
+ cos(deg2rad($lat_A))
* cos(deg2rad($lat_B))
* cos(deg2rad($long_A - $long_B));
$distance = (rad2deg(acos($distance))) * 69.09;
@kurko
kurko / event_handler_for_vars.js
Created February 9, 2011 14:25
Connects variables to callbacks, making it possible to connect JS views to models
/*
event_handler.js adds a layer for setting variables in Javascript
and calling a callback whenever it is changed.
You won't need to call a function manually to update your view.
This method makes it easy to separated view-logic from business-logic
at Javascript level, and let things run automatically.
*/
@kurko
kurko / mysql_tip_on_inserting_and_subqueries.rb
Created April 4, 2011 20:44
Let's say we want to calculate something, i.e. increase a counter
# this doesn't work
"INSERT INTO table1 (field1) VALUE( (SELECT field1 FROM table1 LIMIT 1)+1 )"
# "You can't specify target table 'table1' for update in FROM clause"
# but this works
"INSERT INTO table1 (field1) VALUE( (SELECT field1 FROM (SELECT field1 FROM table1 LIMIT 1))+1 )"
# the FROM statement becomes just a repetition of the subquery.
# Thus, MySQL creates the temporary table with no problem.
@kurko
kurko / omniscient_clone_syntax.sh
Created April 6, 2011 23:37
Sketching a good syntax for Omniscient gem.
# Omniscient is a gem that automatically dumps a MySQL database in a remote server,
# copies the dumped file to localhost via scp and imports it, overwriting the local
# database (or table).
# It'll be very useful for those that are developing using more than one machine and
# don't want to dump and clone data manually.
# Setup a new host machine
$ omniscient add
@kurko
kurko / image_caching.rb
Created July 26, 2011 18:23
Small script for resizing and saving (caching) images inside a directory
require 'mini_magick'
suffix = "cached_"
sleep_between_process = 0.3
sizes = ["x142","x200"]
# loop through files
Dir.glob("[^cache]*").each { |e|
image_path = File.expand_path(e)
image = MiniMagick::Image.open(image_path)
@kurko
kurko / readme.rb
Created November 8, 2011 05:04
SOLID e BDD
# No ActiveRecord, Client has_one ClientProfile
#
# ClientProfile tem um campo chamado Twitter. Como é um varchar
# e um usuário pode ter 2 contas no Twitter, salvo os dados no
# formato "usuario;usuario2;usuario3" neste campo. O problema abaixo gira em torno
# do processamento desta string e como testar isto usando os princípios SOLID que
# você abordou na sua palestra na RubyConf.
#
# O split(";") era feito no model, o qual tirei para fora (classe ClientService abaixo).
# O problema é: como vou testar com mock se o argumento injetado precisa ser ActiveRecord?
# If you've been in a medium to large project, you know how painful
# it becomes to maintain your domain logic in ActiveRecord files.
#
# Later you extract classes to lib/, so now you have a) domain logic
# classes and b) persistence objects (ActiveRecord). This means you
# started using the DataMapper pattern.
#
# Below is the draft for a gem to implement DataMapper pattern in Ruby.
# The Unit of Work pattern stands as an observer - whenever the domain
# object changes, the UoW saves its attributes to the database.
@kurko
kurko / intercept_ivars.rb
Created February 12, 2012 01:11 — forked from myronmarston/intercept_ivars.rb
Crazy hack to intercept all changes to instance variables
module IVarInterceptor
def self.included(klass)
def klass.new(*args, &block)
super.extend IVarInterceptor
end
end
def self.extended(object)
object.singleton_class.class_eval do
original_methods = instance_methods.each_with_object({}) do |method_name, hash|
@kurko
kurko / spec_helper.rb
Created February 29, 2012 21:37
Sunspot with RSpec
#inside RSpec.configure
config.before :all do
SunspotTest.stub
end
config.before(:all, search: true) do
SunspotTest.setup_solr
Sunspot.remove_all!
Sunspot.commit