Skip to content

Instantly share code, notes, and snippets.

View mislav's full-sized avatar

Mislav Marohnić mislav

View GitHub Profile
@mislav
mislav / json2csv.rb
Created October 5, 2009 13:47
Dump Twitter followers to JSON or CSV
# Usage:
# ruby -rubygems json2csv.rb my-dump.json > people.csv
#
require 'json'; require 'csv'
FIELDS = %w(id screen_name name friends_count followers_count)
data = JSON.load ARGF
csv = CSV::Writer.create(STDOUT)
data.each { |user| csv << FIELDS.map { |f| user[f] } }
@mislav
mislav / waves.txt
Created October 15, 2009 01:10
My Shortwave configuration
> This is a Shortwave configuration file
> http://shortwaveapp.com/
>
> Some triggers copied from benpickles (http://gist.github.com/43371)
>
> Urls may contain the following replacement tokens:
>
> %s → search terms
> %r → URL of current page
> %d → domain part of the current URL
@mislav
mislav / nifty_renderer.rb
Created October 19, 2009 22:54
A nifty link renderer for will_paginate 3.0
## requires will_paginate "agnostic" branch
class NiftyRenderer < WillPaginate::ViewHelpers::LinkRenderer
protected
def pagination
[:previous_page, :nifty_info, :next_page]
end
def nifty_info
@mislav
mislav / models.rb
Created October 19, 2009 23:25
OMG DataMapper friggin rocks
class Page
include DataMapper::Resource
belongs_to :crawl
property :id, Serial
property :uri, URI
property :body, Text, :length => 65535
end
@mislav
mislav / gitconfig
Created October 28, 2009 16:20
git "conflicts" and "resolve" aliases
[alias]
conflicts = !git ls-files --unmerged | cut -c51- | sort -u | xargs $EDITOR
resolve = !git ls-files --unmerged | cut -c51- | sort -u | xargs git add
@mislav
mislav / survey
Created November 1, 2009 11:27 — forked from radar/survey.md
What did you do to get good at Rails?
Read source code, wrote plugins
Who taught you what you know?
Josh Susser! (about ActiveRecord)
Do you have any fond (or not so fond) memories of your learning experiences?
@mislav
mislav / datamapper_user_following.rb
Created November 1, 2009 13:56
DataMapper self-referential many-many
require 'dm-core'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite3::memory:')
class User
include DataMapper::Resource
has n, :follows
has n, :followings, :model => 'Follow', :child_key => [:target_id]
@mislav
mislav / walmart-scraper.rb
Created December 1, 2009 14:25
Solving Railscast #190 with Nibbler
#!/usr/bin/env ruby -rubygems
## solving Railscast #190 with Nibbler
## http://github.com/mislav/nibbler
## http://railscasts.com/episodes/190-screen-scraping-with-nokogiri
require 'nokogiri'
require 'open-uri'
require 'nibbler'
class Walmart < Nibbler
@mislav
mislav / update_counters.rb
Created December 2, 2009 19:32
One solution to update out-of-sync association counters
def update_counter(record, *names)
updates = names.each_with_object({}) do |name, memo|
counter = "#{name}_count"
diff = record.send(name).count - record.send(counter)
memo[counter] = diff unless diff.zero?
end
record.class.update_counters(record.id, updates) if updates.any?
end
Community.paginated_each(:per_page => 100) do |record|
@mislav
mislav / magic.rb
Created December 13, 2009 20:23
Poor man's magic models
require 'active_record'
class ActiveRecord::Base
def self.magic!
connection.tables.map { |table|
klass = Class.new(self)
Object.send(:const_set, table.singularize.camelize, klass)
}.each { |model|
model.column_names.grep(/_id$/).each { |foreign_key|
name = foreign_key.sub(/_id$/, '')