Skip to content

Instantly share code, notes, and snippets.

View pedroteixeira's full-sized avatar

Pedro Henriques dos Santos Teixeira pedroteixeira

View GitHub Profile
@pedroteixeira
pedroteixeira / form.rb
Created August 10, 2012 18:27
Dynamic class for Formtastic
class Form
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
# Subclass may provide a types hash. Any attributes not listed will
# default to string.
# self.types = {
# :description => :text,
# :state => {:type => :string, :limit => 2}
@pedroteixeira
pedroteixeira / select2.js
Created August 3, 2012 19:55
select2 + twitter bootstrap
var onscroll = function() {
var open = $(".select2-drop.select2-drop-active.select2-with-searchbox");
var originalTop = open.data('original-top');
var top = originalTop || parseInt(open.css('top'), 10);
if(!originalTop) {
open.data('original-top', top);
}
//ajust top
open.css('top', (top + $(window).scrollTop()) + 'px');
@pedroteixeira
pedroteixeira / jruby_yaml_path.rb
Created July 26, 2012 23:07
psych patch jruby 1.6.7
module Psych
class << self
alias_method :old_dump, :dump unless method_defined?(:old_dump)
def dump(o, io = nil, options = {})
old_dump(o, io, options).force_encoding('utf-8')
end
end
end
@pedroteixeira
pedroteixeira / underscore-ext.js
Created April 24, 2012 22:03
Crates buffered function
// Calls a function only once within the given wait time. Last call is
// considered, which is the difference between from _.throtle
//
_.buffer = function(func, wait, scope) {
var timer = null;
return function() {
if(timer) clearTimeout(timer);
var args = arguments;
timer = setTimeout(function() {
@pedroteixeira
pedroteixeira / hash.rb
Created March 18, 2012 18:11
hash syntax
class Hash
def id
self["id"] || self[:id]
end
def type
self["type"] || self[:type] || self.type
end
def method_missing(meth, *args, &block)
if args.size == 0
@pedroteixeira
pedroteixeira / widget.html
Created March 17, 2012 20:21
exemplo de uso
<html>
<body>
<script type="text/javascript"
src="http://echo.gomus.com.br/player/mb.js?id=<##### ID DO PLAYER ######> ">
</script>
@pedroteixeira
pedroteixeira / SpamLord.java
Created March 11, 2012 23:15
coursera nlp pa1
// Modified SpamLord.java to call clojure code
public List<Contact> processFile(String fileName, BufferedReader input) {
List<Contact> contacts = new ArrayList<Contact>();
// for each line
Matcher m;
String email;
try {
List results = new spamlord().process(input);
;; http://pragprog.com/magazines/2012-03/comparing-java-and-scalas-expressiveness
;; first attempt for impl in clojure.. a lot to be improved! :)
(let [lines (map #(clojure.string/split % #" ")
(line-seq (clojure.java.io/reader (clojure.java.io/file "textfile.txt"))))
grouped (group-by #(second %) lines)
parse-long (fn [log] (Long/parseLong (first log)))
diff-times (fn [longs] (for [x (range (count longs)) :let [ts (nth longs x)] ]
(if (= 0 x) ts
(- ts (nth longs (- x 1))))))
@pedroteixeira
pedroteixeira / ar_storage.rb
Created June 2, 2011 13:09
ar ruote store
# -*- coding: utf-8 -*-
require 'rubygems'
require 'active_support/core_ext/module/aliasing'
require 'active_record' unless defined?(ActiveRecord)
require 'mysql2' #0.2.7
require 'active_record/connection_adapters/mysql2_adapter'
require 'meta_where' unless defined?(MetaWhere)
require 'rufus/json'
''=~+(
('(').
('?'). ((
'{')). ("\`"|
'%').( ('[')^ '-').( ('`')|
'!').( ('`')| "\,"). '"'.(
('[')^ '+'). ("\["^ "\)").( ('`')|