Rails に 'aws-s3' 組み込むと最後に Bucket がついてるクラスが動かなくなるやつ

Gemfile に書いて bundle install だけしてから別の対応してたから3時間ぐらいはまった。

原因

解決

# config/initializer/s3.rb
class Module
  alias_method :const_missing, :const_missing_not_from_s3_library
end

こういうの lib 下に置くか initializer 下に置くか迷います

fluent-plugin-typecast リリース

一個前のエントリーで mongo に入れる時に型変換するプラグイン作ったけど、tag_mapped な時に事前に型変換しておきたい事例だとあんまり使えないこれ。

他にも設定次第で同じようなことできるプラグインはあるけど、一応自作しとくかってとこで型変換するだけのプラグインを作ってみた。

https://rubygems.org/gems/fluent-plugin-typecast

設定例はこんなかんじで。

<match foo.bar>
  item_types test1:integer,test2:string,test3:time,test4:bool
  time_format %d/%b/%Y:%H:%M:%S %z
  prefix typed
</match>

<match typed.foo.bar>
  type mongo
  database fluent_typed
  collection test
</match>

fluent-plugin-mongo-typed の方はコードも被ってるしそのうち消しますたぶん。 mixinとして切り出そうと思ったけど、↑のプラグインがあったら必要なさそうなのでそれもしてません。

fluent-plugin-mongo-typed リリース

https://rubygems.org/gems/fluent-plugin-mongo-typed

某所で必要になったので作りました。

fluentd から mongo に突っ込む時に簡単に型付けできるようにするプラグインです。

こういう形で自分のコード公開するの始めてなので嬉し恥ずかしなんですがこれから適当に慣れて行きたいです。

ドキュメント書いてなかったりテスト足りてなかったりライセンス表記の整合がとれてなかったりするのでちょいちょい直していきます。

引数が長い時の改行

コーディング規約らへんの話。タイトルに引数長い時って書いたけど配列とかマップ初期化する時も同じ。

twitterに書いたんだけど最近のtwitter(前からだっけ?)はWebで見ると改行がスペース扱いになるっぽいので詳しくブログに書き直し。

正直、何でもいいんだけど自分の好みと好む理由ぐらいはどっかに書いといた方が説明する時「ここ見て」で済みそうだから書いとく。

しばらくの間 Ruby 書くことが多くなってるから Ruby に影響された部分は否めないです。 有名どころのRubyコーディング規約とかは守ったことないので知らんです。

1. 自分が書く時

上から下にざーっと眺めてけば読める縦に長くて横に短いコードが好きなのでこんな感じ。

終点分かりやすいし、引数の最後の,(カンマ)が許されてる言語だと追加もしやすいしってところ。

hoge(
  'aaa',
  'bbb',
)

2. もやっとするやつ

  1. のやつは最後の括弧の分だけで行が増えるのが嫌という意見を聞いたことがある。 なんで行が増えるのが嫌なんだろうって疑問が沸いたけど、その時は面倒だったからスルーした。
hoge(
  'aaa',
  'bbb')

3. 正直見たら直したいやつ

横に長くなりがちだし 、1. で書いた利点活かせないし、「こういう書き方する理由ってなんなの?」って正直聞きたいんで誰か教えてください。 デフォルトのフォーマッターがそうなってるからってのなしで。

hoge('aaa',
     'bbb')

4. 最近周囲で見かけてないやつ

