Skip to content

Commit

Permalink
Merge pull request #32 from jvanbaarsen/add-group-exists
Browse files Browse the repository at this point in the history
Add a method to check in clean way if group exists
  • Loading branch information
greatuserongithub committed Oct 4, 2014
2 parents 905d7d7 + 2f8cb76 commit 39e6d92
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/flipper.rb
Expand Up @@ -37,6 +37,13 @@ def self.unregister_groups
groups.clear
end

# Public: Check if a group exists
#
# Returns boolean
def self.group_exists?(name)
self.groups.key?(name)
end

# Internal: Fetches a group by name.
#
# name - The Symbol name of the group.
Expand Down
8 changes: 7 additions & 1 deletion lib/flipper/registry.rb
Expand Up @@ -45,14 +45,20 @@ def add(key, value)

def get(key)
key = key.to_sym

@mutex.synchronize {
@source.fetch(key) {
raise KeyNotFound.new(key)
}
}
end

def key?(key)
key = key.to_sym
@mutex.synchronize {
@source.has_key?(key)
}
end

def each(&block)
@mutex.synchronize { @source.dup }.each(&block)
end
Expand Down
14 changes: 14 additions & 0 deletions spec/flipper/registry_spec.rb
Expand Up @@ -52,6 +52,20 @@
end
end

describe "#key?" do
before do
source[:admins] = "admins"
end

it "returns true if the key exists" do
subject.key?(:admins).should eq true
end

it "returns false if the key does not exists" do
subject.key?(:unknown_key).should eq false
end
end

describe "#each" do
before do
source[:admins] = 'admins'
Expand Down
13 changes: 13 additions & 0 deletions spec/flipper_spec.rb
Expand Up @@ -8,6 +8,19 @@
end
end

describe ".group_exists" do
it "returns true if the group is already created" do
group = Flipper.register('admins') { |actor| actor.admin? }
Flipper.group_exists?(:admins).should eq(true)
end

it "returns false when the group is not yet registered" do
registry = Flipper::Registry.new
Flipper.groups = registry
Flipper.group_exists?(:non_existing).should eq(false)
end
end

describe ".groups" do
it "returns a registry instance" do
Flipper.groups.should be_instance_of(Flipper::Registry)
Expand Down

0 comments on commit 39e6d92

Please sign in to comment.