Skip to content

Instantly share code, notes, and snippets.

View morimori's full-sized avatar

Takatoshi MORIYAMA morimori

View GitHub Profile
@morimori
morimori / date_last_and_next.rb
Created April 2, 2009 06:57
Date と Time に直前と直後の各曜日を取得するメソッドを追加
class Date
module Extender
module LastAndNext
['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'].each_with_index do |weekday, index|
define_method "last_#{weekday}" do
return self if wday == index
offset = wday - index
self - (offset < 0 ? offset + 7 : offset)
end
define_method "next_#{weekday}" do
@morimori
morimori / secuedit.sh
Created April 6, 2009 11:18 — forked from krayinc/lock.sh
ファイルを作成・編集する際に、ロード前の復号、セーブ後の暗号化を自動で行う
#! /bin/sh
if [ -z "$EDITOR" ]; then
echo "EDITOR is not set."
exit 1
fi
TMPFILE="${TMPDIR}secuedit.$$"
NEW=0
@morimori
morimori / html_exception.rb
Created April 16, 2009 13:03
HTTP のステータスコードに対応した例外クラス
module HTTP
class BaseError < StandardError; end
class ClientError < BaseError; end
class BadRequest < ClientError; end
class Unauthorized < ClientError; end
class PaymentRequired < ClientError; end
class Forbidden < ClientError; end
class NotFound < ClientError; end
class MethodNotAllowed < ClientError; end
@morimori
morimori / NP_CustomFilter.php
Created April 17, 2009 17:38
要素にユーザ指定の関数を適用する Nucleus plugin
<?php
class NP_CustomFilter extends NucleusPlugin {
function getName() { return 'NP_CustomFilter'; }
function getAuthor() { return 'Takatoshi -morimori- MORIYAMA'; }
function getURL() { return 'http://kray.jp/'; }
function getVersion() { return '1.0'; }
function getDescription() { return 'in template: &lt;%CustomFilter(body,\'"<strong>".%s."</strong>"\')%&gt;'; }
function supportsFeature($what) {
switch($what) {
@morimori
morimori / tail.rake
Created April 23, 2010 13:38
RAILS_ENV からログを判別して tail -f するタスク
namespace :log do
desc "tail -f log/#{RAILS_ENV || 'development'}.log"
task :tail do
sh "tail -f log/#{RAILS_ENV || 'development'}.log" rescue RuntimeError
puts
end
end
=begin
http://doruby.kbmj.com/hogehoge/20100330/Ruby1.9_Rails_#p4
ここの ActionController 用のパッチを Rails3 に対応させたもの
multipart の場合に正常に動くかは未確認
=end
module ActionDispatch
module Http
module Parameters
private
@morimori
morimori / 1.9.3-p0.log
Created November 1, 2011 07:29
ruby Digest::* benchmark
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
Rehearsal ------------------------------------------
MD5 1.010000 0.000000 1.010000 ( 1.026340)
SHA1 1.710000 0.000000 1.710000 ( 1.724464)
SHA2 3.780000 0.000000 3.780000 ( 3.824757)
SHA256 3.460000 0.010000 3.470000 ( 3.498111)
--------------------------------- total: 9.970000sec
user system total real
MD5 1.020000 0.000000 1.020000 ( 1.025751)
@morimori
morimori / pre-commit
Created December 14, 2011 08:28
Jasmine と RSpec を実行する git pre-commit hook
#! /bin/sh
# Run all specs
git stash --keep-index
RAILS_ENV=test bundle exec rake assets:precompile jasmine:headless assets:clean spec
result=$?
git stash pop
[ $result -ne 0 ] && exit $result
@morimori
morimori / build-centos6-32bit-s3backed-ami.sh
Created January 17, 2012 03:59
CentOS6 (32bit, S3-Backed) の AMI をゼロから作って登録する
#! /bin/sh
# 0. 設定
RELEASE=1.kray
ARCH=i386
EC2_PRIVATE_KEY=/path/to/private_key.pem
EC2_CERT=/path/to/certificate.pem
S3_BUCKET=bucket-name
AWS_ACCOUNT_NUMBER=012345678910
AWS_ACCESS_KEY=youraccesskey
@morimori
morimori / sqlite_optimizer.sh
Created April 9, 2012 04:25
optimize sqlite database
find -type f -print0 | xargs -0 file | grep SQLite | awk -F : '{print $1}' | xargs -n1 -d "\n" sqlite_optimizer.sh