query: tag:patch

JRubyで相対パスを含む場合に、Jarファイルの中のファイルを読めるようにするためのモンキーパッチの最新版です。

ruby>>
def cleanup_path(path)
if path.to_s.match(/^file:/) && path.is_a?(String)
jar_path, inner_path = path.split('!', 2)
inner_path = File.expand_path(inner_path)
path = [jar_path, inner_path].join('!')
end
path
end

alias :require_original :require
def require(path)
require_original cleanup_path(path)
rescue Exception => e
raise e unless path.to_s.match(/^file:/)
end

alias :load_original :load
def load(path)
load_original cleanup_path(path)
rescue Exception => e
raise e unless path.to_s.match(/^file:/)
end

class Dir
class << self
alias :aref_original :[]
def
aref_original *(args.map{|path| cleanup_path(path)})
end
end
end
class File
class << self
alias :mtime_original :mtime
def mtime(path)
if path.match(/^file:/)
jar_file, = path.split('!', 2)
path = jar_file.sub(/^file:/, '')
end
mtime_original(path)
end
end
end
<<--

posted by genki genki on Tue 21 Apr 2009 at 02:41 with 0 comments

config/environment.rbの中で、config.gemによってversion指定したgemを、
rake gems:unpackしようとすると、以下のようなエラーが出るようです。

pre>>
% rake gems:unpack GEM=genki-pagination_scope
(in /Users/takiuchi/project/formula)
ERROR: While executing gem ... (ArgumentError)
Illformed requirement ["">= 0.0.4""]
<<--

RailsのLightHouseでも報告されていました。

**rake gems:unpack version handling broken **

ということで、この問題を回避するために、上記Ticketに添付されていた
Patchを参考に、以下のようなMonkeyPatchを作ってみました。

config/initializers/fix_gem_unpack_for_2_1_1.rb

