Skip to content

Instantly share code, notes, and snippets.

@Wardrop
Wardrop / gist:4976817
Created February 18, 2013 11:48
No means to access outer class from singleton class.
# Doesn't work; needs reference to outer class object.
class SomeClass
class << self
extend Delegate
def hash
@hash ||= {}
end
delegate hash, :[]=, :has_key?, :each
# Formats the given byte value into a human readable form.
#
# Accepts four optional arguments that control the output format:
# as_bits: If true, converts the given byte value to bits and adjusts the suffix accordingly. Defaults to false.
# binary: If true, calculates order of magnitude as base 2 (binary) instead of base 10 (decimal). Defaults to true.
# full_suffix: If true, uses full suffix names such as "Megabyte", otherwise uses abbreviations like "MB" or "Mib". Defaults to false.
# precision: The number of decimal points to include in the output. Trailing zero's are removed. Defaults to 2.
def format_bytes(bytes, as_bits: false, binary: true, full_suffix: false, precision: 2)
suffixes = ['', 'Kilo', 'Mega', 'Giga', 'Tera', 'Peta', 'Exa', 'Zetta', 'Yotta']
# Current Validation Strategy
validates_presence :place_id
errors.add(:place, "must have a parent") unless place.parent_id
# Improved Validation Strategy
validates_presence :place_id
validates :place, "must have a parent" { place.parent_id }
error do |e|
if response.content_type.index(mime_type(:json)) == 0
halt 400, {success: false, exception: e.message}.to_json
end
end
@Wardrop
Wardrop / gist:1797102
Created February 11, 2012 06:20
Padrino reloader problem...
DEBUG -11/Feb/2012 16:18:07 RELOAD (0.0157ms) /Users/tomwardrop/Sites/Tombstone/app/controllers/reservation.rb
DEVEL -11/Feb/2012 16:18:07Reloading Tombstone::App
DEVEL -11/Feb/2012 16:18:07Removed constant: Tombstone::Role
DEVEL -11/Feb/2012 16:18:07Removed constant: Tombstone::ModelPermissions::InstanceMethods
DEVEL -11/Feb/2012 16:18:07Removed constant: Tombstone::ModelPermissions::DatasetMethods
DEVEL -11/Feb/2012 16:18:07Removed constant: Tombstone::ModelPermissions
DEVEL -11/Feb/2012 16:18:07Removed constant: Tombstone
DEBUG -11/Feb/2012 16:18:07 LOADING (0.0287ms) /Users/tomwardrop/Sites/Tombstone/app/lib/model_permissions.rb
DEBUG -11/Feb/2012 16:18:07 LOADING (0.0549ms) /Users/tomwardrop/Sites/Tombstone/app/lib/permissions.rb
DEBUG -11/Feb/2012 16:18:07 LOADING (0.0221ms) /Users/tomwardrop/Sites/Tombstone/app/models/base.rb
@Wardrop
Wardrop / reservation.rb
Created February 11, 2012 06:20
Snippet of controller file
module Tombstone
App.controller :reservation do
get :new do
@root_places = Place.with_child_count.filter(:parent_id => nil).order(:name).naked.all
render 'reservation/new'
end
end
end
final_width = 400
final_height = 400
for (var i = 0; i < app.documents.length; i++) {
crop_width_by = 0
crop_height_by = 0
doc = app.documents[i]
app.activeDocument = doc
landscape = (doc.height < doc.width)
if (landscape == true) {
def precompiled_preamble(locals)
locals.map { |k,v| "#{k} = locals[#{k.inspect}]"}.join("\n")
end
# Please ignore this Gist and check out this instead: https://gist.github.com/913370
def precompiled_preamble(locals)
locals.map { |k,v| "#{k} = #{v}" }.join("\n")
end
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader'
before do
headers "Content-Type" => "application/octet-stream"
end
get "/" do
"Hello World!"