Skip to content

Instantly share code, notes, and snippets.

@joeytrapp
joeytrapp / program_cache.php
Created March 31, 2011 18:31
Make generic the cache calls for a set of models.
<?php
/**
* Program Cache Behavior
*
* Behavior for making cache calls generic in the model layer. Assumes that the
* passed in ID is uuid.
*
* Things to change:
* Different method for handling directory building
* Support for cache configs other than the default (partially implemented)
@joeytrapp
joeytrapp / dynamic_find.php
Created April 5, 2011 03:56
PHP: CakePHP Dynamic Find Behavior
<?php
class DynamicFindBehavior extends ModelBehavior {
public $mapMethods = array(
'/^(find){1}[^(all|by)]{1}(.)+(by){1}(.)+$/' => '_find'
);
public function _find(&$model, $method, $cond = null) {
$fields = $ret = array();
$virtual = $_field = $_cond = false;
$tmp = array_merge(array_keys($model->schema()), array_keys($model->virtualFields));
@joeytrapp
joeytrapp / gist:1201000
Created September 7, 2011 16:14
Global ignore file
# Numerous always-ignore extensions
*.diff
*.err
*.orig
*.log
*.rej
*.swo
*.swp
*.vi
*~
@joeytrapp
joeytrapp / run-jasmine.coffee
Created March 28, 2012 04:42
CoffeeScript: PhantomJS Jasmine Scrapper
# Exit early if users don't pass at least 1 argument
if phantom.args.length < 1
console.log "Usage: phantomjs run-jasmine.coffee URL [timeout]"
phantom.exit()
# Collect the passed int args or set to default value
url = phantom.args[0]
timeout = phantom.args[1] or 6000
# Create the page object to load the test page in
@joeytrapp
joeytrapp / run-mocha.js
Created April 6, 2012 02:09
JavaScript: PhantomJS Mocha Scrapper
/*global phantom:true, console:true, WebPage:true, Date:true*/
(function () {
var url, timeout, page, defer;
if (phantom.args.length < 1) {
console.log("Usage: phantomjs run-mocha.coffee URL [timeout]");
phantom.exit();
}
url = phantom.args[0];
@joeytrapp
joeytrapp / path_compare.js
Last active October 14, 2015 01:28
Ember.js utility to generate a controller computed property that compares a string to a part of the currentPath. Used to create boolean properties that update on based on the state of the application.
Ember.PathCompare = {
comparePartsToString: function(parts, compare, index) {
var str, first;
if (index === undefined) {
index = parts.length - 1;
} else {
if (index < 0) {
index = (parts.length - 1) - (index * -1);
}
}
@joeytrapp
joeytrapp / gist:4693660
Last active December 12, 2015 01:48
How to get parent route models in the child route model() method.
App.Router.map(function() {
this.resource('posts', function() {
this.resource('post', { path: '/:post_id' }, function() {
this.resource('permissions', function() { ... });
});
});
});
App.PermissionsRoute = Ember.Route.extend({
model: function() {
@joeytrapp
joeytrapp / gist:5555232
Created May 10, 2013 15:38
Run a shell command and pipe the stdout to to parent process.
task :test do
cmd = 'shell command to run'
rd, wr = IO::pipe
pid = Process.fork do
$stdout.reopen(wr)
rd.close
exec(cmd)
end
wr.close
rd.each { |line| puts line }
@joeytrapp
joeytrapp / SeedShell.php
Last active December 27, 2015 10:39
Very basic SeedShell, with default and dev files along with ability to specify file manually. Also has a firstOrCreate helper to make creating seed records idempotent.
<?php
class SeedShell extends AppShell {
public $seedFile = 'seed.php';
public $seedDevFile = 'seed_dev.php';
public function main() {
$this->includeFile($this->absolutePath($this->getFile()));
}
battle = (function() {
function completelyAccurateBattleCalculator() {
return Math.floor((Math.random()*10)+1);
}
function predictWinner(i) {
if (i >= 1 && i < 5) {
return 'goku';
} else if (i > 5 && i <= 10) {
return 'jl';