From 69dcae7231e743705d00aa923f13739d94941543 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Fri, 19 Aug 2016 13:52:40 -0500 Subject: [PATCH] Allow strategy to be called on arbitrary classes Prior to this change, it attempt to use the hardcoded `Feature` class, whether or not it exists. So, when I attempted to namespace my Feature class I got the following error: ``` NameError: uninitialized constant Flip::DatabaseStrategy::Feature /Users/jcoyne/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/flip-1.1.0/lib/flip/database_strategy.rb:5:in `initialize' /Users/jcoyne/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/flip-1.1.0/lib/flip/feature_set.rb:36:in `new' /Users/jcoyne/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/flip-1.1.0/lib/flip/feature_set.rb:36:in `add_strategy' /Users/jcoyne/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/flip-1.1.0/lib/flip/declarable.rb:15:in `strategy' /Users/jcoyne/workspace/curation_concerns/app/models/curation_concerns/feature.rb:6:in `' ``` --- lib/flip/abstract_strategy.rb | 3 +++ lib/flip/declarable.rb | 2 +- lib/flip/feature_set.rb | 4 ++-- spec/declarable_spec.rb | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/flip/abstract_strategy.rb b/lib/flip/abstract_strategy.rb index 20ee38b..a93fcd2 100644 --- a/lib/flip/abstract_strategy.rb +++ b/lib/flip/abstract_strategy.rb @@ -1,6 +1,9 @@ module Flip class AbstractStrategy + def initialize(_model_class = nil) + end + def name self.class.name.split("::").last.gsub(/Strategy$/, "").underscore end diff --git a/lib/flip/declarable.rb b/lib/flip/declarable.rb index 8a5331c..f214d28 100644 --- a/lib/flip/declarable.rb +++ b/lib/flip/declarable.rb @@ -12,7 +12,7 @@ def feature(key, options = {}) # Adds a strategy for determining feature status. def strategy(strategy) - FeatureSet.instance.add_strategy strategy + FeatureSet.instance.add_strategy strategy, self end # The default response, boolean or a Proc to be called. diff --git a/lib/flip/feature_set.rb b/lib/flip/feature_set.rb index f37554c..f747171 100644 --- a/lib/flip/feature_set.rb +++ b/lib/flip/feature_set.rb @@ -32,8 +32,8 @@ def << definition end # Adds a strategy for determing feature status. - def add_strategy(strategy) - strategy = strategy.new if strategy.is_a? Class + def add_strategy(strategy, klass = nil) + strategy = strategy.new(klass) if strategy.is_a? Class @strategies[strategy.name] = strategy end diff --git a/spec/declarable_spec.rb b/spec/declarable_spec.rb index ddfc2e4..29b4d80 100644 --- a/spec/declarable_spec.rb +++ b/spec/declarable_spec.rb @@ -22,6 +22,7 @@ it { should_not be_on(:one) } it { should be_on(:three) } end + context "with default set to true" do before { model_class.send(:default, true) } it { should be_on(:one) } @@ -29,4 +30,18 @@ end end + describe "the .strategy class method" do + let!(:model_class) do + Class.new do + extend Flip::Declarable + end + end + + it "passes the class it's on" do + expect(Flip::DeclarationStrategy).to receive(:new) + .with(model_class) + .and_call_original + model_class.strategy Flip::DeclarationStrategy + end + end end