Skip to content

Instantly share code, notes, and snippets.

View jszmajda's full-sized avatar

Jess Szmajda jszmajda

View GitHub Profile
module MongoMapper
module Associations
class InArrayProxy < Collection
def set_parent(parent, options={})
self.each do |k|
k.set_parent(parent)
if (!options.has_key?(:recursive) or options[:recursive] == true) and k.send( reflection.name ).size > 0
k.send( reflection.name ).set_parent(k, options)
end
end
# in lib/asset_tag_helper_ext.rb
module ActionView
module Helpers
module AssetTagHelper
alias :orig_javascript_include_tag :javascript_include_tag
def javascript_include_tag(*sources)
options = sources.extract_options!.stringify_keys
rkey = '__scripts'
request[rkey] ||= []
class Car
include MongoMapper::Document
key :make, String
key :_type, String
end
class Honda < Car
key :honda_thing, String
end
module FooHelper
include PartialMethods
end
gem 'ruby-terminfo'
require 'terminfo'
def syst(cmd)
sym = '-'
dist = ((TermInfo.screen_size[1].to_f/2.to_f) - ((cmd.length.to_f + 2.to_f) / 2.to_f)).floor.to_i
mid = cmd
#color = "7;93"
color = "93"
newlines = 4
# based on http://code.activestate.com/recipes/528911-barcodes-convert-upc-e-to-upc-a/
class Upc
def self.convert_upc_e_to_upc_a(upce=nil)
return upce if upce.nil? or upce.length < 6 or upce.length > 8 # not a UPC-E
upce = case upce.length
when 6
upce
when 7
upce[0,6]
# manifest-entry-field => [ search-method, product-data-source, link-weight ]
# search-method: is the name of a function [fuzzy_search | manufacturer_search | exact]
# OR a lambda which receives ( the value of manifest-entry-field, the value of product-data-source )
# and returns the matching product or nil
LINKING_COLUMNS = {
:title => [:fuzzy_search,
:title,
5],
#:description => [:fuzzy_search, :description, 10],
$('#bidding .editable').editable('/loads/<%=@load.id%>/manifest_entries/edit_in_place.ajax', {
'id': 'elementid',
'name': 'elementname',
'callback': function(result,settings){
var field = $(this).attr('class').split(/ /).pop();
if(field.match(/_cents$/)){
$(this).html(result.manifest_entry.manifest_entry[field]/100).formatCurrency();
} else {
$(this).html(result.manifest_entry.manifest_entry[field]);
}
@jszmajda
jszmajda / bar_spec.rb
Created November 15, 2010 14:17
how to handle conflicting stubbing requirements
require 'spec_helper'
describe "Bar" do
it "should not bar" do
lambda {
"foo".bar
}.should raise_exception
end
end
@jszmajda
jszmajda / test_http_clients.rb
Created February 10, 2011 22:26
a benchmark to test Net::HTTP, HTTParty, Curb, and system curl
require 'rubygems'
require 'httparty'
require 'curb'
require 'net/http'
require 'benchmark'
include Benchmark
RUNS = 1000
url = 'http://localhost:4567/'