Skip to content

Instantly share code, notes, and snippets.

@potatosalad
potatosalad / cdcatalog.xml
Created November 9, 2010 18:40
XML to SQL example
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<!-- Edited by XMLSpy® -->
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
@potatosalad
potatosalad / hashquiz.rb
Created December 31, 2010 04:36
Ruby quiz for convert hash "dot paths" into actual hash hierarchy.
#require 'rubygems'
require 'pp'
#require 'ap' # Awesome Print
class Object
# expects [ [ symbol, *args ], ... ]
def recursive_send(*args)
args.inject(self) { |obj, m| obj.send(m.shift, *m) }
end
end
@potatosalad
potatosalad / kraken-example.rb
Created March 26, 2011 18:13
Example of Kraken's EAV at work
# products:
# - _id
# - name
# - kraken_id
# - parent_id
class Product
include Mongoid::Document
include Mongoid::Kraken
field :name, :type => String
end
#!/usr/bin/env ruby
#
# Originally written by http://redartisan.com/tags/csv
# Added and minor changes by Gavin Laking
# Rewritten by Andrew Bennett for Ruby 1.9
#
# Usage: ruby csv_to_fixture.rb file.csv [--json]
#
# "id","name","mime_type","extensions","icon_url"
# "1","unknown","unknown/unknown","||","/images/icon/file_unknown.gif"
resources :categories
## equivalent to:
get '/categories(.:format)' => 'categories#index', :as => :categories
post '/categories(.:format)' => 'categories#create'
get '/categories/new(.:format)' => 'categories#new', :as => :new_category
get '/categories/:id/edit(.:format)' => 'categories#edit', :as => :edit_category
get '/categories/:id(.:format)' => 'categories#show', :as => :category
put '/categories/:id(.:format)' => 'categories#update'
resources :categories, :constraints => { :id => /.+?/ }
## equivalent to:
get '/categories(.:format)' => 'categories#index', :as => :categories
post '/categories(.:format)' => 'categories#create'
get '/categories/new(.:format)' => 'categories#new', :as => :new_category
get '/categories/*id/edit(.:format)' => 'categories#edit', :as => :edit_category
get '/categories/*id(.:format)' => 'categories#show', :as => :category
put '/categories/*id(.:format)' => 'categories#update'
@potatosalad
potatosalad / bottles.rb
Created September 20, 2011 17:48
Paste this into a rails console and type "99.bottles.of_beer.on_the_wall.sing!"
# paste into a rails console
# usage: 99.bottles.of_beer.on_the_wall.sing!
# alternate: 24.bottles.of_water.on_my_desk.sing!
class Bottle < ::ActiveSupport::BasicObject
attr_accessor :value, :type, :where
def initialize(value, type = nil, where = nil) #:nodoc:
@value, @type, @where = value, type, where
end
<!-- jQuery library for embedding gists in blog posts, etc. -->
<div id="1352696" class="gist"></div>
<!-- or -->
<div id="gist-1352696" class="gist"></div>
<script src="http://gist.pagodabox.com/jquery.gist.js"></script>
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="https://raw.github.com/brandonaaron/jquery-mousewheel/master/jquery.mousewheel.js"></script>
require 'digest/md5'
def bson_id
[
'%08x' % Time.now.utc.to_i, # 4 bytes
Digest::MD5.hexdigest(`hostname`.strip)[0..5], # 3 bytes
'%04x' % $$, # 2 bytes
'%06x' % rand(0..('f' * 6).to_i(16)) # 3 bytes
] # 12 bytes total
end