Skip to content

Instantly share code, notes, and snippets.

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

Tiago Davi tiagodavi

🏠
Working from home
View GitHub Profile
<?php
// app/classes/myrules.php
class MyRules
{
// note this is a static method
public static function _validation_unique($val, $options)
{
list($table, $field, $id) = explode('.', $options);
$result = DB::select("LOWER (\"$field\")", 'id')
@tiagodavi
tiagodavi / login_spec.rb
Last active August 29, 2015 14:17
Capybara passes with rack_test, but don't passes with selenium. Wtf?
require 'rails_helper'
feature "Login" do
background do
#Factory Girl
@user = create(:user)
end
scenario "With correct credentials" do
@tiagodavi
tiagodavi / resolve-nodejs.sh
Created March 31, 2015 14:51
Resolve nodejs npm ERR!
npm config set registry https://registry.npmjs.org/
@tiagodavi
tiagodavi / happy_number_spec.rb
Last active August 29, 2015 14:18
BDD with Happy Numbers
describe HappyNumber do
it "Is possible to chek if a number is happy?" do
happy_number = HappyNumber.new
expect(happy_number.happy?(7)).to be true
end
end
#happy_number.rb
class HappyNumber
def happy?(number)
@tiagodavi
tiagodavi / decorators.py
Created November 10, 2011 19:54
Decorators In Python
#Recebe a função soma como argumento (f = soma)
def meu_decorator(f):
#Escreve uma nova função
#nova_soma recebe os mesmos argumentos da soma (não precisa ser *args)
#Imagine que é uma nova versão da sua soma
def nova_soma(x,y):
#Executa algo antes
print 'iniciando nova soma'
f(x,y) #Chama a própria função soma (a soma externa)
#Executa algo depois
@tiagodavi
tiagodavi / arguments.py
Created November 10, 2011 20:49
Exemplo de *args e **kwargs (argumentos ilimitados, nomeados e em qualquer ordem)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Exemplo de *args e **kwargs (argumentos ilimitados, nomeados e em qualquer ordem)
class Conta():
#É precico passar o self para dentro dos métodos de instância
#O Python é bem explícito :)
def metodo(self, *args, **kwargs):
print args
print kwargs
@tiagodavi
tiagodavi / Laco.scala
Created November 12, 2011 02:49
Exemplo de for super legal utilizando Scala
//Exemplo de for super legal utilizando Scala
object Laco{
//main (um pouquinho diferente do java)
def main(args: Array[String]){
for{
/*
Para cada 5 incrementos em i vai haver 1 incremento em j
Quando i chegar a 5 vai resetar seu contador para 1 e j vai incrementar para 2
E assim sucessivamente... ate j chegar a 5
*/
@tiagodavi
tiagodavi / clojure.php
Created November 18, 2011 22:42
Simplificando e juntando a recursividade com closures.
<?php
//mdc (PHP 5.3)
$mdc = function($x,$y) use (&$mdc){
$a = max($x,$y);
$b = min($x,$y);
return ($a%$b == 0) ? ($b) : ($mdc($b,($a%$b)));
};
//mmc (PHP 5.3)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- jquery CDN -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script>
<script type="text/javascript">
$(function(){
//Verifica se temos acesso ao recurso
<?php
//Content-Type do tipo stream
header('Content-Type: text/event-stream');
//Sem cash
header('Cache-Control: no-cache');
//Inicia uma função
function send_msg($msg) {
//Envia um json
echo 'data:'.json_encode(array('id' => rand(0,100), 'msg' => $msg));
//Descarrega o buffer de saída