関数名の横に ( があった方がその関数の始点であることが明確だと思うんです

hoge
(
  'aaa',
  'bbb'
)

RhodeCode 1.4.4 on CentOS 6.3

久しぶり過ぎて記法忘れたけど今は Markdown とか使えるんですね。記法分からんところがあるので後で整えます。

CentOS 6.3 に RhodeCode 1.4.4 入れた時にいくつかつっかかったのでメモというか隣の人に書けと言われたので書いときます。

むしろ入れたのは隣の人で自分は問題を解決しただけなので実はインストール手順分からないです。

1. 日本語コミットメッセージが文字化ける

$rhodecode_path/env.rhodecode に追記

export HGENCODING="UTF-8"

2. git の repository に push できない

問題

rhodecode.lib.vcs.exceptions.RepositoryError: Couldn't run git command (git -c core.quotepath=false for-each-ref --format='%(refname)' 'refs/heads/*').

こんなの出て push できない。

git に -c オプションが実装されたのは 1.7.4 で CentOS 6.3 にデフォルトで入ってるのが 1.7.1 なので動かないらしーです。

https://groups.google.com/forum/?fromgroups=#!topic/rhodecode/b-16MaCBDTo

解決

rpmforge とかでインストールする

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
rpm -K rpmforge-release-0.5.2-2.el6.rf.*.rpm
rpm -i rpmforge-release-0.5.2-2.el6.rf.*.rpm
yum erase git
yum install --enablerepo=rpmforge,rpmforge-extras git 

http://wiki.centos.org/AdditionalResources/Repositories/RPMForge#head-f0c3ecee3dbb407e4eed79a56ec0ae92d1398e01

3. git の branch が削除できない?

実は 2. より先に見つけて潰したのでこれに引っかかったかどうかは検証してないけど一応書いとく。

ここらへんを参考に直した

diff

*** lib/celerylib/tasks.py.bak  2012-11-13 17:48:08.243682357 +0900
--- lib/celerylib/tasks.py      2012-11-13 17:50:00.543679086 +0900
***************
*** 403,412 ****
      backend = get_backend(repo_type)

      if repo_type == 'git':
!         backend(safe_str(destination_fork_path), create=True,
                  src_url=safe_str(source_repo_path),
                  update_after_clone=update_after_clone,
                  bare=True)
      elif repo_type == 'hg':
          backend(safe_str(destination_fork_path), create=True,
                  src_url=safe_str(source_repo_path),
--- 403,413 ----
      backend = get_backend(repo_type)

      if repo_type == 'git':
!         r = backend(safe_str(destination_fork_path), create=True,
                  src_url=safe_str(source_repo_path),
                  update_after_clone=update_after_clone,
                  bare=True)
+         ScmModel().install_git_hook(repo=r)
      elif repo_type == 'hg':
          backend(safe_str(destination_fork_path), create=True,
                  src_url=safe_str(source_repo_path),

Erubis 2.7.0 の rails_helper が Ruby1.9 系で動かない

--- lib/erubis/helpers/rails_form_helper.rb.org
+++ lib/erubis/helpers/rails_form_helper.rb
@@ -123,7 +123,7 @@
       s = ""
       ## start tag
       s << "<select id=\"#{object}_#{method}\" name=\"#{object}[#{method}]\""
-      for key, val in html_options:
+      for key, val in html_options
           s << " #{key}=\"#{val}\""
       end
       s << ">\n"

今まで非公式に使えていたthenの代わりのコロン(:)がif/unlessやcase構文で使用できなくなった,などの違いがある。

http://itpro.nikkeibp.co.jp/article/NEWS/20090131/323916/

1.8系ならfor文でも動いてたそうでs

Redmine の ガントチャートの PDF 出力に縦線を出してみた

--- lib/redmine/helpers/gantt.rb.org	Mon Mar 07 20:41:58 2011
+++ lib/redmine/helpers/gantt.rb	Thu Apr 07 18:24:09 2011
@@ -259,0 +260 @@
+          vertical_line_pdf(options)
@@ -261,0 +263,21 @@
+      
+      def vertical_line_pdf(options)
+        left = options[:subject_width]
+        day = 0
+        options[:pdf].SetFontStyle('B',7)
+        width = options[:zoom]
+        options[:pdf].SetDrawColor(200,200,200)
+        (self.date_to - self.date_from).to_i.times do 
+          options[:pdf].SetY(options[:top_start])
+          options[:pdf].SetX(left)
+          options[:pdf].Cell(width, options[:top] - options[:top_start], "", "R", 0, "C")
+          if Time.now.beginning_of_day == self.date_from.since(day.days)
+            options[:pdf].SetDrawColor(255, 0, 0)
+            options[:pdf].Line(left + width / 2, options[:top_start], left + width / 2, options[:top])
+            options[:pdf].SetDrawColor(200,200,200)
+          end
+          left = left + width
+          day = day + 1
+        end
+        options[:pdf].SetDrawColor(0, 0, 0)
+      end
@@ -529,0 +552 @@
+        show_day_num = false
@@ -536 +559,2 @@
-            headers_heigth = 3*header_heigth
+            show_day_num = true
+            headers_heigth = 4*header_heigth
@@ -584,0 +609,27 @@
+
+        # Day num headers
+        if show_day_num
+          left = subject_width
+          height = header_heigth
+          wday = self.date_from.cwday
+          pdf.SetFontStyle('B',7)
+          day_num = self.date_from
+          (self.date_to - self.date_from + 1).to_i.times do
+            width =  zoom 
+            pdf.SetY(y_start + 2 * header_heigth)
+            pdf.SetX(left)
+            case(wday)
+            when 6
+              pdf.SetTextColor(0, 0, 255)
+            when 7
+              pdf.SetTextColor(255, 0, 0)
+            else
+              pdf.SetTextColor(0, 0, 0)
+            end
+            pdf.Cell(width, height, day_num.day.to_s, "LTR", 0, "C")
+            left = left + width
+            day_num = day_num + 1
+            wday = wday + 1
+            wday = 1 if wday > 7
+          end
+        end
@@ -594 +645 @@
-            pdf.SetY(y_start + 2 * header_heigth)
+            pdf.SetY(y_start + 3 * header_heigth)
@@ -595,0 +647,8 @@
+            case(wday)
+            when 6
+              pdf.SetTextColor(0, 0, 255)
+            when 7
+              pdf.SetTextColor(255, 0, 0)
+            else
+              pdf.SetTextColor(0, 0, 0)
+            end
@@ -602 +661,2 @@
-        
+
+        pdf.SetTextColor(0, 0, 0)
@@ -610,0 +671 @@
+          :top_start => top,
@@ -704,0 +766 @@
+          vertical_line_pdf(options)
@@ -706 +768 @@
-          options[:top] = 15
+          options[:top] = options[:top_start] = 15