Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/ruby
#
# I deliberately didn't DRY /usr/local references into a variable as this
# script will not "just work" if you change the destination directory. However
# please feel free to fork it and make that possible.
#
# If you do fork ensure you add a comment here that explains what the changes
# are intended to do and how well you tested them.
#
require 'rubygems'
require 'dm-core'
require 'dm-postgres-adapter'
require 'adapter.rb' # local; patched
require 'comparison.rb' # local; patched
require 'symbol.rb'
#require 'dm-migrations'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'postgres://user:password@localhost/database')
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
#! /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:
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.
@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
@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 / .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:
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 / 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