私はクラスを書いています:
module SharedConstants
class Stage
CONST = {
created: 0,
prepared: 1,
shadowed: 2,
vmxed: 3,
sbooted: 4,
booted: 5,
saved: 6,
dontknowen: 7
}
def self.method_missing(method_name, *args, &block)
fail "Constant not defined: #{name}.#{method_name}" if CONST[method_name].nil?
CONST[method_name]
end
def self.respond_to?(method_name, include_private = false)
CONST[method_name].nil? ? super : true
end
end
end
...これは、次のような定数にアクセスしたいときにうまく機能します。
Stage.created # => 0
ここで、Foo と呼ばれる別の定数セットを紹介したいと思いますが、コードを乾かしたいと思います。
両方のクラス メソッドを SharedConstant モジュールに移動し、そのモジュールを「拡張」しようとしました。「ベース」クラスを作成してそこから派生しようとしましたが、どちらのアプローチも機能しません。
これが私の仕様の例です:
require 'rails_helper'
RSpec.describe SharedConstants::Stage, type: :lib do
[:created, :prepared, :shadowed, :vmxed, :sbooted,
:booted, :saved, :dontknowen].each do |const|
it "defines #{const}" do
expect(described_class.send(const)).to be >= 0
end
it "responds to #{const}" do
expect(described_class.respond_to?(const)).to eq(true)
end
end
end
どんなアイデアでも大歓迎です。前もって感謝します。