Skip to content
Browse files

No longer force passing actor instance to enable/disable.

This is related to #13. First, it allows just passing in anything that
responds to id to enable/disable and friends. Second, it wraps whatever
is passed in, if possible, and uses the wrapped value. Third,
identifier is no longer supported. From now on id.to_s will be used
everywhere.
  • Loading branch information...
1 parent 8bb9ae4 commit fad3fd0bdac0710711c1b363a7407c79096c26b9 @jnunemaker committed Jan 1, 2013
View
8 lib/flipper/feature.rb
@@ -22,12 +22,12 @@ def disable(thing = Types::Boolean.new)
gate_for(thing).disable(thing)
end
- def enabled?(actor = nil)
- !! catch(:short_circuit) { gates.detect { |gate| gate.open?(actor) } }
+ def enabled?(thing = nil)
+ !!catch(:short_circuit) { gates.detect { |gate| gate.open?(thing) } }
end
- def disabled?(actor = nil)
- !enabled?(actor)
+ def disabled?(thing = nil)
+ !enabled?(thing)
end
# Internal: Gates to check to see if feature is enabled/disabled
View
22 lib/flipper/gates/actor.rb
@@ -11,19 +11,27 @@ def toggle_class
Toggles::Set
end
- def open?(actor)
- return if actor.nil?
- return unless Types::Actor.wrappable?(actor)
- actor = Types::Actor.wrap(actor)
- identifiers.include?(actor.identifier)
+ def open?(thing)
+ return if thing.nil?
+ return unless Types::Actor.wrappable?(thing)
+ actor = Types::Actor.wrap(thing)
+ ids.include?(actor.value)
end
- def identifiers
+ def ids
toggle.value
end
def protects?(thing)
- thing.is_a?(Flipper::Types::Actor)
+ thing.is_a?(Flipper::Types::Actor) || thing.respond_to?(:id)
+ end
+
+ def enable(thing)
+ toggle.enable Types::Actor.wrap(thing)
+ end
+
+ def disable(thing)
+ toggle.disable Types::Actor.wrap(thing)
end
end
end
View
9 lib/flipper/gates/percentage_of_actors.rb
@@ -9,13 +9,18 @@ def type_key
Key
end
- def open?(actor)
+ def open?(thing)
percentage = toggle.value
if percentage.nil?
false
else
- Zlib.crc32(actor.identifier.to_s) % 100 < percentage
+ if Types::Actor.wrappable?(thing)
+ actor = Types::Actor.wrap(thing)
+ Zlib.crc32(actor.value) % 100 < percentage
+ else
+ false
+ end
end
end
View
16 lib/flipper/types/actor.rb
@@ -2,9 +2,7 @@ module Flipper
module Types
class Actor < Type
def self.wrappable?(thing)
- thing.is_a?(Flipper::Types::Actor) ||
- thing.respond_to?(:identifier) ||
- thing.respond_to?(:to_i)
+ thing.is_a?(Flipper::Types::Actor) || thing.respond_to?(:id)
end
def self.wrap(thing)
@@ -15,21 +13,13 @@ def self.wrap(thing)
end
end
- attr_reader :identifier
+ attr_reader :value
def initialize(thing)
raise ArgumentError, "thing cannot be nil" if thing.nil?
@thing = thing
- @identifier = if thing.respond_to?(:identifier)
- thing.identifier
- else
- thing
- end.to_s
- end
-
- def value
- @identifier
+ @value = thing.id.to_s
end
def respond_to?(*args)
View
18 spec/flipper/dsl_spec.rb
@@ -117,20 +117,12 @@ def feature(name)
end
describe "#actor" do
- context "for something that responds to identifier" do
- it "returns actor instance with identifier set to id" do
- user = Struct.new(:identifier).new(45)
- actor = subject.actor(user)
+ context "for a thing" do
+ it "returns actor instance" do
+ thing = Struct.new(:id).new(33)
+ actor = subject.actor(thing)
actor.should be_instance_of(Flipper::Types::Actor)
- actor.identifier.should eq('45')
- end
- end
-
- context "for a number" do
- it "returns actor instance with identifer set to number" do
- actor = subject.actor(33)
- actor.should be_instance_of(Flipper::Types::Actor)
- actor.identifier.should eq('33')
+ actor.value.should eq('33')
end
end
View
57 spec/flipper/types/actor_spec.rb
@@ -3,15 +3,16 @@
describe Flipper::Types::Actor do
subject {
- described_class.new(2)
+ thing = Struct.new(:id).new('2')
+ described_class.new(thing)
}
let(:thing_class) {
Class.new {
- attr_reader :identifier
+ attr_reader :id
def initialize(identifier)
- @identifier = identifier
+ @id = id
end
def admin?
@@ -22,22 +23,15 @@ def admin?
describe ".wrappable?" do
it "returns true if actor" do
- thing = described_class.new(1)
- described_class.wrappable?(thing).should be_true
+ thing = Struct.new(:id).new('1')
+ actor = described_class.new(thing)
+ described_class.wrappable?(actor).should be_true
end
- it "returns true if responds to identifier" do
- thing = Struct.new(:identifier).new(10)
+ it "returns true if responds to id" do
+ thing = Struct.new(:id).new(10)
described_class.wrappable?(thing).should be_true
end
-
- it "returns true if responds to to_i" do
- described_class.wrappable?(1).should be_true
- end
-
- it "returns false if not actor and does not respond to identifier or to_i" do
- described_class.wrappable?(Object.new).should be_false
- end
end
describe ".wrap" do
@@ -51,22 +45,17 @@ def admin?
context "for other thing" do
it "returns actor" do
- actor = described_class.wrap(1)
+ thing = Struct.new(:id).new('1')
+ actor = described_class.wrap(thing)
actor.should be_instance_of(described_class)
end
end
end
- it "initializes with identifier" do
- actor = described_class.new(2)
- actor.should be_instance_of(described_class)
- end
-
- it "initializes with object that responds to identifier" do
- identifier = '1'
- thing = Struct.new(:identifier).new(identifier)
+ it "initializes with thing that responds to id" do
+ thing = Struct.new(:id).new('1')
actor = described_class.new(thing)
- actor.identifier.should be(identifier)
+ actor.value.should eq('1')
end
it "raises error when initialized with nil" do
@@ -75,14 +64,10 @@ def admin?
}.to raise_error(ArgumentError)
end
- it "converts identifier to string" do
- actor = described_class.new(2)
- actor.identifier.should eq('2')
- end
-
- it "has identifier" do
- actor = described_class.new(2)
- actor.identifier.should eq('2')
+ it "converts id to string" do
+ thing = Struct.new(:id).new(2)
+ actor = described_class.new(thing)
+ actor.value.should eq('2')
end
it "proxies everything to thing" do
@@ -93,7 +78,8 @@ def admin?
describe "#respond_to?" do
it "returns true if responds to method" do
- actor = described_class.new(10)
+ thing = Struct.new(:id).new('1')
+ actor = described_class.new(thing)
actor.respond_to?(:value).should be_true
end
@@ -104,7 +90,8 @@ def admin?
end
it "returns false if does not respond to method and thing does not respond to method" do
- actor = described_class.new(10)
+ thing = Struct.new(:id).new(10)
+ actor = described_class.new(thing)
actor.respond_to?(:frankenstein).should be_false
end
end
View
2 spec/helper.rb
@@ -18,6 +18,8 @@
Logger.new(log_path.join('test.log'))
RSpec.configure do |config|
+ config.fail_fast = true
+
config.filter_run :focused => true
config.alias_example_to :fit, :focused => true
config.alias_example_to :xit, :pending => true
View
33 spec/integration_spec.rb
@@ -5,17 +5,19 @@
describe Flipper::Feature do
subject { described_class.new(:search, adapter) }
+ let(:actor_class) { Struct.new(:id) }
+
let(:source) { {} }
let(:adapter) { Flipper::Adapters::Memory.new(source) }
let(:admin_group) { Flipper.group(:admins) }
let(:dev_group) { Flipper.group(:devs) }
- let(:admin_thing) { double 'Non Flipper Thing', :identifier => 1, :admin? => true, :dev? => false }
- let(:dev_thing) { double 'Non Flipper Thing', :identifier => 10, :admin? => false, :dev? => true }
+ let(:admin_thing) { double 'Non Flipper Thing', :id => 1, :admin? => true, :dev? => false }
+ let(:dev_thing) { double 'Non Flipper Thing', :id => 10, :admin? => false, :dev? => true }
- let(:pitt) { Flipper::Types::Actor.new(1) }
- let(:clooney) { Flipper::Types::Actor.new(10) }
+ let(:pitt) { actor_class.new(1) }
+ let(:clooney) { actor_class.new(10) }
let(:five_percent_of_actors) { Flipper::Types::PercentageOfActors.new(5) }
let(:five_percent_of_random) { Flipper::Types::PercentageOfRandom.new(5) }
@@ -36,9 +38,10 @@ def enable_group(group)
adapter.set_add key, name
end
- def enable_actor(actor)
+ def enable_actor(thing)
key = Flipper::Key.new(subject.name, Flipper::Gates::Actor::Key)
- adapter.set_add key, actor.identifier
+ actor = Flipper::Types::Actor.wrap(thing)
+ adapter.set_add key, actor.value
end
def enable_percentage_of_actors(percentage)
@@ -120,7 +123,11 @@ def enable_percentage_of_random(percentage)
end
it "enables feature for actor within percentage" do
- enabled = (1..100).select { |i| subject.enabled?(Flipper::Types::Actor.new(i)) }.length
+ enabled = (1..100).select { |i|
+ thing = Struct.new(:id).new(i)
+ subject.enabled?(thing)
+ }.size
+
enabled.should be_within(2).of(5)
end
@@ -188,7 +195,11 @@ def enable_percentage_of_random(percentage)
end
it "disables actor in percentage of actors" do
- enabled = (1..100).select { |i| subject.enabled?(Flipper::Types::Actor.new(i)) }.length
+ enabled = (1..100).select { |i|
+ thing = Struct.new(:id).new(i)
+ subject.enabled?(thing)
+ }.size
+
enabled.should be(0)
end
@@ -255,7 +266,11 @@ def enable_percentage_of_random(percentage)
end
it "disables feature" do
- enabled = (1..100).select { |i| subject.enabled?(Flipper::Types::Actor.new(i)) }.length
+ enabled = (1..100).select { |i|
+ thing = Struct.new(:id).new(i)
+ subject.enabled?(thing)
+ }.size
+
enabled.should be(0)
end

0 comments on commit fad3fd0

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