美しくない解決法

いろいろ調べるうちに,FoodynPlugin::Baseというクラス・オブジェクトが二つ存在していることが分かってきました。「なんてこった」って感じです。クラスはシングルトンじゃないんでしょうか。
愚痴りたい気持ちはやまやまですが,とにかく動くようにすることが先決なので,なんとか解決法を考えました。

  def self.list
    @pluginlist ||= Array.new
    if @pluginlist.size == 0
      @pluginlist = Class.constants.select do |c|
        cclass = c.constantize
        if (cclass.is_aModule) && (cclass.constants.include'Base')
          klass = (c+'::Base').constantize
          klass.ancestors.any? {|anc| anc.name == 'FoodynPlugin::Base'}
        end
      end
    end
    @pluginlist
  end
  
  def self.plugin_class plugin_name
    if (pl = list.find {|p| p.downcase == plugin_name.downcase})
      return (pl + '::Main').constantize
    end
  end

プラグイン・クラスの一覧を取るアルゴリズムですが,まずはClassレベルにおける定数を調べ,そこでModuleに属するものを選び,さらに,Module内の定数で「Base」を含んでいるものを選びます。この中から,Base付きのクラスの先祖のクラスを調べ,そこにFoodynPlugin::Baseがあるかどうかで判断しています。本来ここはクラスレベルで判定できるはずですが,上記の理由によって,わざわざ文字列に変換して比較しています。

美しくはありませんが,ともかくこれでスキン変数が動くようになりました。なんだかどっと疲れました。


« Prev item - Next Item »
---------------------------------------------

Comments


No comments yet. You can be the first!


Leave comment

© 2007 yoursite.com | Designed by DesignsByDarren
Ported to Nucleus CMS: Suvoroff