私が取り組んでいる Ruby プロジェクトでは、次のような mixin アーキテクチャを持つモデル クラスに ActiveRecord スタイルの MVC 機能を追加します。
module Model
# Classes that mixin this module gain ActiveRecord-style class methods
# like Model.all, Model.first, Model.last et al.
#
# Throughout this module, @@database_bridge will contain a reference to a
# database ORM bridge, that does the dirty implementation of these methods.
def all
# Implementation stuff here, using @@database_bridge as appropriate
end
def first
...
end
# et al
end
class ExampleModel
extend Model
# Model-specific implementation goes here...
end
を呼び出すと、データベース内e = ExampleModel.first
の最初のものが に割り当てられます。ExampleModel
e
@@database_bridge
依存性注入を使用して実行時に設定し、それを含むすべてのクラスがextend Model
同じ指定された ORM オブジェクトを使用するようにしたいと考えています。
これどうやってするの?
そのクラス変数をオンデマンドで設定するための何らかのヘルパー メソッドを記述できれば、それは素晴らしいことです。