flipper 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/Changelog.md +12 -0
- data/Gemfile +4 -7
- data/Guardfile +16 -4
- data/README.md +63 -34
- data/examples/basic.rb +1 -1
- data/examples/dsl.rb +10 -12
- data/examples/group.rb +10 -4
- data/examples/individual_actor.rb +9 -6
- data/examples/instrumentation.rb +39 -0
- data/examples/percentage_of_actors.rb +12 -9
- data/examples/percentage_of_random.rb +4 -2
- data/lib/flipper.rb +43 -10
- data/lib/flipper/adapter.rb +106 -21
- data/lib/flipper/adapters/memoized.rb +7 -0
- data/lib/flipper/adapters/memory.rb +10 -3
- data/lib/flipper/adapters/operation_logger.rb +7 -0
- data/lib/flipper/dsl.rb +73 -16
- data/lib/flipper/errors.rb +6 -0
- data/lib/flipper/feature.rb +117 -19
- data/lib/flipper/gate.rb +72 -4
- data/lib/flipper/gates/actor.rb +41 -12
- data/lib/flipper/gates/boolean.rb +21 -11
- data/lib/flipper/gates/group.rb +45 -12
- data/lib/flipper/gates/percentage_of_actors.rb +29 -10
- data/lib/flipper/gates/percentage_of_random.rb +22 -9
- data/lib/flipper/instrumentation/log_subscriber.rb +107 -0
- data/lib/flipper/instrumentation/metriks.rb +6 -0
- data/lib/flipper/instrumentation/metriks_subscriber.rb +92 -0
- data/lib/flipper/instrumenters/memory.rb +25 -0
- data/lib/flipper/instrumenters/noop.rb +9 -0
- data/lib/flipper/key.rb +23 -4
- data/lib/flipper/registry.rb +22 -6
- data/lib/flipper/spec/shared_adapter_specs.rb +59 -12
- data/lib/flipper/toggle.rb +19 -2
- data/lib/flipper/toggles/boolean.rb +36 -3
- data/lib/flipper/toggles/set.rb +9 -3
- data/lib/flipper/toggles/value.rb +9 -3
- data/lib/flipper/type.rb +1 -0
- data/lib/flipper/types/actor.rb +12 -14
- data/lib/flipper/types/percentage.rb +8 -2
- data/lib/flipper/version.rb +1 -1
- data/spec/flipper/adapter_spec.rb +163 -27
- data/spec/flipper/adapters/memoized_spec.rb +6 -6
- data/spec/flipper/dsl_spec.rb +51 -54
- data/spec/flipper/feature_spec.rb +179 -17
- data/spec/flipper/gate_spec.rb +47 -0
- data/spec/flipper/gates/actor_spec.rb +52 -0
- data/spec/flipper/gates/boolean_spec.rb +52 -0
- data/spec/flipper/gates/group_spec.rb +79 -0
- data/spec/flipper/gates/percentage_of_actors_spec.rb +98 -0
- data/spec/flipper/gates/percentage_of_random_spec.rb +54 -0
- data/spec/flipper/instrumentation/log_subscriber_spec.rb +104 -0
- data/spec/flipper/instrumentation/metriks_subscriber_spec.rb +69 -0
- data/spec/flipper/instrumenters/memory_spec.rb +26 -0
- data/spec/flipper/instrumenters/noop_spec.rb +22 -0
- data/spec/flipper/key_spec.rb +8 -2
- data/spec/flipper/registry_spec.rb +20 -2
- data/spec/flipper/toggle_spec.rb +22 -0
- data/spec/flipper/toggles/boolean_spec.rb +40 -0
- data/spec/flipper/toggles/set_spec.rb +35 -0
- data/spec/flipper/toggles/value_spec.rb +55 -0
- data/spec/flipper/types/actor_spec.rb +28 -33
- data/spec/flipper_spec.rb +16 -3
- data/spec/helper.rb +37 -3
- data/spec/integration_spec.rb +90 -83
- metadata +40 -4
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Changelog.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
## 0.4.0
|
2
|
+
|
3
|
+
* No longer use #id for detecting actors. You must now define #flipper_id on
|
4
|
+
anything that you would like to behave as an actor.
|
5
|
+
* Strings are now used instead of Integers for Actor identifiers. More flexible
|
6
|
+
and the only reason I used Integers was to do modulo for percentage of actors.
|
7
|
+
Since percentage of actors now uses hashing, integer is no longer needed.
|
8
|
+
* Easy integration of instrumentation with AS::Notifications or anything similar.
|
9
|
+
* A bunch of stuff around inspecting and getting names/descriptions out of
|
10
|
+
things to more easily figure out what is going on.
|
11
|
+
* Percentage of actors hash is now also seeded with feature name so the same
|
12
|
+
actors don't get all features instantly.
|
data/Gemfile
CHANGED
@@ -2,17 +2,14 @@ source 'https://rubygems.org'
|
|
2
2
|
gemspec
|
3
3
|
|
4
4
|
gem 'rake'
|
5
|
+
gem 'metriks', :require => false
|
6
|
+
gem 'rspec'
|
7
|
+
gem 'rack-test'
|
8
|
+
gem 'activesupport', :require => false
|
5
9
|
|
6
10
|
group(:guard) do
|
7
11
|
gem 'guard'
|
8
12
|
gem 'guard-rspec'
|
9
13
|
gem 'guard-bundler'
|
10
|
-
gem 'terminal-notifier-guard'
|
11
14
|
gem 'rb-fsevent'
|
12
15
|
end
|
13
|
-
|
14
|
-
group(:test) do
|
15
|
-
gem 'rspec'
|
16
|
-
gem 'rack-test'
|
17
|
-
end
|
18
|
-
|
data/Guardfile
CHANGED
@@ -6,8 +6,20 @@ guard 'bundler' do
|
|
6
6
|
watch(/^.+\.gemspec/)
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
rspec_options = {
|
10
|
+
:all_after_pass => false,
|
11
|
+
:all_on_start => false,
|
12
|
+
:keep_failed => false,
|
13
|
+
}
|
14
|
+
|
15
|
+
guard 'rspec', rspec_options do
|
16
|
+
watch(%r{^spec/.+_spec\.rb$})
|
17
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
18
|
+
watch(%r{shared_adapter_specs\.rb$}) { |m|
|
19
|
+
[
|
20
|
+
"spec/flipper/adapters/memory_spec.rb",
|
21
|
+
"spec/flipper/adapters/memoized_spec.rb",
|
22
|
+
]
|
23
|
+
}
|
24
|
+
watch('spec/helper.rb') { "spec" }
|
13
25
|
end
|
data/README.md
CHANGED
@@ -2,23 +2,11 @@
|
|
2
2
|
|
3
3
|
Feature flipping is the act of enabling or disabling features or parts of your application, ideally without re-deploying or changing anything in your code base.
|
4
4
|
|
5
|
-
The goal of this gem is to make turning features on or off so easy that everyone does it. Whatever your data store, throughput, or experience, feature flipping should be easy and
|
6
|
-
|
7
|
-
## Note
|
8
|
-
|
9
|
-
This project is a work in progress and not ready for production yet, but will be very soon.
|
10
|
-
|
11
|
-
## Why not use <insert gem name here, most likely rollout>?
|
12
|
-
|
13
|
-
I've used rollout extensively in the past and it was fantastic. The main reason I reinvented the wheel to some extent is:
|
14
|
-
|
15
|
-
* API - For whatever reason, I could never remember the API for rollout.
|
16
|
-
* Adapter Based - Rather than force redis, if you can implement a few simple methods, you can use the data store of your choice to power your flippers (memory, file system, mongo, redis, sql, etc.). It is also dead simple to front your data store with memcache if you so desire since feature checking is read heavy, as opposed to write heavy.
|
5
|
+
The goal of this gem is to make turning features on or off so easy that everyone does it. Whatever your data store, throughput, or experience, feature flipping should be easy and have minimal impact on your application.
|
17
6
|
|
18
7
|
## Coming Soon™
|
19
8
|
|
20
|
-
* Web UI (think resque UI for features toggling/status)
|
21
|
-
* Optimizations for per request in process caching of trips to the adapter
|
9
|
+
* [Web UI](https://github.com/jnunemaker/flipper-ui) (think resque UI for features toggling/status)
|
22
10
|
|
23
11
|
## Usage
|
24
12
|
|
@@ -26,9 +14,9 @@ The goal of the API for flipper was to have everything revolve around features a
|
|
26
14
|
|
27
15
|
```ruby
|
28
16
|
require 'flipper'
|
29
|
-
require 'flipper/adapters/memory'
|
30
17
|
|
31
18
|
# pick an adapter
|
19
|
+
require 'flipper/adapters/memory'
|
32
20
|
adapter = Flipper::Adapters::Memory.new
|
33
21
|
|
34
22
|
# get a handy dsl instance
|
@@ -92,39 +80,56 @@ There is no requirement that the thing yielded to the block be a user model or w
|
|
92
80
|
|
93
81
|
### 3. Individual Actor
|
94
82
|
|
95
|
-
Turn on for individual thing. Think enable feature for someone to test or for a buddy.
|
83
|
+
Turn feature on for individual thing. Think enable feature for someone to test or for a buddy. The only requirement for an individual actor is that it must respond to `flipper_id`.
|
96
84
|
|
97
85
|
```ruby
|
98
86
|
flipper = Flipper.new(adapter)
|
99
87
|
|
100
|
-
|
101
|
-
|
88
|
+
flipper[:stats].enable user
|
89
|
+
flipper[:stats].enabled? user # true
|
102
90
|
|
103
|
-
flipper[:stats].
|
104
|
-
flipper[:stats].enabled?
|
91
|
+
flipper[:stats].disable user
|
92
|
+
flipper[:stats].enabled? user # false
|
105
93
|
|
106
|
-
|
107
|
-
flipper[:
|
94
|
+
# you can enable anything, does not need to be user or person
|
95
|
+
flipper[:search].enable group
|
96
|
+
flipper[:search].enabled? group
|
97
|
+
```
|
98
|
+
|
99
|
+
The key is to make sure you do not enable two different types of objects for the same feature. Imagine that user has a flipper_id of 6 and group has a flipper_id of 6. Enabling search for user would automatically enable it for group, as they both have a flipper_id of 6.
|
100
|
+
|
101
|
+
The one exception to this rule is if you have globally unique flipper_ids, such as uuid's. If your flipper_ids are unique globally in your entire system, enabling two different types should be safe. Another way around this is to prefix the flipper_id with the class name like this:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
class User
|
105
|
+
def flipper_id
|
106
|
+
"User:#{id}"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
class Group
|
111
|
+
def flipper_id
|
112
|
+
"Group:#{id}"
|
113
|
+
end
|
114
|
+
end
|
108
115
|
```
|
109
116
|
|
110
117
|
### 4. Percentage of Actors
|
111
118
|
|
112
|
-
Turn this on for a percentage of actors (think
|
119
|
+
Turn this on for a percentage of actors (think user, member, account, group, whatever). Consistently on or off for this user as long as percentage increases. Think slow rollout of a new feature to a percentage of things.
|
113
120
|
|
114
121
|
```ruby
|
115
122
|
flipper = Flipper.new(adapter)
|
116
123
|
|
117
|
-
# convert user or person or whatever to flipper actor for checking if in percentage
|
118
|
-
actor = flipper.actor(user.id)
|
119
|
-
|
120
124
|
# returns a percentage of actors instance set to 10
|
121
125
|
percentage = flipper.actors(10)
|
122
126
|
|
123
127
|
# turn stats on for 10 percent of users in the system
|
124
128
|
flipper[:stats].enable percentage
|
125
129
|
|
126
|
-
# checks if actor's
|
127
|
-
|
130
|
+
# checks if actor's flipper_id is in the enabled percentage by hashing
|
131
|
+
# user.flipper_id.to_s to ensure enabled distribution is smooth
|
132
|
+
flipper[:stats].enabled? user
|
128
133
|
|
129
134
|
```
|
130
135
|
|
@@ -144,24 +149,48 @@ percentage = flipper.random(5)
|
|
144
149
|
flipper[:logging].enable percentage
|
145
150
|
```
|
146
151
|
|
147
|
-
Randomness is
|
148
|
-
|
152
|
+
Randomness is not a good idea for enabling new features in the UI. Most of the time you want a feature on or off for a user, but there are definitely times when I have found percentage of random to be very useful.
|
149
153
|
|
150
154
|
## Adapters
|
151
155
|
|
152
|
-
I plan on supporting in-memory, Mongo, and Redis as adapters for flipper. Others are welcome so please let me know if you create one.
|
156
|
+
I plan on supporting [in-memory](https://github.com/jnunemaker/flipper/blob/master/lib/flipper/adapters/memory.rb), [Mongo](https://github.com/jnunemaker/flipper-mongo), and [Redis](https://github.com/jnunemaker/flipper-redis) as adapters for flipper. Others are welcome so please let me know if you create one.
|
153
157
|
|
154
158
|
### Memory
|
155
159
|
|
156
|
-
You can use
|
160
|
+
You can use the [in-memory adapter](https://github.com/jnunemaker/flipper/blob/master/lib/flipper/adapters/memory.rb) for tests if you want. That is pretty much all I use it for.
|
157
161
|
|
158
162
|
### Mongo
|
159
163
|
|
160
|
-
Currently, the mongo adapter
|
164
|
+
Currently, the [mongo adapter](https://github.com/jnunemaker/flipper-mongo) comes in two flavors.
|
165
|
+
|
166
|
+
The [vanilla mongo adapter](https://github.com/jnunemaker/flipper-mongo/blob/master/lib/flipper/adapters/mongo.rb) stores each key in its own document. This means for each gate checked per feature there will be a query to mongo.
|
167
|
+
|
168
|
+
Personally, the adapter I prefer is the [single document adapter](https://github.com/jnunemaker/flipper-mongo/blob/master/lib/flipper/adapters/mongo_single_document.rb), which stores all features and gates in a single document. If you combine this adapter with the [local cache middleware](https://github.com/jnunemaker/flipper/blob/master/lib/flipper/middleware/local_cache.rb), the document will only be queried once per request, which is pretty awesome.
|
161
169
|
|
162
170
|
### Redis
|
163
171
|
|
164
|
-
Redis is great for this type of stuff and it only took a few minutes to implement. The only real problem with redis right now is that automated failover isn't that easy so relying on it for every code path in my app would make me nervous.
|
172
|
+
Redis is great for this type of stuff and it only took a few minutes to implement a [redis adapter](https://github.com/jnunemaker/flipper-redis). The only real problem with redis right now is that automated failover isn't that easy so relying on it for every code path in my app would make me nervous.
|
173
|
+
|
174
|
+
## Optimization
|
175
|
+
|
176
|
+
One optimization that flipper provides is a local cache. Once you have a flipper instance you can use the local cache to store each adapter key lookup in memory for as long as you want.
|
177
|
+
|
178
|
+
Out of the box there is a middleware that will store each key lookup for the duration of the request. This means you will only actually query your adapter's data store once per feature per gate that is checked. You can use the middleware from a Rails initializer like so:
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
require 'flipper/middleware/local_cache'
|
182
|
+
|
183
|
+
# create flipper dsl instance, see above examples for more details
|
184
|
+
flipper = Flipper.new(...)
|
185
|
+
|
186
|
+
# ensure entire request is wrapped, `use` would probably be ok instead of
|
187
|
+
# `insert_after`, but I noticed that Rails used `insert_after` for their
|
188
|
+
# identity map, which this is akin to, and figured it was for a reason.
|
189
|
+
Rails.application.config.middleware.insert_after \
|
190
|
+
ActionDispatch::Callbacks,
|
191
|
+
Flipper::Middleware::LocalCache,
|
192
|
+
flipper
|
193
|
+
```
|
165
194
|
|
166
195
|
## Installation
|
167
196
|
|
data/examples/basic.rb
CHANGED
data/examples/dsl.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require File.expand_path('../example_setup', __FILE__)
|
2
2
|
|
3
3
|
require 'flipper'
|
4
4
|
require 'flipper/adapters/memory'
|
@@ -13,6 +13,9 @@ class Person
|
|
13
13
|
def initialize(id)
|
14
14
|
@id = id
|
15
15
|
end
|
16
|
+
|
17
|
+
# Must respond to flipper_id
|
18
|
+
alias_method :flipper_id, :id
|
16
19
|
end
|
17
20
|
|
18
21
|
person = Person.new(1)
|
@@ -53,9 +56,7 @@ flipper.disable :stats
|
|
53
56
|
stats.disable
|
54
57
|
|
55
58
|
puts "stats.enabled?: #{stats.enabled?}"
|
56
|
-
puts "stats.disabled?: #{stats.disabled?}"
|
57
59
|
puts "stats.enabled? person: #{stats.enabled? person}"
|
58
|
-
puts "stats.disabled? person: #{stats.disabled? person}"
|
59
60
|
puts
|
60
61
|
|
61
62
|
# get an instance of the percentage of random type set to 5
|
@@ -64,16 +65,13 @@ puts flipper.random(5).inspect
|
|
64
65
|
# get an instance of the percentage of actors type set to 15
|
65
66
|
puts flipper.actors(15).inspect
|
66
67
|
|
67
|
-
# get an instance of an actor using an object that responds to
|
68
|
-
|
69
|
-
puts flipper.actor(
|
70
|
-
|
71
|
-
# get an instance of an actor using an object that responds to identifier
|
72
|
-
responds_to_identifier = Struct.new(:identifier).new(11)
|
73
|
-
puts flipper.actor(responds_to_identifier).inspect
|
68
|
+
# get an instance of an actor using an object that responds to flipper_id
|
69
|
+
responds_to_flipper_id = Struct.new(:flipper_id).new(10)
|
70
|
+
puts flipper.actor(responds_to_flipper_id).inspect
|
74
71
|
|
75
|
-
# get an instance of an actor using
|
76
|
-
|
72
|
+
# get an instance of an actor using an object
|
73
|
+
thing = Struct.new(:flipper_id).new(22)
|
74
|
+
puts flipper.actor(thing).inspect
|
77
75
|
|
78
76
|
# register a top level group
|
79
77
|
admins = Flipper.register(:admins) { |actor|
|
data/examples/group.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require File.expand_path('../example_setup', __FILE__)
|
2
2
|
|
3
3
|
require 'flipper'
|
4
4
|
require 'flipper/adapters/memory'
|
@@ -14,17 +14,23 @@ end
|
|
14
14
|
|
15
15
|
# Some class that represents actor that will be trying to do something
|
16
16
|
class User
|
17
|
-
|
17
|
+
attr_reader :id
|
18
|
+
|
19
|
+
def initialize(id, admin)
|
20
|
+
@id = id
|
18
21
|
@admin = admin
|
19
22
|
end
|
20
23
|
|
24
|
+
# Must respond to flipper_id
|
25
|
+
alias_method :flipper_id, :id
|
26
|
+
|
21
27
|
def admin?
|
22
28
|
@admin == true
|
23
29
|
end
|
24
30
|
end
|
25
31
|
|
26
|
-
admin = User.new(true)
|
27
|
-
non_admin = User.new(false)
|
32
|
+
admin = User.new(1, true)
|
33
|
+
non_admin = User.new(2, false)
|
28
34
|
|
29
35
|
puts "Stats for admin: #{stats.enabled?(admin)}"
|
30
36
|
puts "Stats for non_admin: #{stats.enabled?(non_admin)}"
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require File.expand_path('../example_setup', __FILE__)
|
2
2
|
|
3
3
|
require 'flipper'
|
4
4
|
require 'flipper/adapters/memory'
|
@@ -14,16 +14,19 @@ class User
|
|
14
14
|
def initialize(id)
|
15
15
|
@id = id
|
16
16
|
end
|
17
|
+
|
18
|
+
# Must respond to flipper_id
|
19
|
+
alias_method :flipper_id, :id
|
17
20
|
end
|
18
21
|
|
19
22
|
user1 = User.new(1)
|
20
23
|
user2 = User.new(2)
|
21
24
|
|
22
|
-
puts "Stats for user1: #{stats.enabled?(
|
23
|
-
puts "Stats for user2: #{stats.enabled?(
|
25
|
+
puts "Stats for user1: #{stats.enabled?(user1)}"
|
26
|
+
puts "Stats for user2: #{stats.enabled?(user2)}"
|
24
27
|
|
25
28
|
puts "\nEnabling stats for user1...\n\n"
|
26
|
-
stats.enable(
|
29
|
+
stats.enable(user1)
|
27
30
|
|
28
|
-
puts "Stats for user1: #{stats.enabled?(
|
29
|
-
puts "Stats for user2: #{stats.enabled?(
|
31
|
+
puts "Stats for user1: #{stats.enabled?(user1)}"
|
32
|
+
puts "Stats for user2: #{stats.enabled?(user2)}"
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.expand_path('../example_setup', __FILE__)
|
2
|
+
|
3
|
+
require 'securerandom'
|
4
|
+
require 'active_support/notifications'
|
5
|
+
|
6
|
+
class FlipperSubscriber
|
7
|
+
def call(*args)
|
8
|
+
event = ActiveSupport::Notifications::Event.new(*args)
|
9
|
+
puts event.inspect
|
10
|
+
end
|
11
|
+
|
12
|
+
ActiveSupport::Notifications.subscribe(/flipper/, new)
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'flipper'
|
16
|
+
require 'flipper/adapters/memory'
|
17
|
+
|
18
|
+
# pick an adapter
|
19
|
+
adapter = Flipper::Adapters::Memory.new
|
20
|
+
|
21
|
+
# get a handy dsl instance
|
22
|
+
flipper = Flipper.new(adapter, :instrumenter => ActiveSupport::Notifications)
|
23
|
+
|
24
|
+
# grab a feature
|
25
|
+
search = flipper[:search]
|
26
|
+
|
27
|
+
perform = lambda do
|
28
|
+
# check if that feature is enabled
|
29
|
+
if search.enabled?
|
30
|
+
puts 'Search away!'
|
31
|
+
else
|
32
|
+
puts 'No search for you!'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
perform.call
|
37
|
+
puts 'Enabling Search...'
|
38
|
+
search.enable
|
39
|
+
perform.call
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require File.expand_path('../example_setup', __FILE__)
|
2
2
|
|
3
3
|
require 'flipper'
|
4
4
|
require 'flipper/adapters/memory'
|
@@ -14,22 +14,25 @@ class User
|
|
14
14
|
def initialize(id)
|
15
15
|
@id = id
|
16
16
|
end
|
17
|
+
|
18
|
+
# Must respond to flipper_id
|
19
|
+
alias_method :flipper_id, :id
|
17
20
|
end
|
18
21
|
|
19
22
|
pitt = User.new(1)
|
20
23
|
clooney = User.new(10)
|
21
24
|
|
22
|
-
puts "Stats for pitt: #{stats.enabled?(
|
23
|
-
puts "Stats for clooney: #{stats.enabled?(
|
25
|
+
puts "Stats for pitt: #{stats.enabled?(pitt)}"
|
26
|
+
puts "Stats for clooney: #{stats.enabled?(clooney)}"
|
24
27
|
|
25
28
|
puts "\nEnabling stats for 5 percent...\n\n"
|
26
29
|
stats.enable(Flipper::Types::PercentageOfActors.new(5))
|
27
30
|
|
28
|
-
puts "Stats for pitt: #{stats.enabled?(
|
29
|
-
puts "Stats for clooney: #{stats.enabled?(
|
31
|
+
puts "Stats for pitt: #{stats.enabled?(pitt)}"
|
32
|
+
puts "Stats for clooney: #{stats.enabled?(clooney)}"
|
30
33
|
|
31
|
-
puts "\nEnabling stats for
|
32
|
-
stats.enable(Flipper::Types::PercentageOfActors.new(
|
34
|
+
puts "\nEnabling stats for 50 percent...\n\n"
|
35
|
+
stats.enable(Flipper::Types::PercentageOfActors.new(50))
|
33
36
|
|
34
|
-
puts "Stats for pitt: #{stats.enabled?(
|
35
|
-
puts "Stats for clooney: #{stats.enabled?(
|
37
|
+
puts "Stats for pitt: #{stats.enabled?(pitt)}"
|
38
|
+
puts "Stats for clooney: #{stats.enabled?(clooney)}"
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require File.expand_path('../example_setup', __FILE__)
|
2
2
|
|
3
3
|
require 'flipper'
|
4
4
|
require 'flipper/adapters/memory'
|
@@ -25,9 +25,11 @@ perform_test = lambda do |number|
|
|
25
25
|
actual = (enabled.size / total.to_f * 100).round(2)
|
26
26
|
|
27
27
|
# puts "#{enabled.size} / #{total}"
|
28
|
-
puts "percentage: #{actual} vs #{number}"
|
28
|
+
puts "percentage: #{actual.to_s.rjust(6, ' ')} vs #{number.to_s.rjust(3, ' ')}"
|
29
29
|
end
|
30
30
|
|
31
|
+
puts "percentage: Actual vs Hoped For"
|
32
|
+
|
31
33
|
[1, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 99, 100].each do |number|
|
32
34
|
perform_test.call number
|
33
35
|
end
|
data/lib/flipper.rb
CHANGED
@@ -1,26 +1,59 @@
|
|
1
1
|
module Flipper
|
2
|
-
|
3
|
-
|
4
|
-
end
|
5
|
-
|
6
|
-
def self.groups
|
7
|
-
@groups ||= Registry.new
|
8
|
-
end
|
2
|
+
# Private: The namespace for all instrumented events.
|
3
|
+
InstrumentationNamespace = :flipper
|
9
4
|
|
10
|
-
|
11
|
-
|
5
|
+
# Public: Start here. Given an adapter returns a handy DSL to all the flipper
|
6
|
+
# goodness. To see supported options, check out dsl.rb.
|
7
|
+
def self.new(adapter, options = {})
|
8
|
+
DSL.new(adapter, options)
|
12
9
|
end
|
13
10
|
|
11
|
+
# Public: Use this to register a group by name.
|
12
|
+
#
|
13
|
+
# name - The Symbol name of the group.
|
14
|
+
# block - The block that should be used to determine if the group matches a
|
15
|
+
# given thing.
|
16
|
+
#
|
17
|
+
# Examples
|
18
|
+
#
|
19
|
+
# Flipper.registry(:admins) { |thing|
|
20
|
+
# thing.respond_to?(:admin?) && thing.admin?
|
21
|
+
# }
|
22
|
+
#
|
23
|
+
# Returns a Flipper::Group.
|
24
|
+
# Raises Flipper::DuplicateGroup if the group is already registered.
|
14
25
|
def self.register(name, &block)
|
15
26
|
group = Types::Group.new(name, &block)
|
16
27
|
groups.add(group.name, group)
|
17
28
|
group
|
18
29
|
rescue Registry::DuplicateKey
|
19
|
-
raise DuplicateGroup, %Q{Group
|
30
|
+
raise DuplicateGroup, %Q{Group #{name.inspect} has already been registered}
|
20
31
|
end
|
21
32
|
|
33
|
+
# Internal: Fetches a group by name.
|
34
|
+
#
|
35
|
+
# name - The Symbol name of the group.
|
36
|
+
#
|
37
|
+
# Examples
|
38
|
+
#
|
39
|
+
# Flipper.group(:admins)
|
40
|
+
#
|
41
|
+
# Returns the Flipper::Group if group registered.
|
42
|
+
# Raises Flipper::GroupNotRegistered if group is not registered.
|
22
43
|
def self.group(name)
|
23
44
|
groups.get(name)
|
45
|
+
rescue Flipper::Registry::KeyNotFound => e
|
46
|
+
raise GroupNotRegistered, "Group #{e.key.inspect} has not been registered"
|
47
|
+
end
|
48
|
+
|
49
|
+
# Internal: Registry of all groups.
|
50
|
+
def self.groups
|
51
|
+
@groups ||= Registry.new
|
52
|
+
end
|
53
|
+
|
54
|
+
# Internal: Change the groups registry.
|
55
|
+
def self.groups=(registry)
|
56
|
+
@groups = registry
|
24
57
|
end
|
25
58
|
end
|
26
59
|
|