0

私はクラスを書いています:

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

どんなアイデアでも大歓迎です。前もって感謝します。

4

1 に答える 1

0

実際の定数の代わりに、サブクラスとクラス メソッドを使用します。

class ConstantAccessor
  def self.method_missing(method_name, *args, &block)
    super unless const[method_name]
    const[method_name]
  end

  def self.respond_to?(method_name, include_private = false)
    super unless const[method_name]
    true
  end
end

class State < ConstantAccessor
  def self.const
    @const ||= {
      created:    0,
      prepared:   1,
      shadowed:   2,
      vmxed:      3,
      sbooted:    4,
      booted:     5,
      saved:      6,
      dontknowen: 7
    }
  end
end

class AnotherSetOfConstants < ConstantAccessor
  def self.const
    @const ||= {
      something:    0,
      somethingelse:   1,
      another:   2
    }
  end
end
于 2014-10-10T20:24:19.443 に答える