ruby Ruby中的简单特征翻转器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Ruby中的简单特征翻转器相关的知识,希望对你有一定的参考价值。

```ruby
require 'feature'

# feature = Feature.new(:new_year_landing_page)
# feature.activate(:company1)

if feature.activated?(:company1)
  show_new_year_landing_page
else
  show_regular_landing_page
end
```
require 'spec_helper'

require 'mock_redis'

describe Feature do
  let(:redis) { MockRedis.new }

  before(:each) do
    Feature.redis = redis
  end

  describe '#activate' do
    it 'activates feature for given company' do
      feature_name = :nice_new_feature
      feature = Feature.new(feature_name)
      feature.activate(:company1)

      redis_namespace = "sys:ff"
      redis_key = "#{redis_namespace}:nice_new_feature:company1"
      expect(redis.get(redis_key)).to eq('true')
    end
  end

  describe '#activated?' do
    context 'feature activated for the company' do
      it 'returns true' do
        feature_name = :nice_new_feature
        feature = Feature.new(feature_name)
        feature.activate(:company1)

        expect(feature.activated?(:company1)).to eq(true)
      end
    end

    context "feature isn't activated for the company" do
      it 'returns false' do
        feature_name = :nice_new_feature
        feature = Feature.new(feature_name)

        expect(feature.activated?(:company1)).to eq(false)
      end
    end
  end
end
require 'mock_redis'

class Feature
  class << self
    attr_accessor :redis
  end

  def initialize(name)
    @name = name
  end

  def activate(company_name)
    redis_key = ['sys', 'ff', @name, company_name].join(':')
    self.class.redis.set(redis_key, true)
  end

  def activated?(company_name)
    redis_key = ['sys', 'ff', @name, company_name].join(':')
    self.class.redis.get(redis_key) == 'true'
  end
end

以上是关于ruby Ruby中的简单特征翻转器的主要内容,如果未能解决你的问题,请参考以下文章

ruby Rails中的简单特征部署

ruby Ruby中的简单命令行解析器

为啥学习 Ruby 中的元编程和特征类很重要?

ruby Ruby on Rails简单的youtube嵌入帮助器

ruby 创建简单的rspec自定义匹配器

一个 Ruby 对象可以有多个特征类吗?