Skip to content

Instantly share code, notes, and snippets.

@pedromg
pedromg / gist:2898946
Created June 9, 2012 01:11
Use of mouse event offsetX and offsetY in Firefox. With jQuery 1.7.2
$("#element").click(function (ev) {
if(typeof ev.offsetX === "undefined" || typeof ev.offsetY === "undefined") {
var targetOffset = $(ev.target).offset();
ev.offsetX = ev.pageX - targetOffset.left;
ev.offsetY = ev.pageY - targetOffset.top;
}
// now ev.offsetX and ev.offsetY can be used
// ...
Gem::Specification.new do |s|
s.name = 'bang'
s.version = '0.1.0'
s.platform = Gem::Platform::RUBY
s.author = 'Jeff Kreeftmeijer'
s.email = 'jeff@kreeftmeijer.nl'
s.summary = 'Bang!'
s.description = 'Bangs existing model methods'
s.files = ['bang.rb']
@pedromg
pedromg / Gemfile
Created March 9, 2012 10:27
Rails Lightweight Stack. Most of this is detailed on Crafting Rails Applications - http://pragprog.com/book/jvrails/crafting-rails-applications
source :rubygems
# We are not loading Active Record, nor Active Resources etc.
# We can do this in any app by simply replacing the rails gem
# by the parts we want to use.
gem "actionpack", "~> 3.2"
gem "railties", "~> 3.2"
gem "tzinfo"
# Let's use thin
query = "param1=1&param2=2&offset=3"
unless query.empty?
# build an Hash based on the query string
# we want to build {"param1"=>"1", "param2"=>"2", "offset"=>"3"}
temp_params = {}
query.split("&").each {|v| temp_params.merge!(Hash[*v.split("=")]) unless (v.split("=").count < 2)}
# remove the offset parameter
temp_params.delete("offset")
# convert the Hash back to a String
@pedromg
pedromg / .bash_prompt
Created October 26, 2011 20:26 — forked from melo/.bash_prompt
uber git PS1
#!/bin/bash
#
# PS1 magic
#
# Mostly copied from YUVAL KOGMAN version, added my own __git_ps1 stuff
# Original: http://gist.github.com/621452
#
# See video demo of this at http://vimeo.com/15789794
#
# To enable save as .bash_prompt in $HOME and add to .bashrc:
@pedromg
pedromg / gist:1090142
Created July 18, 2011 17:47
DataMapper update_or_create
module DataMapper
module Model
# update_or_create method: finds and updates, or creates;
# -upon create, returns the object
# -upon update, returns the object (by default, returned True)
# @param[Hash] Conditions hash for the search query.
# @param[Hash] Attributes hash with the property value for the update or creation of a new row.
# @param[Boolean] Merger is a boolean that determines if the conditions are merged with the attributes upon create.
# If true, merges conditions to attributes and passes the merge to the create method;
# If false, only attributes are passed into the create method
@pedromg
pedromg / webapp.rb
Created November 17, 2010 12:53 — forked from igrigorik/webapp.rb
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
if !has('ruby')
finish
endif
" Vim syntax functions
" Language: wordpress_vim
" Maintainer: pedro mg <pedro.mota [at] gmail.com>
" Version: 1.1
" Last Change: 2008 Apr 04
" Remark: Simple functions for vim blogging bundle in ruby.
#! /usr/bin/ruby
##
# == remember:
# * insert your login and password at Line 100, and save this file.
# * of couse, be very carefull if you send this to a friend... REMOVE your credentials first!
# * if you are using Google Hosted for your Domain (Google Apps), your_login is something like: me@mydomain.com
# * example: go = GoGmail.new($*[0], 'imap.gmail.com', 993, $*[1], 'tree@forest.org', 'littlebird', $*[2])
#
# == usage:
module DataMapper
module Model
# update_or_create method: finds and updates, or creates;
# merger is a boolean that determines if the conditions are merged with the attributes upon create;
# merge = true => merges conditions to attributes and passes the merge to the create method
# merge = false => only attributes are passed into the create method
def update_or_create(conditions = {}, attributes = {}, merger = true)
(first(conditions) && first(conditions).update(attributes)) || create(merger ? (conditions.merge(attributes)) : attributes )
end