Browse files

Shortcut for getting instance of an actor

  • Loading branch information...
1 parent c4ea604 commit 9db60de0f3af61d30610664dd61db28621bab86f @jnunemaker committed Aug 2, 2012
Showing with 59 additions and 0 deletions.
  1. +14 −0 lib/flipper/dsl.rb
  2. +45 −0 spec/flipper/dsl_spec.rb
View
14 lib/flipper/dsl.rb
@@ -28,6 +28,20 @@ def group(name)
Flipper.group(name)
end
+ def actor(actor_or_number)
+ raise ArgumentError, "actor cannot be nil" if actor_or_number.nil?
+
+ identifier = if actor_or_number.respond_to?(:identifier)
+ actor_or_number.identifier
+ elsif actor_or_number.respond_to?(:id)
+ actor_or_number.id
+ else
+ actor_or_number
+ end
+
+ Flipper::Types::Actor.new(identifier)
+ end
+
private
def features
View
45 spec/flipper/dsl_spec.rb
@@ -93,4 +93,49 @@ def feature(name)
end
end
end
+
+ describe "#actor" do
+ context "for something that responds to id" do
+ it "returns actor instance with identifier set to id" do
+ user = Struct.new(:id).new(23)
+ actor = subject.actor(user)
+ actor.should be_instance_of(Flipper::Types::Actor)
+ actor.identifier.should eq(23)
+ end
+ end
+
+ 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)
+ actor.should be_instance_of(Flipper::Types::Actor)
+ actor.identifier.should eq(45)
+ end
+ end
+
+ context "for something that responds to identifier and id" do
+ it "returns actor instance with identifier set to identifier" do
+ user = Struct.new(:id, :identifier).new(1, 50)
+ actor = subject.actor(user)
+ actor.should be_instance_of(Flipper::Types::Actor)
+ actor.identifier.should eq(50)
+ 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)
+ end
+ end
+
+ context "for nil" do
+ it "raises error" do
+ expect {
+ subject.actor(nil)
+ }.to raise_error(ArgumentError)
+ end
+ end
+ end
end

0 comments on commit 9db60de

Please sign in to comment.