Skip to content

Instantly share code, notes, and snippets.

View cyberfox's full-sized avatar

Morgan Schweers cyberfox

View GitHub Profile
@cyberfox
cyberfox / Category.h
Created February 9, 2011 09:00
All the code necessary to find a Category object by name in Objective C
#import <CoreData/CoreData.h>
#import "FindableData.h"
@interface Category : FindableData
{
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSSet *auctions;
@property (nonatomic, retain) NSNumber *auctionCount;
@cyberfox
cyberfox / auction_table_datasource.rb
Created February 9, 2011 08:55
MacRuby example of using Core Data to search for Category objects
class AuctionTableDatasource
attr_writer :app_delegate
attr_accessor :view
.
.
.
def set_selected_category(category_name)
context = @app_delegate.managedObjectContext
@current_category = Category.find_first(context, by_name:category_name)
@cyberfox
cyberfox / drag_drop.rb
Created February 4, 2011 02:41
Drag and drop destination setup using MacRuby
class TableViewDataSource
def awakeFromNib
view.registerForDraggedTypes(
NSArray.arrayWithObjects("BookmarkDictionaryListPboardType", "MozURLType",
NSFilenamesPboardType, NSURLPboardType, NSStringPboardType, nil))
end
def tableView(aView, validateDrop:info, proposedRow:row, proposedDropOperation:op)
NSDragOperationEvery
end
@cyberfox
cyberfox / stable_sort_example.rb
Created January 31, 2011 09:56
Showing unstable and stable sorting in Ruby.
# This example is flawed, but hopefully useful for demonstration purposes.
def test_stable_sorting
ary = (1..100).to_a.shuffle + (1..100).to_a.shuffle
# This associates an ordering with the randomized numbers
idx = 0
paired = ary.collect {|value| [value, idx += 1]}
puts "Now the numbers are paired; the first is the random number 1-100,"
puts "the second is its sequence within the 200 entries."
puts paired.inspect
@cyberfox
cyberfox / whazzup.rb
Created January 5, 2011 01:43
Asks for status every 15-25 minutes to track where time goes. Traps USR2 to wake and prompt for status. Designed for Linux, works on OS X, needs tweaks for Windows. Uses JRuby, since it's the only easy-to-get cross-platform UI toolkit for Ruby.
require "java"
java_import javax.swing.JOptionPane
class Whazzup
def initialize(file = "#{ENV['HOME']}/snippets.txt")
@snippets ||= open(file, 'ab')
log('[Starting up (ruby)]')
thread_loop = Thread.current
# Include this in your RAILS_ROOT/test/test_helper.rb
class ActionController::TestCase
def better_cookies
cookies = {}
Array(@response.headers['Set-Cookie']).each do |cookie|
key = nil
details = cookie.split(';').inject({}) do |fields, cookie_field|
pair = cookie_field.split('=').map {|val| Rack::Utils.unescape(val.strip)}
key = pair.first unless key
$$('.switch').each(function(elem) {
real_element = elem.id.replace("-switch", "");
if(!$(real_element).visible()) {
table = elem.parentNode.parentNode.parentNode.parentNode;
table.hide();
if(table.previousElementSibling != null) {
if(table.previousElementSibling.tagName == 'BR') {
table.previousElementSibling.hide();
}
}
begin-base64 644 auto_shoulda.tar.bz2
QlpoOTFBWSZTWfMxtz8ABAZ/lPyQACBy5/+/eiGeCv/v//QACIABAAhQBR7qp0dBylAUFAylAGmg
BoaAABoA0YmR6gAARFNp6UyZGgBo00BoZNGQA0ADTIaCRCCEpqe01I0P1T1NqeoaDQNqGI9IPU9R
6gNqHDTTIxGE0wEMAmmEYJiZDTI0NAFSSENMpo00NNTaJNMmIaA0AAYmTRppo9Ej0/YnSdhvk0e+
q/GRa0rFA7KSJx0d/u798WggWrGrJEArqr4qLRwh0/HdwtMiQxe0UFSEqQtIshUj8BOyO17Kiqr+
/L1bxloIg7QPCIYgdn6mhnXBsLXGskaRjMPk1RcupnL5GxJI9cioE0CR6sRpRaz07fkhNpjLf69U
jAUGsHL2cJXReb6hs/rVfYDbomNFhfx04Ji3lrfycAGYpSLygdwMQw/fXckIEImFCcAoFGYsqfUI
RlOiRCE9IEgVZmqGTGya0yjUMBQ4hoIPcSfU2M14tczczVRk128eKaP2f2s53AzeuQGaYgOEvTlb
8EQ3Y3uTtg4yX+GHo9tNyttpl24QNxB7sFcn4u0vY8M5VZzi9SRJI0MB7I5MBvgEABoNOZ3K3FKU
5SABpJCDEQANQZQGYMkkqt7MLCRY3pDl1mJY2EzJLz15IuY0vP4JDzpXXMk4xVXebJFMvdRpUpYS
@cyberfox
cyberfox / resource_factory.rb
Created November 9, 2009 23:35
Make ActiveResource able to handle authentication w/o acting like a global variable.
# This is a gross hack to make it possible to access the same
# resource from the context of different users at the same time.
# I'm quite positive this is wasteful and bad, but it's the only
# way to 'de-global' subclasses of ActiveResource::Base. :( It's
# also possible it leaks memory if the GC doesn't collect Class
# objects. -- Morgan Schweers, 09-Nov-2009
class ResourceFactory
def self.get(klass)
Class.new(klass) do |resource|
@cyberfox
cyberfox / Modules can access instance variables of the included-to class
Created October 29, 2009 19:25
Modules can access instance variables of the included-to class
>> module Foo
>> def bar
>> puts @quux
>> end
>> end
=> nil
>> class Baz
>> attr_accessor :quux
>> def xyzzy