Skip to content

Instantly share code, notes, and snippets.

View Oshuma's full-sized avatar

Dale Campbell Oshuma

  • @unite-us
  • Earth
View GitHub Profile
@Oshuma
Oshuma / gist:30800
Created December 1, 2008 18:42
View helper I use in my Merb blog's atom feed to create a TagURI.
# Create a Tag URI for the given +article+
# (See http://www.taguri.org/)
# Should return something along the lines of:
# tag:example.org,2008-12-01:/articles/1
def tag_uri(article)
tag = absolute_url(:article, :id => article)
tag.gsub!(/^http:\/\//, 'tag:')
tag.gsub!('#', '/')
pub_date = article.created_at.strftime("%Y-%m-%d")
tag.gsub!(/#{request.host}/, "#{request.host},#{pub_date}:")
def next_page(format = 'markdown')
chapter = params[:chapter] || :toc
page_name = params[:page_name]
current_file = Dir["#{Merb.root}/book-content/#{language}/*-#{chapter}/*-#{page_name||"*"}.#{format}"].entries.first
if current_file
current_file.grep(/book-content\/\w{2}\/(\d{1,})[-]\w+[-]\w+\/(\d{1,})[-]\w+[.]\w+/)
chapter_number = $1
page_number = $2
end
next_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number}-*/#{page_number.to_i + 1}-*.#{format}"].entries.first
@Oshuma
Oshuma / merb-0.1.rb
Created December 7, 2008 21:41
The first 'public' version of Merb, originally located here: http://pastie.org/14416
#!/usr/local/bin/ruby
require 'rubygems'
require 'mongrel'
# Add the request handler directory to the load path.
# Files in the 'app/controllers' dir will be mapped against the first segment
# of a URL
$LOAD_PATH.unshift( File.join( File.dirname( __FILE__ ) , 'app/controllers' ) )
PORT = 4000
# in the model:
def self.all_by_date(year, month, order = [:created_at.desc])
year = year.to_i
start_month = month ? month.to_i : 1
start_day = 1
start_date = Date.new(year, start_month, start_day)
end_month = month ? month.to_i : 12
end_day = days_in_month(year, end_month)
@Oshuma
Oshuma / rock_paper_scissors.rb
Created January 16, 2009 00:47
Sinatra powered Rock, Paper, Scissors!
# Sinatra powered Rock, Paper, Scissors!
require 'rubygems'
require 'sinatra'
configure do
TITLE = 'Sinatra powered Rock, Paper, Scissors!'
RPS = %w(rock paper scissors)
RPSSL = RPS + %w(spock lizard)
end
# Return the given key's value from the (CakePHP) database config
# file. This will read through the config string looking for
# 'key' => 'value' pairs in the PHP array. Then, it will
# strip newlines, spaces, and substitute any quotes.
#
# I use this in a Rakefile I created to help ease the
# pain of working with CakePHP.
#
# Usage:
# File.open('database.php') do |f|
@Oshuma
Oshuma / Rakefile
Created February 12, 2009 06:30
CakePHP Rakefile...wait, what?
# A Rakefile I use to ease the pain of working with CakePHP.
# Put this in your app/ directory.
APP_DIR = File.dirname(__FILE__)
CAKE_DIR = File.join(File.dirname(__FILE__), '..', 'cake')
CONSOLE_DIR = File.join(CAKE_DIR, 'console')
task :default => 'notes:todo'
desc 'Open the CakePHP console'
@Oshuma
Oshuma / tweet_trend.rb
Created February 16, 2009 03:43
Simple class to grab the latest trendy asshole topics.
require 'json'
require 'open-uri'
# Access the latest Twitter trends.
#
# Usage:
#
# TweetTrend.new.display
#
# - or -
~2 days per pack
20 smokes per pack
10 packs in a carton
smoking for 7 years
7 * 365 = 2555 days
2555 days / 2 (packs/day) = 1277.5 packs
@Oshuma
Oshuma / ruby_proj.rb
Created March 12, 2009 02:29
ruby_proj.rb
class RubyProj
def self.create(name)
%x| mkdir #{name} |
%x| touch #{name}/Rakefile |
%x| mkdir -p #{name}/lib |
%x| touch #{name}/lib/#{name}.rb |
%x| mkdir -p #{name}/spec |
%x| touch #{name}/spec/spec_helper.rb |
%x| touch #{name}/spec/#{name}_spec.rb |
end