Hatena::ブログ(Diary)

すぱぶらの日記

2012-02-01

File to import not found or unreadable: compass. in Rails 3.2.1

@import "compass"

Rails 3.2.1, compass 0.11.7 で compass を import しようとするとエラーになるので、Gemfile を以下のように書き換えると動く。

gem 'compass', :git => 'git://github.com/chriseppstein/compass.git', :branch => 'no_rails_integration'
gem 'compass-rails', :git => 'git://github.com/Compass/compass-rails.git'

compass 便利だけど、Rails のバージョンが上がるとすぐ動かなくなる印象がある。

その他、参考

PHP で時刻に関わるテストをする

時刻に関わるテストの大体は面倒くさい。

次の金曜日のUNIXTIMEを返す関数が来週も正しく動作することをテストしたい場合、以下のようにサーバ時刻を変更し、完了後に元に戻すことでテストができる。

<?php
date_default_timezone_set("Asia/Tokyo");

// 次の金曜日のUNIXTIMEを返す 
function nextFriday() {
    echo strtotime("next Friday");
}

// 1週間後にサーバ時刻を変更
exec("date --set '′date --date \'7 day\'′' ");
// テストを実行
nextFriday();
// ntpdateを利用し時刻を元に戻す
exec("ntpdate ntpd_hostname");

正しく動作はするけど、サーバ時刻をずらすのは大袈裟すぎる。

ntpdateが正しく動作しなかったとき、commit時刻が未来をさしてて大変なことになったりする。

関数引数にUNIXTIMEを受け取り、初期値をnullとしてnullの場合現在時刻を利用するように実装すると、開発では引数を使わず、テストでは1週間後のUNIXTIMEを渡すことがテストができる。

<?php
date_default_timezone_set("Asia/Tokyo");

// 次の金曜日のUNIXTIMEを返す 
// 引数にUNIXTIMEを取るようにする
function nextFriday($time = null) {
    echo strtotime("next Friday", is_null($time) ? time() : $time);
}

// テストを実行
nextFriday(strtotime("7 days"));

テストがしやすいように実装しようという話。

#よりよい方法を教えてください

2012-01-29

Redcarpet のシンタックスハイライトに CodeRay を使う

require "redcarpet"
require "coderay"

class HTMLwithCoderay < Redcarpet::Render::HTML
  def block_code(code, language)
    CodeRay.scan(code, language.to_sym).div(:line_numbers => :table)
  end
end

def markdown(text)
  rndr = HTMLwithCoderay.new(:filter_html => true, :hard_wrap => true)
  markdown = Redcarpet::Markdown.new(rndr, :autolink => true, :space_after_headers => true, :fenced_code_blocks => true)
  markdown.render(text)
end

その他、参考

2011-12-26

title を動的に設定する

<title><%= content_for?(:title) ? yield(:title) : "default title" %></title>
<% content_for :title do %>
  <%= post.user.name %>'s post
<% end %>

その他、参考