Skip to content

Instantly share code, notes, and snippets.

View speedmax's full-sized avatar

Taylor luk speedmax

View GitHub Profile
@speedmax
speedmax / seed.rake
Created April 23, 2009 02:13
seed data rake task to load and dump from your current database, everything stored in yaml
require 'FileUtils'
namespace :seed do
desc 'Dump a database to yaml fixtures. Set environment variables DB
and DEST to specify the target database and destination path for the
fixtures. DB defaults to development and DEST defaults to RAILS_ROOT/
db/fixtures.'
task :dump => :environment do
path = ENV['DEST'] || "#{RAILS_ROOT}/db/fixtures"
db = ENV['DB'] || 'development'
<?php
class P extends Object {
static $something;
static function foo() {
return __METHOD__;
}
function bar() {}
}
$c = _class('P');
<?php
/*
Backward compat version of get_called_class
*/
if (!function_exists('get_called_class')):
function get_called_class() {
$bt = debug_backtrace();
$lines = file($bt[1]['file']);
preg_match('/([a-zA-Z0-9\_]+)::'.$bt[1]['function'].'/', $lines[$bt[1]['line']-1], $matches);
return $matches[1];
@speedmax
speedmax / 1. Person and hobbies.php
Created March 31, 2009 02:47
concept for better active_record in CakePHP
<?php
class Person extends Model {
var $hasMany = 'hobbies';
}
class Hobby extends Model {
var $belongsTo = 'person';
}
echo Person::create(array(
class Openbet_Collection {
static function fromArray($array, $class) {
$instance = new Openbet_Collection;
foreach ($list as $item) {
$instance[] = new $class($item);
}
return $instance;
}
}
<?php
/* Blocks */
$list->reduce(array(), '|$rs, $e|
$rs[$e->name] = $e->price;
$rs;
');
/* Yielding
<?php
function extend($object, $class) {
$attr = 'methods';
$$attr = get_class_methods($class);
eval($object.'::$'.$attr.' = $methods;');
}
class Object {
static $methods = array();
function evaluate_block($block, $arguments = array()) {
# implict return
$lines = explode(';', $block);
$last =& $lines[count($lines)-2];
if (strpos($last, 'return') === false) {
$last = 'return '.$last;
}
$block = join(';', $lines);
if (isset($arguments['this'])) {
require 'FileUtils'
namespace :seed do
desc 'Dump a database to yaml fixtures. Set environment variables DB
and DEST to specify the target database and destination path for the
fixtures. DB defaults to development and DEST defaults to RAILS_ROOT/
db/fixtures.'
task :dump => :environment do
path = ENV['DEST'] || "#{RAILS_ROOT}/db/fixtures"
db = ENV['DB'] || 'development'
<?php
/**
* Patch isset($model->attr) problem for this
* php library http://lukebaker.org/projects/activerecord-in-php/
*/
class ActiveRecord {
function __isset($attr) {
try {
$value = $this->$attr;