Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move groups to Flipper registry instance instead of class var

  • Loading branch information...
commit c6d5e7fffd704e2136343c3c40eced63da8e3dff 1 parent 4f7e6dd
@jnunemaker authored
View
2  examples/group.rb
@@ -7,7 +7,7 @@
stats = Flipper::Feature.new(:stats, adapter)
# Define group
-admins = Flipper::Types::Group.define(:admins) do |actor|
+admins = Flipper.register(:admins) do |actor|
actor.respond_to?(:admin?) && actor.admin?
end
View
19 lib/flipper.rb
@@ -1,5 +1,24 @@
require 'flipper/feature'
+require 'flipper/registry'
+require 'flipper/types/group'
require 'flipper/adapters/memory'
module Flipper
+ def groups
+ @groups ||= Registry.new
+ end
+
+ def register(name, &block)
+ group = Types::Group.new(name, &block)
+ groups.add(group.name, group)
+ group
+ rescue Registry::DuplicateKey
+ raise DuplicateGroup, %Q{Group named "#{name}" is already registered}
+ end
+
+ def group(name)
+ groups.get(name)
+ end
+
+ extend self
end
View
5 lib/flipper/errors.rb
@@ -1,10 +1,11 @@
module Flipper
- class Error < StandardError
- end
+ class Error < StandardError; end
class GateNotFound < Error
def initialize(thing)
super "Could not find gate for #{thing.inspect}"
end
end
+
+ class DuplicateGroup < Error; end
end
View
2  lib/flipper/gates/group.rb
@@ -21,7 +21,7 @@ def group_names
end
def groups
- group_names.map { |name| Flipper::Types::Group.get(name) }.compact
+ group_names.map { |name| Flipper.groups.get(name) }.compact
end
def protects?(thing)
View
24 lib/flipper/types/group.rb
@@ -1,32 +1,10 @@
module Flipper
module Types
class Group < Type
- def self.all
- @all ||= []
- end
-
- def self.define(name, &block)
- group = new(name, &block)
- all.push group
- group
- end
-
- def self.get(name)
- detect { |group| group.name == name }
- end
-
- class << self
- include Enumerable
-
- def each
- all.each { |group| yield group }
- end
- end
-
attr_reader :name
def initialize(name, &block)
- @name = name
+ @name = name.to_sym
@block = block
end
View
8 spec/flipper/feature_spec.rb
@@ -12,8 +12,8 @@
let(:boolean_key) { Flipper::Gates::Boolean::Key }
let(:group_key) { Flipper::Gates::Group::Key }
- let(:admin_group) { Flipper::Types::Group.get(:admins) }
- let(:dev_group) { Flipper::Types::Group.get(:devs) }
+ let(:admin_group) { Flipper.groups.get(:admins) }
+ let(:dev_group) { Flipper.groups.get(:devs) }
let(:admin_thing) { double 'Non Flipper Thing', :admin? => true, :dev? => false }
let(:dev_thing) { double 'Non Flipper Thing', :admin? => false, :dev? => true }
@@ -28,8 +28,8 @@
let(:percentage_of_random_key) { Flipper::Gates::PercentageOfRandom::Key }
before do
- Flipper::Types::Group.define(:admins) { |thing| thing.admin? }
- Flipper::Types::Group.define(:devs) { |thing| thing.dev? }
+ Flipper.register(:admins) { |thing| thing.admin? }
+ Flipper.register(:devs) { |thing| thing.dev? }
source.clear
end
View
34 spec/flipper/types/group_spec.rb
@@ -6,40 +6,6 @@
Flipper::Types::Group.new(:admins) { |actor| actor.admin? }
end
- it "is enumerable at the class level" do
- Flipper::Types::Group.define(:admins) { |actor| actor.admin? }
- Flipper::Types::Group.map { |group|
- group.name
- }.should eq([:admins])
- end
-
- describe ".all" do
- it "defaults to empty array" do
- Flipper::Types::Group.all.should eq([])
- end
- end
-
- describe ".define" do
- before do
- @result = Flipper::Types::Group.define(:admins) { |actor| actor.admin? }
- end
-
- it "adds group to all" do
- Flipper::Types::Group.all.should include(@result)
- end
- end
-
- describe ".get" do
- it "returns nil if group does not exist" do
- Flipper::Types::Group.get(:not_found).should be_nil
- end
-
- it "returns group if group exists" do
- group = Flipper::Types::Group.define(:admins) { |actor| actor.admin? }
- Flipper::Types::Group.get(:admins).should eq(group)
- end
- end
-
it "initializes with name" do
group = Flipper::Types::Group.new(:admins)
group.should be_instance_of(Flipper::Types::Group)
View
38 spec/flipper_spec.rb
@@ -1,4 +1,42 @@
require 'helper'
describe Flipper do
+ describe ".groups" do
+ it "returns a registry instance" do
+ Flipper.groups.should be_instance_of(Flipper::Registry)
+ end
+ end
+
+ describe ".register" do
+ it "adds a group to the group_registry" do
+ group = Flipper.register(:admins) { |actor| actor.admin? }
+ Flipper.groups.get(:admins).should eq(group)
+ end
+
+ it "raises exception if group already registered" do
+ Flipper.register(:admins) { }
+
+ expect {
+ Flipper.register(:admins) { }
+ }.to raise_error(Flipper::DuplicateGroup)
+ end
+ end
+
+ describe ".group" do
+ context "for registered group" do
+ before do
+ @group = Flipper.register(:admins) { }
+ end
+
+ it "returns group" do
+ Flipper.group(:admins).should eq(@group)
+ end
+ end
+
+ context "for unregistered group" do
+ it "returns nil" do
+ Flipper.group(:cats).should be_nil
+ end
+ end
+ end
end
View
2  spec/helper.rb
@@ -24,7 +24,7 @@
config.run_all_when_everything_filtered = true
config.before(:each) do
- Flipper::Types::Group.all.clear
+ Flipper.groups.clear
Timecop.return
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.