Skip to content

Instantly share code, notes, and snippets.

View kastiglione's full-sized avatar

Dave Lee kastiglione

View GitHub Profile
@kastiglione
kastiglione / signalForSelector.m
Last active August 29, 2015 13:58 — forked from bobspryn/signalForSelector.m
Delegate dance
- (void)viewDidLoad
{
[super viewDidLoad];
@weakify(self);
self.exploreSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, mTCTableViewFrameWidth, 44)];
self.exploreSearchBar.placeholder = @"Search";
[self.exploreSearchBar setBackgroundImage:[UIImage squareImageWithColor:mTCLightTanColor dimension:1] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
self.tableView.tableHeaderView = self.exploreSearchBar;
@kastiglione
kastiglione / distinct.m
Last active August 29, 2015 14:06
Distinct
- (RACSignal *)distinct {
return [RACSignal defer:^{
NSMutableSet *set = [NSMutableSet new];
NSObject *nullObject = [NSObject new];
return [self filter:^(id x) {
x = x ?: nullObject;
BOOL distinct = ![set containsObject:x];
if (distinct) [set addObject:x];
@kastiglione
kastiglione / gist:1062860
Created July 4, 2011 03:14
Hostname banning zsh function
# assumes control of all hosts in /Local/Default/Hosts
# https://gist.github.com/1062860
hostoggle() {
if (( ${#*} == 0 )); then
# no host given, print current list of hosts banned
dscl . -ls Hosts
fi
for host in "$@"; do
if $(dscl . -read Hosts/$host >&- 2>&-); then
# delete existing host records (raw and www-less)
@kastiglione
kastiglione / tldre.rb
Created April 20, 2012 03:57
Generate a TLD regex
require 'open-uri'
# monkey patching, but we're still cool right?
class Fixnum
def to_proc
->(obj) { obj[self] }
end
end
class Hash
@kastiglione
kastiglione / rubymotion-app-setup-after-hook.rb
Created July 28, 2012 22:14
RubyMotion App::setup after hook
class << Motion::Project::App
real_setup = instance_method(:setup)
define_method :setup do |&block|
real_setup.bind(self).call(&block)
# run after hook here
end
end
@kastiglione
kastiglione / custom_store.rb
Created October 4, 2012 21:04
Setup a minimal NSIncrementalStore subclass for learning / experimenting with MacRuby
framework 'CoreData'
# Create a minimal model
$thing = NSEntityDescription.alloc.init
$thing.name = 'Thing'
$model = NSManagedObjectModel.alloc.init
$model.entities = [$thing]
# Create the custom store class
class CustomStore < NSIncrementalStore
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
a = NSEntityDescription.new
a.name = 'A'
b = NSEntityDescription.new
b.name = 'B'
bs = NSRelationshipDescription.new
bs.name = 'bs'
bs.destinationEntity = b
bs.ordered = true
CGRect CGRectIntegralScaledEx(CGRect rect, CGFloat scale)
{
return CGRectMake(floorf(rect.origin.x * scale) / scale, floorf(rect.origin.y * scale) / scale, ceilf(rect.size.width * scale) / scale, ceilf(rect.size.height * scale) / scale);
}
CGRect CGRectIntegralScaled(CGRect rect)
{
return CGRectIntegralScaledEx(rect, [[UIScreen mainScreen] scale]);
}
@kastiglione
kastiglione / dynamic_rescue.rb
Last active December 14, 2015 12:08
Rescue on more than just exception type.
module UncaughtThrowError
def self.===(ex)
ArgumentError === ex && ex.message =~ /uncaught throw/
end
end
begin
throw :foo
rescue UncaughtThrowError
puts 'Success: Rescued UncaughtThrowError'
@kastiglione
kastiglione / gist:5475944
Last active December 16, 2015 18:09
Compose a signal corresponding to whether a source signal has sent an equal number of YESes and NOs.
[[[signal
// Map YES to 1, NO to -1
map:^(NSNumber* b) {
return @(b.boolValue ? 1 : -1);
}]
// Running sum of yes/1's and no/-1's
startWithScan:@0 combine:^(NSNumber* running, NSNumber* next) {
return @(running.intValue + next.intValue);
}]
// A 0-sum means equal number of YES/NO's; Map 0 to YES