Skip to content

Instantly share code, notes, and snippets.

View xslim's full-sized avatar

Taras Kalapun xslim

View GitHub Profile
def format_time_interval(from_time, to_time)
seconds = (to_time - from_time).abs.round
minutes, seconds = seconds.divmod(60)
if minutes > 60 * 24
" "
elsif minutes > 60
hours, minutes =
" %2dh %2dm " % minutes.divmod(60)
else
" %2dm %2ds " % [minutes, seconds]
class Account
include Mongoid::Document
include Mongoid::Timestamps
field :subdomain, :type => String
embeds_many :users
accepts_nested_attributes_for :users
validates_presence_of :name, :subdomain
validates_uniqueness_of :subdomain, :case_sensitive => false
@wmerrifield
wmerrifield / build+archive.sh
Created November 8, 2010 17:53
A shell script to perform the equivalent of Xcode's "Build & Archive" command.
#!/bin/sh
#
# Copyright (c) 2010 Warren Merrifield
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@assimovt
assimovt / user.rb
Created December 18, 2010 09:56
Railscast #236 extension with Fb_graph and Twitter
# app/models/user.rb
def apply_omniauth(omniauth)
case omniauth['provider']
when 'facebook'
self.apply_facebook(omniauth)
when 'twitter'
self.apply_twitter(omniauth)
end
authentications.build(hash_from_omniauth(omniauth))
@igrigorik
igrigorik / em-http-vcr.rb
Created January 30, 2011 04:56
Mocking with WebMock and VCR
require 'rubygems'
require 'test/unit'
require 'em-http'
require 'vcr'
VCR.config do |c|
c.cassette_library_dir = 'fixtures/vcr_cassettes'
c.http_stubbing_library = :webmock
end
+ (NSManagedObject *)managedObjectFromStructure:(NSDictionary *)structureDictionary
entityName:(NSString *)entityName
withManagedObjectContext:(NSManagedObjectContext *)context {
NSMutableDictionary *mutableStructureDictionary = [structureDictionary mutableCopy];
NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:entityName
inManagedObjectContext:context];
NSDictionary *relationshipsByName = [[managedObject entity] relationshipsByName];
@pnegri
pnegri / application_controller.rb
Created February 24, 2011 01:19
Trying to do a DRY multi tenancy with mongoid
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter do
# Find Account_ID by seach for a domain - NOT Refactored yet
host_id = $memcache_instance.get('hosts/' + request.host)
unless host_id
host_db_row = BimbooHost.where( :host => request.host ).one
if host_db_row
$memcache_instance.set('hosts/' + request.host, host_db_row.bimboo_account_id, 60*60)
@zbyhoo
zbyhoo / solve_pbxproj_merge_conflict.sh
Created May 5, 2011 09:00
Solving pbxproj files git merge conflicts when two users add files at the same time.
#!/bin/sh
projectfile=`find -d . -name 'project.pbxproj'`
projectdir=`echo *.xcodeproj`
projectfile="${projectdir}/project.pbxproj"
tempfile="${projectdir}/project.pbxproj.out"
savefile="${projectdir}/project.pbxproj.mergesave"
cat $projectfile | grep -v "<<<<<<< HEAD" | grep -v "=======" | grep -v "^>>>>>>> " > $tempfile
cp $projectfile $savefile
@lukeredpath
lukeredpath / ExampleClass.m
Created June 30, 2011 22:18
Macro for creating your "shared instance" using GCD
@implementation MySharedThing
+ (id)sharedInstance
{
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
return [[self alloc] init];
});
}
@end
//
// VKBlockSelector.h
//
#include <objc/runtime.h>
#import <Foundation/Foundation.h>
/*
* note: each file has its own!
*/