Skip to content

Instantly share code, notes, and snippets.

View ackintosh's full-sized avatar
🎯
Focusing

Akihito Nakano ackintosh

🎯
Focusing
View GitHub Profile
@ackintosh
ackintosh / gist:5699747
Created June 3, 2013 17:26
Observer pattern in PHP with SplObjectStrage.
<?php
class Subject
{
private $observers;
public $message;
public function __construct()
{
$this->observers = new SplObjectStorage();
}
@ackintosh
ackintosh / gist:5684693
Created May 31, 2013 12:32
Interpreter pattern in Ruby.
require 'find'
# Interpreter pattern in Ruby
# for search file
class Expression
def |(other)
Or.new(self, other)
end
@ackintosh
ackintosh / gist:5666911
Created May 28, 2013 23:24
Builder pattern in Ruby
# -*- encoding: utf-8 -*-
class Computer
attr_accessor :display, :motherboard
attr_reader :drives
def initialize(display = :crt, motherboard = Motherboard.new, drives = [])
@display = display
@motherboard = motherboard
@drives = drives
@ackintosh
ackintosh / gist:5488231
Created April 30, 2013 11:52
File upload in FuelPHP
<!-- app/views/files/select.php -->
<p>Select</p>
<?php if (isset($html_error)): ?>
<?= $html_error; ?>
<?php endif; ?>
<?= Form::open(array('action' => 'files/upload', 'enctype' => 'multipart/form-data')); ?>
<div>
<?= Form::label('ファイル', 'uploadfile'); ?>
@ackintosh
ackintosh / gist:5480241
Created April 29, 2013 07:45
custom configuration file for FuelPHP
<?php
/* fuel/app/config/profile.php */
return array(
'name' => 'akihito',
'twitter' => 'NAKANO_Akihito',
);
@ackintosh
ackintosh / gist:5479927
Last active December 16, 2015 18:48
custom validation rule for fuelPHP
<?php
/* fuel/app/classes/myvalidations.php */
class MyValidations
{
// Method name must start at '_validation_'
public static function _validation_something_check($value)
{
return $value ? true : false;
}
}
@ackintosh
ackintosh / gist:5456690
Created April 25, 2013 00:32
Dynamically adding methods in PHP
<?php
class DynamicMethod
{
protected $dynamic_methods = array();
public function __call($name, $args)
{
if ($this->dynamic_methods[$name]) {
return call_user_func_array($this->dynamic_methods[$name], $args);
@ackintosh
ackintosh / gist:5431663
Created April 22, 2013 00:10
Currying and Partial application in Ruby
# see also
# http://ja.wikipedia.org/wiki/%E3%82%AB%E3%83%AA%E3%83%BC%E5%8C%96
# before
div = -> x, y { x / y }
p div.call(6, 2)# 6
# currying
cdiv = -> x { return -> y { x / y } }
@ackintosh
ackintosh / gist:5431584
Created April 21, 2013 23:40
Currying in PHP
<?php
// before
function talk($person, $dialogue)
{
echo "{$person} 「{$dialogue}」" . PHP_EOL;
}
talk('Bob', 'Hi');
talk('Alice', 'Hi');
/*
* Bob 「Hi」
@ackintosh
ackintosh / gist:5388678
Created April 15, 2013 14:52
get title, keywords, description using Anemone.
# -*- encoding: utf-8 -*-
require 'anemone'
Anemone.crawl('http://www.example.jp') do |anemone|
anemone.on_every_page do |page|
puts page.url
puts page.doc.xpath('//head/title/text()').first.to_s if page.doc
puts page.doc.xpath("//meta[@name='keywords']/@content") if page.doc
puts page.doc.xpath("//meta[@name='description']/@content") if page.doc
end