基本クラスでクラス変数を設定する最良の方法は何ですか? CacheMixin
ActiveRecord モデルで使用される を定義している次のコード スニペットを検討してください。モデルごとに、キャッシュされたデータを格納するテーブルを定義できるようにしたいと考えています。class_variable_set
andを使用せずにこれを行うより良い方法はありclass_variable_get
ますか?
require 'rubygems'
require 'active_support/concern'
module CacheMixin
extend ActiveSupport::Concern
module ClassMethods
def with_cache_table(table)
self.class_variable_set('@@cache_table', table)
end
end
def fetch_data
puts self.class.class_variable_get('@@cache_table')
end
end
class TestClass
include CacheMixin
with_cache_table("my_cache_table")
end