Rails以DRY方式使用method_missing共享常量

时间:2014-10-10 18:36:52

标签: ruby-on-rails ruby method-missing

我写了一堂课:

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模块中,然后“扩展”#39;那个模块。尝试创建一个基地'上课并从中得出,但我不能同时接受工作。

这是我的规范示例:

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

任何想法都表示赞赏。提前谢谢。

1 个答案:

答案 0 :(得分: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