Skip to content
Browse files

Introduce Flipper::Client

Wrapper for shortcuts to access features and soon more.
  • Loading branch information...
1 parent 620f045 commit c262a4c7fc754f055fdaff903632f19b15d93ad6 @jnunemaker committed Aug 2, 2012
Showing with 111 additions and 0 deletions.
  1. +33 −0 lib/flipper/client.rb
  2. +78 −0 spec/flipper/client_spec.rb
View
33 lib/flipper/client.rb
@@ -0,0 +1,33 @@
+module Flipper
+ class Client
+ def initialize(adapter)
+ @adapter = adapter
+ end
+
+ def enabled?(name, *args)
+ feature(name).enabled?(*args)
+ end
+
+ def disabled?(name, *args)
+ !enabled?(name, *args)
+ end
+
+ def enable(name, *args)
+ feature(name).enable(*args)
+ end
+
+ def disable(name, *args)
+ feature(name).disable(*args)
+ end
+
+ def feature(name)
+ features[name.to_sym] ||= Flipper::Feature.new(name, @adapter)
+ end
+
+ private
+
+ def features
+ @features ||= {}
+ end
+ end
+end
View
78 spec/flipper/client_spec.rb
@@ -0,0 +1,78 @@
+require 'helper'
+require 'flipper/client'
+
+describe Flipper::Client do
+ subject { Flipper::Client.new(adapter) }
+
+ let(:source) { {} }
+ let(:adapter) { Flipper::Adapters::Memory.new(source) }
+
+ let(:admins_feature) { feature(:admins) }
+
+ before do
+ source.clear
+ end
+
+ def feature(name)
+ Flipper::Feature.new(name, adapter)
+ end
+
+ describe "#enabled?" do
+ before do
+ subject.stub(:feature => admins_feature)
+ end
+
+ it "passes arguments to feature enabled check and returns result" do
+ admins_feature.should_receive(:enabled?).with(:foo).and_return(true)
+ subject.should_receive(:feature).with(:stats).and_return(admins_feature)
+ subject.enabled?(:stats, :foo).should be_true
+ end
+ end
+
+ describe "#disabled?" do
+ it "passes all args to enabled? and returns the opposite" do
+ subject.should_receive(:enabled?).with(:stats, :foo).and_return(true)
+ subject.disabled?(:stats, :foo).should be_false
+ end
+ end
+
+ describe "#enable" do
+ before do
+ subject.stub(:feature => admins_feature)
+ end
+
+ it "calls enable for feature with arguments" do
+ admins_feature.should_receive(:enable).with(:foo)
+ subject.should_receive(:feature).with(:stats).and_return(admins_feature)
+ subject.enable :stats, :foo
+ end
+ end
+
+ describe "#disable" do
+ before do
+ subject.stub(:feature => admins_feature)
+ end
+
+ it "calls disable for feature with arguments" do
+ admins_feature.should_receive(:disable).with(:foo)
+ subject.should_receive(:feature).with(:stats).and_return(admins_feature)
+ subject.disable :stats, :foo
+ end
+ end
+
+ describe "#feature" do
+ before do
+ @result = subject.feature(:stats)
+ end
+
+ it "returns instance of feature with correct name and adapter" do
+ @result.should be_instance_of(Flipper::Feature)
+ @result.name.should eq(:stats)
+ @result.adapter.should eq(adapter)
+ end
+
+ it "memoizes the feature" do
+ subject.feature(:stats).should equal(@result)
+ end
+ end
+end

0 comments on commit c262a4c

Please sign in to comment.
Something went wrong with that request. Please try again.