Skip to content

Instantly share code, notes, and snippets.

@sunaot
sunaot / static_variable.php
Created August 15, 2011 10:14
how static variable works in PHP.
<?php
class Base
{
static public $name = 'BASE';
function stamp($name) {
static::$name = $name;
}
}
class A extends Base
@sunaot
sunaot / how_to_access_static_private_variable_destructively.php
Created August 15, 2011 11:04
Accessing static private variable hack using closure.
<?php
class Sample
{
static private $value = 0;
static function privateHack() {
$hack =& static::$value;
return function($val) use (&$hack) {
$hack = $val;
};
}
@sunaot
sunaot / dsl_sample.php
Created August 15, 2011 11:51
DSL in PHP sample. See Entity::customize method.
<?php
class HackObject
{
public $_entityName;
public $_attributeNames;
function entityName($val) { $func = $this->_entityName; $func($val); }
function attributeNames($val) { $func = $this->_attributeNames; $func($val); }
}
class EntityBase
@sunaot
sunaot / groupBy.sql
Created October 6, 2011 15:54
Haskell の groupBy の動作を誤解してるときに SQL で書いてみたそれっぽい動きをするもの
-- id num id+1 num
-------------------------
-- 1 1 2 2
-- 2 2 3 3
-- 3 3 4 4
-- 4 4 5 5
-- 5 5 6 6
-- 6 6 7 7
-- 7 7 8 8
-- 8 8 9 9
@sunaot
sunaot / designing_testable_module_in_MVC.md
Created October 11, 2011 10:33
MVC な WAF でテストを書くときの設計方針

MVC な WAF でテストを書くための設計について

Controller から DB アクセスをしない

  • Controller が DB アクセスをするのを許すと、フローのテストが遅くなる
    • DB アクセスが必要な処理は Model へ追いやる
    • QUERY_STRING, ENV あたりを入力とする Controller のフローのテストと、DB や memcached を入力 とする Model のテストは分離するとテストしやすい
    • Controller と Model, View などの API の切れ目で抽象化してテスト可能にしておくとやりやすいので、 切れ目をフレームワークで強制したり、運用ルールで縛ると最低限のテスト可能性が確保された設計に
@sunaot
sunaot / coding_guideline.md
Created November 9, 2011 02:11
コーディングガイドライン

コーディング指針

基本方針

こんなコードはよいコード

  • 読みやすいことはよいことだ
    • 見た目が単純だと読みやすい
    • 皆がよく知っている構成は読みやすい (標準に従う。すでに知っているもの (Unix のコマンドとか) に従う。すでにあるものに従う)
  • 書かないことはよいことだ
@sunaot
sunaot / ycon.rb
Created November 18, 2011 11:59
Ruby で Y コンビネータを写経してみた
#! ruby
# coding: utf-8
#
# http://d.hatena.ne.jp/nowokay/20090409#1239268405
require 'expectations'
Expectations do
# boolean
t = (-> x { (-> y {x}) })
@sunaot
sunaot / install_git.sh
Created December 1, 2011 02:20
自分の $HOME に git をインストールするスクリプト
#!/bin/bash
# .bashrc か .bash_profile に
#
# export PATH=$HOME/libexec:$PATH
#
# を追記しておく (既存の export PATH があるなら $HOME/libexec を追加) 。
src_path=$HOME/.src
exec_dir=libexec
package_dir=pkg
@sunaot
sunaot / install_tig.sh
Created December 1, 2011 02:30
自分の $HOME に tig をインストールするスクリプト
#!/bin/bash
# .bashrc か .bash_profile に
#
# export PATH=$HOME/bin:$PATH
#
# を追記しておく (既存の export PATH があるなら $HOME/bin を追加) 。
src_path=$HOME/.src
exec_dir=bin
package_dir=pkg
@sunaot
sunaot / builder.rb
Created January 20, 2012 18:41
sample builder script in Ruby
module Builder
def self.method_missing name, *args, &block
tag = Tag.new
tag.send(name, *args, &block).flatten.join
end
class Tag
def initialize
@tags = []
end