Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Added some examples and explained internals.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Feb 16, 2013
1 parent ddca002 commit 3fe611e
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -2,6 +2,7 @@ source 'https://rubygems.org'
gemspec

gem 'flipper', :git => 'git://github.com/jnunemaker/flipper.git'
gem 'redis-namespace', :require => false
gem 'rake'
gem 'rspec'

Expand Down
65 changes: 65 additions & 0 deletions README.md
Expand Up @@ -26,6 +26,71 @@ flipper = Flipper.new(adapter)
# profit...
```

## Internals

Currently, each feature is stored in a hash. Getting a feature is therefore one redis call to get all the keys for the feature.

```ruby
require 'flipper/adapters/redis'
require 'redis/namespace'

client = Redis.new
namespaced_client = Redis::Namespace.new(:flipper, :redis => client)
adapter = Flipper::Adapters::Redis.new(namespaced_client)
flipper = Flipper.new(adapter)

# Register a few groups.
Flipper.register(:admins) { |thing| thing.admin? }
Flipper.register(:early_access) { |thing| thing.early_access? }

# Create a user class that has flipper_id instance method.
User = Struct.new(:flipper_id)

flipper[:stats].enable
flipper[:stats].enable flipper.group(:admins)
flipper[:stats].enable flipper.group(:early_access)
flipper[:stats].enable User.new('25')
flipper[:stats].enable User.new('90')
flipper[:stats].enable User.new('180')
flipper[:stats].enable flipper.random(15)
flipper[:stats].enable flipper.actors(45)

flipper[:search].enable

print '# all keys: '
pp namespaced_client.keys
puts
# all keys: ["stats", "flipper_features", "search"]

puts '# stats keys'
pp namespaced_client.hgetall('stats')
puts
# stats keys
# {"boolean"=>"true",
# "groups/admins"=>"1",
# "actors/25"=>"1",
# "percentage_of_random"=>"15",
# "percentage_of_actors"=>"45",
# "groups/early_access"=>"1",
# "actors/90"=>"1",
# "actors/180"=>"1"}

puts '# search keys'
pp namespaced_client.hgetall('search')
puts
# search keys
# {"boolean"=>"true"}

puts '# flipper get of feature'
pp adapter.get(flipper[:stats])
# flipper get of feature
# {:boolean=>"true",
# :groups=>#<Set: {"admins", "early_access"}>,
# :actors=>#<Set: {"25", "90", "180"}>,
# :percentage_of_actors=>"45",
# :percentage_of_random=>"15"}
```

## Contributing

1. Fork it
Expand Down
27 changes: 27 additions & 0 deletions examples/basic.rb
@@ -0,0 +1,27 @@
require 'pathname'
require 'logger'

root_path = Pathname(__FILE__).dirname.join('..').expand_path
lib_path = root_path.join('lib')
$:.unshift(lib_path)

require 'flipper/adapters/redis'
client = Redis.new
adapter = Flipper::Adapters::Redis.new(client)
flipper = Flipper.new(adapter)

flipper[:stats].enable

if flipper[:stats].enabled?
puts "Enabled!"
else
puts "Disabled!"
end

flipper[:stats].disable

if flipper[:stats].enabled?
puts "Enabled!"
else
puts "Disabled!"
end
66 changes: 66 additions & 0 deletions examples/internals.rb
@@ -0,0 +1,66 @@
require 'pp'
require 'pathname'
require 'logger'

root_path = Pathname(__FILE__).dirname.join('..').expand_path
lib_path = root_path.join('lib')
$:.unshift(lib_path)

require 'flipper/adapters/redis'
require 'redis/namespace'

client = Redis.new
namespaced_client = Redis::Namespace.new(:flipper, :redis => client)
adapter = Flipper::Adapters::Redis.new(namespaced_client)
flipper = Flipper.new(adapter)

# Register a few groups.
Flipper.register(:admins) { |thing| thing.admin? }
Flipper.register(:early_access) { |thing| thing.early_access? }

# Create a user class that has flipper_id instance method.
User = Struct.new(:flipper_id)

flipper[:stats].enable
flipper[:stats].enable flipper.group(:admins)
flipper[:stats].enable flipper.group(:early_access)
flipper[:stats].enable User.new('25')
flipper[:stats].enable User.new('90')
flipper[:stats].enable User.new('180')
flipper[:stats].enable flipper.random(15)
flipper[:stats].enable flipper.actors(45)

flipper[:search].enable

print '# all keys: '
pp namespaced_client.keys
puts
# all keys: ["stats", "flipper_features", "search"]

puts '# stats keys'
pp namespaced_client.hgetall('stats')
puts
# stats keys
# {"boolean"=>"true",
# "groups/admins"=>"1",
# "actors/25"=>"1",
# "percentage_of_random"=>"15",
# "percentage_of_actors"=>"45",
# "groups/early_access"=>"1",
# "actors/90"=>"1",
# "actors/180"=>"1"}

puts '# search keys'
pp namespaced_client.hgetall('search')
puts
# search keys
# {"boolean"=>"true"}

puts '# flipper get of feature'
pp adapter.get(flipper[:stats])
# flipper get of feature
# {:boolean=>"true",
# :groups=>#<Set: {"admins", "early_access"}>,
# :actors=>#<Set: {"25", "90", "180"}>,
# :percentage_of_actors=>"45",
# :percentage_of_random=>"15"}
3 changes: 3 additions & 0 deletions lib/flipper/adapters/redis.rb
Expand Up @@ -13,6 +13,9 @@ class Redis
# Public: The name of the adapter.
attr_reader :name

# Public: Initializes a Redis flipper adapter.
#
# client - The Redis client to use. Feel free to namespace it.
def initialize(client)
@client = client
@name = :redis
Expand Down

0 comments on commit 3fe611e

Please sign in to comment.