+# coding: utf-8
class Song < ActiveRecord::Base
include ::SelectableAttr::Base
+ before_save :make_words_for_search
selectable_attr :font_size do
- entry "0", :xsmall, 'XSmall'
- entry "1", :small, 'Small'
- entry "2", :middle, 'Middle'
- entry "3", :large, 'Large'
- entry "4", :xlarge, 'XLarge'
+ entry "0", :xsmall, "25pt"
+ entry "1", :small, "30pt"
+ entry "2", :middle, "34pt"
+ entry "3", :large, "39pt"
+ entry "4", :xlarge, "44pt"
end
+ RUBY_KANJI_HIRA = %r/([一-龠]+)\(([ぁ-ん]+)\)/
+ RUBY_ENGLISH_KANA = %r/([a-zA-Z]+)\(([ァ-ンー]+)\)/
+ RUBY_KANA_HIRA = %r/([ァ-ン]+)\(([ぁ-ん]+)\)/
+
CODES = %w(A Ab Bb C Cm D E Eb Em F F#m Fm G)
named_scope :active, :conditions => ["#{table_name}.deleted_at IS NULL"]
- validates_presence_of :titile, :words, :font_size
+ validates_presence_of :title, :words, :font_size
validates_numericality_of :font_size
def self.default_font_size
- self.font_size_id_by_key(:middle)
+ font_size_id_by_key(:middle)
+ end
+
+ def make_words_for_search
+ words_for_search = words + kana
+ end
+
+ def kana
+ strip_ruby_base(words)
+ end
+
+ def ruby
+ markup_html_ruby_tag(words)
+ end
+
+ # 慈(いつく)しみ
+ # => <ruby><rb>慈</rb><rp>(</rp><rt>いつく</rt><rp>)</rp></ruby>しみ
+ def markup_html_ruby_tag(str)
+ str.
+ to_s.
+ gsub(RUBY_KANJI_HIRA) { ruby_tag_template($1, $2) }.
+ gsub(RUBY_ENGLISH_KANA){ ruby_tag_template($1, $2) }.
+ gsub(RUBY_KANA_HIRA) { ruby_tag_template($1, $2) }
+ end
+
+ def ruby_tag_template(base, text)
+ "<ruby><rb>#{base}</rb><rp>(</rp><rt>#{text}</rt><rp>)</rp></ruby>"
+ end
+
+ def strip_ruby_base(str)
+ str.gsub(RUBY_KANJI_HIRA){ $2 }.gsub(RUBY_ENGLISH_KANA){ $2 }.gsub(RUBY_KANA_HIRA){ $2 }
end
end