ruby>>
module Rails
class GemDependency
def unpack_to(directory)
FileUtils.mkdir_p directory
Dir.chdir directory do
Gem::GemRunner.new.run(
unpack_command.map {|i| i.gsub(/\A["']|["']\z/, '')})
end

  spec_filename = File.join(gem_dir(directory), '.specification')
  File.open(spec_filename, 'w') do |file|
    file.puts specification.to_yaml
  end
end

end
end
<<--

これをconfig/initializers/の下に置いておけば、とりあえず正常にunpackする事ができます。

posted by genki genki on Wed 24 Sep 2008 at 01:54 with 0 comments

named_scopeは大変素晴らしいRailsの新しい機能ですが、
おなじみのwith_scopeのように、
スコープつきのブロックを伴った利用ができないという問題がありました。
例えばこんな感じに利用しようとしてもうまくいきません。

ruby>>
User.active do
User.count # => Not run
end

User.active.with_scope do
User.count #=> Not scoped
end
<<ruby

そこで、昔ながらのwith_scopeと同じようにnamed_scopeを使えるようにする
Gemプラグインを作りました。

with_named_scope

これを使うと、以下のように期待通りに動きます。

ruby>>
User.active.with do
User.count #=> User.active.count
end
<<--

なかなか便利だと思うので、Rails本家にパッチを送っておきました。

Improved named_scope to be used like as with_scope

posted by genki genki on Fri 1 Aug 2008 at 21:09 with 0 comments

Gitでpatchを作成するには、git-format-patchコマンドを使うと便利です。
メール送信用に整形された状態でpatchが作成されます。
こんな感じでリビジョンを指定して実行します。

pre>>
% git-format-patch -r HEAD~
<<--

0001-Changed-capitalization-of-exts-of-header-files-of-FL.patchのようなファイル名でpatchが作成されます。

作成されたpatchはメール送信用のヘッダ情報などを含んでいますが、
メール送信せずにローカルでgit-am
コマンドを使ってpatchを当てることができます。

pre>>
% git-am ../path/to/patch/0001-Changed-capitalization-of-exts-of-header-files-of-FL.patch
<<--

posted by genki genki on Thu 24 Jul 2008 at 00:25 with 0 comments

lighttpd-1.5.xから導入される予定のmod_deflateは、
static/dynamicを問わず、レンダリング出力を
圧縮してくれる便利なモジュールですが、
残念ながらlighttpd-1.4.x系では使うことができません。

しかし、非常に便利な機能なので、1.5.x系から
バックポートするための様々な試みがなされています。
今回は、lighttpd-1.4.8にパッチを当てて
mod_deflateを使う方法を紹介します。

以下のようにlighttpd-1.4.8のソースと、パッチを
ダウンロードし、パッチを適用します。

shell>>

cd /usr/local/src

wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2

tar xvjf lighttpd-1.4.18.tar.bz2

cd lighttpd-1.4.18

curl http://poocs.net/files/lighttpd-1.4.18.mod_deflate.scoop.patch.gz | gzip -d | patch -p1

<<--

あとは、通常通りソースからインストールすれば完了です。

shell>>

./configure --with-bzip2

~ (snip) ~

enabled:
auth-crypt
compress-bzip2
compress-deflate

~ (snip) ~

make

make install

<<--

mod_deflateの使い方は、こちらのサイトをご覧ください。

posted by genki genki on Sun 13 Jan 2008 at 07:43 with 0 comments

cache_fuやmemcachedを使うときに、cacheメソッドがオプションを
受け取ってくれないので不便だなと思っていたのですが、
EdgeRailsでは以下のようなパッチが当たっていて
この問題は解決されているようです。

[PATCH] Rails have poor support to work with memcached

Let's assume we have a caching installation with Nginx used as a frontend. Nginx attempts to get page body by key from memcached. If it succeeds (meaning that the page is in memcached), Rails doesn't get control, making this schema extremely fast, up to 3K requests per second. In case memcached doesn't contain cached page by url as a key, Rails receive control, render page and cache it in memcached in order for the next request to be retrieved from memcached.

現状のリリースバージョンのコードでは、以下のようになっています。

vendor/rails/actionpack/lib/action_view/helpers/cache_helper.rb

rails>>
def cache(name = {}, &block)
@controller.cache_erb_fragment(block, name)
end
<<--

これが、EdgeRailsでは以下のように変更されています。

vendor/rails/actionpack/lib/action_view/helpers/cache_helper.rb

rails>>
def cache(name = {}, options = nil, &block)
template_extension = first_render[/.(\w+)$/, 1].to_sym

case template_extension
when :erb, :rhtml
@controller.cache_erb_fragment(block, name, options)
when :rjs
@controller.cache_rjs_fragment(block, name, options)
when :builder, :rxml
@controller.cache_rxml_fragment(block, name, options)
else
# do a last ditch effort for those brave souls using
# different template engines. This should give plugin
# writters a simple hook.
unless @controller.respond_to?("cache_#{template_extension}_fragment")
raise "fragment caching not supported for #{template_extension} files."
end

@controller.send!("cache_#{template_extension}_fragment", block, name, options)

end
end
<<--

posted by genki genki on Fri 11 Jan 2008 at 05:52 with 0 comments

Railsアプリケーションなどで、lighttpdを使って開発していると、
tmp/sockets以下にソケットファイルが生成されます。

これがあると、
以前紹介したRak
でエラーが発生して
上手く検索することが出来なくなる問題があったので、
対処方法を紹介します。

ソースコードを修正する必要があるので、
Rubyforgeから tar ball を持ってきます。

これを展開して、以下のようにファイルを書き換えます。

bin/rak

diff>>
--- rak_original 2007-12-26 20:48:26.440270979 +0900
+++ bin/rak 2007-12-26 20:43:56.736258421 +0900
@@ -464,6 +464,8 @@
end
return false
end

  • rescue Errno::ENXIO
  • false

end

def self.search2(str, files)
<<--

あとは、rake install_gem すればOKです。

作者のDanには、メールでPatchを送っておきました。

posted by genki genki on Wed 26 Dec 2007 at 21:05 with 0 comments