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