Skip to content

Instantly share code, notes, and snippets.

@a2ikm
a2ikm / gist:1315920
Created October 26, 2011 09:47
Make Ruby Class Inheritance Tree
#
# originated from http://stackoverflow.com/questions/2393697/look-up-all-descendants-of-a-class-in-ruby
#
class Class
def descendants
result = {}
ObjectSpace.each_object(::Class) {|klass| result[klass] = klass.descendants if klass < self }
result
end
end
@a2ikm
a2ikm / before_only_filter.rb
Created October 27, 2011 12:57
before xxx :only => xxx みたいなやつ
# before xxx :only => xxx みたいなやつ
# http://blog.alastairdawson.com/2010/07/27/a-sinatra-before-only-filter/
# 若干書き換えて[]を不要にした
module Sinatra
module BeforeOnlyFilter
def before_only(*routes, &block)
before do
routes.map!{|x| x = x.gsub(/\*/, '[^/]')}
routes_regex = routes.map{|x| x = x.gsub(/\//, '\/')}
@a2ikm
a2ikm / listup_mongoid_fields_for_i18n.rb
Created November 24, 2011 07:33
list up mongoid fields for i18n
PATH_TO_MODELS = "path/to/models/"
class Module
def descendants
result = {}
ObjectSpace.each_object(::Class) {|klass| result[klass] = klass.descendants if klass < self }
result
end
end
@a2ikm
a2ikm / gist:1484133
Created December 16, 2011 02:23
incr/decr
module ActiveRecord
class Base
class <<self
def quote_column_name(column_name)
"#{quoted_table_name}.#{connection.quote_column_name(column_name)}"
end
end
def incr(attribute, by)
quoted_attribute = self.class.quote_column_name(attribute.to_s)
self.class.where(id: id).update_all("#{quoted_attribute} = #{quoted_attribute} + #{by.to_i}")
@a2ikm
a2ikm / check_emoji_conflicts.rb
Created January 10, 2012 08:49
Check conflicts of emoji codepoints in iOS5 and softbank.
# -*- coding: utf-8 -*-
UNIFIED_EMOJI = %w(
U+2600
U+2601
U+2614
U+26C4
U+26A1
U+1F300
U+1F301
@a2ikm
a2ikm / gist:1588660
Created January 10, 2012 11:57
concat transparent images of emoji-css-builder
# montage seems not to keep transparency.
# So I resized images with sips, and concated with convert.
Dir.glob("assets/iphone/*.png").each do |path|
out1 = File.dirname(path)
out2 = File.basename(path)
out = "#{out1}/xxx_#{out2}"
%x(sips --resampleHeightWidth 20 20 --out #{out} #{path})
end
@a2ikm
a2ikm / p4d-20120129.txt
Created January 29, 2012 08:23
SELECT系SQLでできることとかActiveRecordでできること - p4dでの資料
SELECT系SQLでできることとかActiveRecordでできること
# people
* name : 名前(文字列)
* age : 年齢(整数)
| id | name | age |
| 1 | John | 18 |
| 2 | Ben | 27 |
@a2ikm
a2ikm / from.html
Created March 26, 2012 12:27
postMessage sample
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
</head>
<body>
<input id="foo" type="button" value="foo" />
<iframe src="http://192.168.11.19:8001/to.html" id="proxy"></iframe>
<script>
window.callbacks = {
@a2ikm
a2ikm / gist:2225502
Created March 28, 2012 11:18
log all queries to table with mysql
# output to mysql.general_log
# if you want mysql to log to file, set it as `FILE`, or `FILE,TABLE`
log-output=TABLE
general_log=1
@a2ikm
a2ikm / getbijin.rb
Created July 26, 2012 03:17
get bijin-tokei pictures
#!/usr/bin/env ruby
# coding: utf-8
require "digest/sha1"
require "fileutils"
BASE_URL = "http://www.bijint.com/%{location}/tokei_images/%{time}.jpg"
BASE_DIR = File.expand_path("~/tmp/bijin/%{location}")
TEMP_PATH = File.join(BASE_DIR, "%{time}.jpg")
SAVE_PATH = File.join(BASE_DIR, "%{time}_%{digest}.jpg")