Skip to content

Instantly share code, notes, and snippets.

require 'ostruct'
class ConfigStruct < OpenStruct
def self.block_to_hash(block=nil)
config = self.new
if block
block.call(config)
config.to_hash
else
{}
@cldwalker
cldwalker / x
Created April 7, 2009 17:48
ubiquity github-user command
CmdUtils.makeBookmarkletCommand({
name: 'github-user',
icon: "http://github.com/favicon.ico",
homepage: 'http://tagaholic.me/2009/04/06/github-bookmarklet-for-user-pages.html',
license: 'MIT',
author: {name: 'Gabriel', email: 'gabriel.horner@gmail.com'},
description: 'Enhances github user pages by providing repository sorting and inserting additional repository stats.',
help: 'Use it on a github user page i.e. http://github.com/defunkt',
url: 'javascript:(function(){$.getScript("http://tagaholic.me/javascripts/jquery.githubUserPage.js",function(){$.githubUserPage()})})();'
});
@cldwalker
cldwalker / mini-irb.rb
Created July 23, 2009 11:28
mini-irb
%w{readline rubygems bond}.each {|e| require e }
Bond.start
history_file = File.join(ENV["HOME"], '.mini_irb_history')
IO.readlines(history_file).each {|e| Readline::HISTORY << e.chomp } if File.exists?(history_file)
while (input = Readline.readline('>> ', true)) != 'exit'
begin puts "=> #{eval(input).inspect}"; rescue Exception; puts "Error: #{$!}" end
end
File.open(history_file, 'w') {|f| f.write Readline::HISTORY.to_a.join("\n") }
@cldwalker
cldwalker / mini-console.rb
Created July 23, 2009 11:34
mini script/console
['readline', 'rubygems', 'bond', File.dirname(__FILE__) + '/../config/boot'].each {|e| require e }
Bond.start
["#{RAILS_ROOT}/config/environment", 'console_app', 'console_with_helpers'].each {|e| require e}
history_file = File.join(ENV["HOME"], '.myrb_history')
IO.readlines(history_file).each {|e| Readline::HISTORY << e.chomp } if File.exists?(history_file)
while (input = Readline.readline('>> ', true)) != 'exit'
begin puts "=> #{eval(input).inspect}"; rescue Exception; puts "Error: #{$!}" end
end
File.open(history_file, 'w') {|f| f.write Readline::HISTORY.to_a.join("\n") }
@cldwalker
cldwalker / my_active_record_table.rb
Created September 3, 2009 06:44
Example of customizing hirb helper for active record
# Customize description to include model class
class Hirb::Helpers::MyActiveRecordTable < Hirb::Helpers::AutoTable
def self.render(rows, options={})
@model_class = rows.is_a?(Array) ? rows[0].class : rows.class
super
end
def self.model_class; @model_class; end
def render_table_description
@cldwalker
cldwalker / chars.rb
Created December 18, 2009 09:10 — forked from jeroenvandijk/chars.rb
Example of using Boson as an option parser
#!/usr/bin/env ruby
module Chars
extend self
def scale(args, options={})
scale = options[:scale]
chars = args.map { |x| Chars.char(x.to_i).map { |row| row.split("") } }
print_chars scale_vertically(scale_horizontally(chars, scale), scale)
end
@cldwalker
cldwalker / run_example.rb
Created December 28, 2009 22:37 — forked from jeroenvandijk/run_example.rb
Example of using Boson to call subcommands in one file
# In example.rb
module Example
extend self
# This method does something
def command1(arg1, arg2, options = {})
end
# This method does something else
def command2(arg1, arg2, options = {})
@cldwalker
cldwalker / my_core.rb
Created January 29, 2010 20:14
Example of overriding a boson core command in a separate library
module MyCore
def self.config
{:force=>true}
end
# My overridden version that also handles arrays of hashes with :url
def browser(*urls)
urls.map! {|e| e[:url] } if urls[0].is_a?(Hash) and urls[0].has_key?(:url)
system('open', *urls)
end
@cldwalker
cldwalker / gemspec_builder.rb
Created May 10, 2010 20:01
Builds gemspecs for multiple gems based on a YAML config.
# GemspecBuilder builds a Gem::Specification object by looking up a gem's config in the config file ~/.gems.yml.
# GemspecBuilder assumes the current directory's basename is the gem name (unless told otherwise) and needs to be
# run inside the gem's root directory in order for the gemspec to build correctly.
#
# The builder merges the default gemspec hash in config[:default] with any gem-specific configuration in config[:gems][gem_name].
# Gemspec hashes are assumed to have the same attribute-value pairs as Gem::Specification objects (described here:
# http://rubygems.rubyforge.org/rdoc/Gem/Specification.html). The only difference is the :files attribute which takes globbed expressions
# and is converted with Dir.glob.
#
# For an example config see mine: http://github.com/cldwalker/dotfiles/blob/master/.gems.yml
@cldwalker
cldwalker / dumped
Created June 1, 2010 20:23
Example output of boson dump_rdf command
$ boson dump_rdf http://datagraph.org/jhacker/foaf.nt
+------------------------------------+-------------------------------------------------+---------------------------------------------------+
| subject | predicate | object |
+------------------------------------+-------------------------------------------------+---------------------------------------------------+
| http://datagraph.org/jhacker/foaf | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://xmlns.com/foaf/0.1/PersonalProfileDocument |
| http://datagraph.org/jhacker/foaf | http://xmlns.com/foaf/0.1/maker | http://datagraph.org/jhacker/#self |
| http://datagraph.org/jhacker/foaf | http://xmlns.com/foaf/0.1/primaryTopic | http://datagraph.org/jhacker/#self |
| http://datagraph.org/jhacker/#self | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://xmlns.com/foaf/0.1/Person