Skip to content

Instantly share code, notes, and snippets.

View joshnesbitt's full-sized avatar

Josh Nesbitt joshnesbitt

View GitHub Profile
@joshnesbitt
joshnesbitt / newpost.rb
Created February 1, 2010 14:46 — forked from jrk/newpost.rb
#!/usr/bin/env ruby
require Dir
unless ARGV[0]
puts 'Usage: newpost "the post title"'
exit(-1)
end
blog_root = "/Users/jrk/proj/blog"
EM.add_periodic_timer(1) do
Job.all_queued do |job|
# perform job action
end
end
module ActionView
module Helpers
# CaptureHelper exposes methods to let you extract generated markup which
# can be used in other parts of a template or layout file.
# It provides a method to capture blocks into variables through capture and
# a way to capture a block of markup for use in a layout through content_for.
module CaptureHelper
# The capture method allows you to extract part of a template into a
# variable. You can then use this variable anywhere in your templates or layout.
#
class Integer
def tries(options={}, &block)
attempts = self
exception_classes = options[:on] || Exception
exception_classes = exception_classes.is_a?(Array) ? exception_classes : [exception_classes]
begin
return yield
rescue *exception_classes
retry if (attempts -= 1) > 0
module Configuration
def self.attributes
@attributes
end
def self.configure
base = ConfigBlock.new
yield base
module PercentagePatch
module InstanceMethods
def as_percentage_of(total)
return 0 if total == 0
self.to_f / total * 100
end
end
end
require 'rubygems'
require 'sinatra'
require 'redis'
# To use, simply start your Redis server and boot this
# example app with:
# ruby example_note_keeping_app.rb
#
# Point your browser to http://localhost:4567 and enjoy!
#
# With macro
has_many :comments, :foreign_key => 'commentable_id', :conditions => "commentable_type = 'User'", :dependent => :destroy
# Without macro
def comments
Comment.find_by_sql <<-SQL
SELECT * FROM comments C
WHERE C.commentable_id IS #{self.id}
AND C.commentable_type IS 'User'
SQL
module Configuration
def self.attributes
@attributes
end
def self.configure
base = ConfigBlock.new
yield base
Configuration.configure do |config|
config.support_email = "josh@example.net"
config.google_analytics_key = "UA-x343x-SDS"
config.twitter.screen_name = "joshnesbitt"
config.twitter.password = "mypassword"
end
puts Configuration.support_email
puts Configuration.google_analytics_key