Skip to content

Instantly share code, notes, and snippets.

View matsuda's full-sized avatar

Kosuke Matsuda matsuda

  • Tokyo, Japan
View GitHub Profile
@matsuda
matsuda / password_validation.rb
Created August 16, 2010 08:23
「英大文字」「英小文字」「数字」「記号」のうち、単一の1種類のみで構成されることなく、2種類以上が使われていること
# 「英大文字」「英小文字」「数字」「記号」のうち、単一の1種類のみで構成されることなく、2種類以上が使われていること
# http://e-words.jp/p/r-ascii.html
class User
PASSWORD_FORMAT = %r{^
(?![0-9])[\x20-\x7e]*?[0-9][\x20-\x7e]*?|
(?![a-z])[\x20-\x7e]*?[a-z][\x20-\x7e]*?|
(?![A-Z])[\x20-\x7e]*?[A-Z][\x20-\x7e]*?|
(?![!-\/:-@\[-`\{-~])[\x20-\x7e]*?[!-\/:-@\[-`\{-~][\x20-\x7e]*?
$}x
@matsuda
matsuda / new_rails_with_version
Created September 6, 2010 05:26
railsコマンドでバージョンを指定してプロジェクトを作成する
$ rails _2.3.9_ test_2_3_9
class String
def half_spaceize
self.gsub(/\s| /, ' ')
end
def half_spaceize!
self.gsub!(/\s| /, ' ')
end
# '-ー‐-' => '-'
def hyphenize
self.gsub(/\xE3\x83\xBC|\xE2\x80\x90|\x2D/, "\xEF\xBC\x8D")
@matsuda
matsuda / call_helper_method_from_controller
Created October 15, 2010 06:53
コントローラーからヘルパーを呼ぶ
ApplicationController.helpers.number_with_delimiter('10000')
@matsuda
matsuda / url_for_with_jpmobile
Created October 21, 2010 03:40
携帯で link_to (url_for) のパラメータに日本語が含まれている場合に、適切な文字コードに変換する
module ActionView
module Helpers
module UrlHelper
def url_for_with_jpmobile(options = {})
if !options.nil? && options.is_a?(Hash)
if request && (mobile = request.mobile) && !(mobile.instance_of?(Jpmobile::Mobile::Vodafone)||mobile.instance_of?(Jpmobile::Mobile::Softbank))
options = options.inject({}){ |result, option|
result[option[0]] = convert_multibyte_with_mobile(option[1])
result
}
@matsuda
matsuda / array.rb
Created December 13, 2010 02:27
Hashの値もflattenする
class Array
def flatten_with_hash
inject([]){ |r, h|
if h.is_a?(Hash)
r << h.keys.flatten_with_hash
r << h.values.flatten_with_hash
else
r << h
end
r
@matsuda
matsuda / array.rb
Created January 5, 2011 06:00
hash_with_underscore_key is only converting key on Hash into underscore.
class Array
def to_underscore_key
self.class.new.tap do |array|
self.each { |value|
case value
when Hash, Array
array << value.to_underscore_key
else
array << value
end
@matsuda
matsuda / git_clone_port_forwarding
Created January 12, 2011 07:03
git clone with port forwarding
# ポートフォワード
$ ssh -L 10000:remote.host.com:22 gate.com -p 11122
# clone
$ git clone git+ssh://git@localhost:10000/repo/project.git
@matsuda
matsuda / action_dispatch_ext.rb
Created January 17, 2011 05:46
Rails-3.0.3 + jpmobile-0.1.4 ( + devise-1.1.5 ) でCookie非対応の携帯でログインできるようにするパッチ
#
# config/initializers配下やlib配下においてinclude
#
if defined?(Jpmobile)
class ActionDispatch::Request
def reset_session_with_jpmobile
reset_session_without_jpmobile
if self.mobile?
self.session_options[:id] = ActiveSupport::SecureRandom.hex(16)
if Rails.application.config.session_store == ActiveRecord::SessionStore
# $ cd config/initializers && wget https://gist.github.com/raw/818098/8bb0db2303e86629166cecca8c57699690de58c9/deprecated_active_view.rb
# $ vi ~/.subversion/config
# [miscellany]
# global-ignores = deprecated_active_view.rb
#
ActionView.class_eval do
remove_const :SafeBuffer
SafeBuffer = ActiveSupport::SafeBuffer
end