クラス宣言時に、抽出条件を定義する

record で繰り返しになっている部分を抽出し、
has_property でプロパティになる部分を一つずつ抽出する定義をする。


以下のような感じにもしようかと思ったけれども、あまりきれいじゃない。

  record :xpath => '//td[@bgcolor=#000000]' do |has_property|
    has_property[:link] = :regexp => '(http://[^\s]+?\.asx)'
    ...

音泉用解析クラス

class Onsen < Rire::PropertyBase
  record :xpath => '//td[@bgcolor=#000000]' do
    has_property :title, :regexp => '<td width="135"[^>]?>(?:(?:<.+?>)*)\s*(.+?)\s*(?:<.+?>)'
    has_property :link, :regexp => '(http://[^\s]+?\.asx)'
    has_property :date, :regexp => '(http://[^\s]+?\.asx)'
    has_property :category, :regexp => '<img height="11" alt="(.+?)!"'
    has_property :description, :regexp => '<td align="center"><img .+?alt="([^"]+?)"'
  end

  after_hook do |entry|
    next nil if entry.title.nil? || entry.link.nil?

    entry.description = "#{entry.title} #{entry.description}"
    entry.category = "" if entry.category.nil?

    # 年が変わった場合は考慮していない
    if entry.date =~ /([0-9]{2}[0-9]{2}).asx/
      entry.date = Time.parse($1)
    else
      entry.date = Time.now
    end

    entry
  end
end