flipper 0.20.1 → 0.21.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +57 -0
  3. data/Changelog.md +66 -0
  4. data/Gemfile +1 -0
  5. data/README.md +103 -47
  6. data/docs/Adapters.md +9 -9
  7. data/docs/Caveats.md +2 -2
  8. data/docs/Gates.md +74 -74
  9. data/docs/Optimization.md +70 -47
  10. data/docs/http/README.md +12 -11
  11. data/docs/images/banner.jpg +0 -0
  12. data/docs/read-only/README.md +8 -5
  13. data/examples/basic.rb +1 -12
  14. data/examples/configuring_default.rb +2 -5
  15. data/examples/dsl.rb +13 -24
  16. data/examples/enabled_for_actor.rb +8 -15
  17. data/examples/group.rb +3 -6
  18. data/examples/group_dynamic_lookup.rb +5 -19
  19. data/examples/group_with_members.rb +4 -14
  20. data/examples/importing.rb +1 -1
  21. data/examples/individual_actor.rb +2 -5
  22. data/examples/instrumentation.rb +1 -2
  23. data/examples/memoizing.rb +3 -7
  24. data/examples/percentage_of_actors.rb +6 -16
  25. data/examples/percentage_of_actors_enabled_check.rb +7 -10
  26. data/examples/percentage_of_actors_group.rb +5 -18
  27. data/examples/percentage_of_time.rb +3 -6
  28. data/lib/flipper.rb +4 -1
  29. data/lib/flipper/adapters/http.rb +32 -28
  30. data/lib/flipper/adapters/memory.rb +20 -94
  31. data/lib/flipper/adapters/pstore.rb +4 -0
  32. data/lib/flipper/adapters/sync/interval_synchronizer.rb +1 -1
  33. data/lib/flipper/configuration.rb +33 -7
  34. data/lib/flipper/errors.rb +2 -3
  35. data/lib/flipper/identifier.rb +17 -0
  36. data/lib/flipper/middleware/memoizer.rb +29 -14
  37. data/lib/flipper/railtie.rb +38 -0
  38. data/lib/flipper/version.rb +1 -1
  39. data/spec/flipper/adapters/http_spec.rb +74 -8
  40. data/spec/flipper/adapters/memory_spec.rb +21 -1
  41. data/spec/flipper/configuration_spec.rb +20 -2
  42. data/spec/flipper/identifier_spec.rb +14 -0
  43. data/spec/flipper/middleware/memoizer_spec.rb +95 -35
  44. data/spec/flipper/middleware/setup_env_spec.rb +0 -16
  45. data/spec/flipper/railtie_spec.rb +69 -0
  46. data/spec/flipper_spec.rb +0 -1
  47. data/spec/support/spec_helpers.rb +20 -0
  48. data/test/test_helper.rb +1 -0
  49. metadata +12 -5
  50. data/examples/example_setup.rb +0 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3419ab68fa52334e5a6f71463c213a039f819439dc71c836c8c3b70b4c80270b
4
- data.tar.gz: d6ade104432d5f5eaab36f00ccd0b559ee9bd9af4772a224cb9f70d26f578062
3
+ metadata.gz: 1cdc35a74141977ee7aa3f92a8ddba338f9e268aee9ae9ed709c2f9c391cac8a
4
+ data.tar.gz: f4f387595c12a6219dea6f0451e5c47c2e12cfb3d830fd602f3e648e23a2d0ab
5
5
  SHA512:
6
- metadata.gz: 48bc857cb049418b020bc1216de81238df104321b62c7ff3b3967dfdc8dfedf18a1779d58c84db233cc67d9058a1b7beb295e671739a232f8384cf324879b950
7
- data.tar.gz: b5e0c90341f3b0a4f61a72ee26810ccdec216f32bd7e07b022f15ca04b075eb44a53bd392afdb91d7a868d3c724c96b2a962a37ed54fd5614a3a5f385e0ac147
6
+ metadata.gz: 314fe25d0dfd891c038c8296ab3f07dfe539fbbbd33bbf1e9fec93267048f8f979025d0166f1b81ee629aee1b06d19ae891ab6bce45ca74039b82ff4978a747a
7
+ data.tar.gz: 515e5b5a19e9394a50fab5c6d0bd93b55e9f7c05c49ab8d1c88b6d9ff838aa5b0e2e57980e25e668cda71659cefdcdb1ee3f282eb03b08e9769bdc37c236400f
@@ -0,0 +1,57 @@
1
+ name: CI
2
+ on:
3
+ push:
4
+ branches: [master]
5
+ pull_request:
6
+ jobs:
7
+ build:
8
+ runs-on: ubuntu-latest
9
+ services:
10
+ redis:
11
+ image: redis
12
+ ports: ['6379:6379']
13
+ options: >-
14
+ --health-cmd "redis-cli ping"
15
+ --health-interval 10s
16
+ --health-timeout 5s
17
+ --health-retries 5
18
+ strategy:
19
+ matrix:
20
+ ruby: ['2.5', '2.6', '2.7']
21
+ env:
22
+ RAILS_VERSION: 6.0.0
23
+ SQLITE3_VERSION: 1.4.1
24
+ REDIS_URL: redis://localhost:6379/0
25
+ CI: true
26
+ steps:
27
+ - name: Setup memcached
28
+ uses: KeisukeYamashita/memcached-actions@v1
29
+ - name: Start MongoDB
30
+ uses: supercharge/mongodb-github-action@1.3.0
31
+ with:
32
+ mongodb-version: 4.0
33
+ - name: Check out repository code
34
+ uses: actions/checkout@v2
35
+ - name: Do some action caching
36
+ uses: actions/cache@v1
37
+ with:
38
+ path: vendor/bundle
39
+ key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }}
40
+ restore-keys: |
41
+ ${{ runner.os }}-gem-
42
+ - name: Set up Ruby
43
+ uses: actions/setup-ruby@v1
44
+ with:
45
+ ruby-version: ${{ matrix.ruby }}
46
+ - name: Install libpq-dev
47
+ run: sudo apt-get -yqq install libpq-dev
48
+ - name: Install bundler
49
+ run: gem install bundler
50
+ - name: Run bundler
51
+ run: bundle install --jobs 4 --retry 3
52
+ - name: Run Rake
53
+ run: bundle exec rake
54
+ - name: Run Examples
55
+ env:
56
+ FLIPPER_CLOUD_TOKEN: ${{ secrets.FLIPPER_CLOUD_TOKEN }}
57
+ run: script/examples
data/Changelog.md CHANGED
@@ -1,3 +1,69 @@
1
+ ## 0.21.0
2
+
3
+ ### Additions/Changes
4
+
5
+ * Default to using memory adapter (https://github.com/jnunemaker/flipper/pull/501)
6
+ * Adapters now configured on require when possible (https://github.com/jnunemaker/flipper/pull/502)
7
+ * Added cloud recommendation to flipper-ui. Can be disabled with `Flipper::UI.configure { |config| config.cloud_recommendation = false }`. Just want to raise awareness that more is available if people want it (https://github.com/jnunemaker/flipper/pull/504)
8
+ * Added default `flipper_id` implementation via `Flipper::Identifier` and automatically included it in ActiveRecord and Sequel models (https://github.com/jnunemaker/flipper/pull/505)
9
+ * Deprecate superflous sync_method setting (https://github.com/jnunemaker/flipper/pull/511)
10
+ * Flipper is now pre-configured when used with Rails. By default, it will [memoize and preload all features for each request](docs/Optimization.md#memoization). (https://github.com/jnunemaker/flipper/pull/506)
11
+
12
+ ### Upgrading
13
+
14
+ You should be able to upgrade to 0.21 without any breaking changes. However, if you want to simplify your setup, you can remove some configuration that is now handled automatically:
15
+
16
+ 1. Adapters are configured when on require, so unless you are using caching or other customizations, you can remove adapter configuration.
17
+
18
+ ```diff
19
+ # config/initializers/flipper.rb
20
+ - Flipper.configure do |config|
21
+ - config.default { Flipper.new(Flipper::Adapters::ActiveRecord.new) }
22
+ - end
23
+ ```
24
+
25
+ 2. `Flipper::Middleware::Memoizer` will be enabled by default.
26
+
27
+ ```diff
28
+ # config/initializers/flipper.rb
29
+ - Rails.configuration.middleware.use Flipper::Middleware::Memoizer,
30
+ - preload: [:stats, :search, :some_feature]
31
+ + Rails.application.configure do
32
+ + # Uncomment to configure which features to preload on all requests
33
+ + # config.flipper.preload = [:stats, :search, :some_feature]
34
+ + end
35
+ ```
36
+
37
+ 3. `#flipper_id`, which is used to enable features for specific actors, is now defined by [Flipper::Identifier](lib/flipper/identifier.rb) on all ActiveRecord and Sequel models. You can remove your implementation if it is in the form of `ModelName;id`.
38
+
39
+ 4. When using `flipper-cloud`, The `Flipper::Cloud.app` webhook receiver is now mounted at `/_flipper` by default.
40
+
41
+ ```diff
42
+ # config/routes.rb
43
+ - mount Flipper::Cloud.app, at: "/_flipper"
44
+ ```
45
+
46
+ ## 0.20.4
47
+
48
+ ### Additions/Changes
49
+
50
+ * Allow actors and time gates to deal with decimal percentages (https://github.com/jnunemaker/flipper/pull/492)
51
+ * Change Flipper::Cloud::Middleware to receive webhooks at / in addition to /webhooks.
52
+ * Add `write_through` option to ActiveSupportCacheStore adapter to support write-through caching (https://github.com/jnunemaker/flipper/pull/512)
53
+
54
+ ## 0.20.3
55
+
56
+ ### Additions/Changes
57
+
58
+ * Changed the internal structure of how the memory adapter stores things.
59
+
60
+ ## 0.20.2
61
+
62
+ ### Additions/Changes
63
+
64
+ * Http adapter now raises error when enable/disable/add/remove/clear fail.
65
+ * Cloud adapter sends some extra info like hostname, ruby version, etc. for debugging and decision making.
66
+
1
67
  ## 0.20.1
2
68
 
3
69
  ### Additions/Changes
data/Gemfile CHANGED
@@ -18,6 +18,7 @@ gem 'minitest', '~> 5.8'
18
18
  gem 'minitest-documentation'
19
19
  gem 'webmock', '~> 3.0'
20
20
  gem 'climate_control'
21
+ gem 'redis-namespace'
21
22
 
22
23
  group(:guard) do
23
24
  gem 'guard', '~> 2.15'
data/README.md CHANGED
@@ -1,23 +1,17 @@
1
- <pre>
2
- __
3
- _.-~ )
4
- _..--~~~~,' ,-/ _
5
- .-'. . . .' ,-',' ,' )
6
- ,'. . . _ ,--~,-'__..-' ,'
7
- ,'. . . (@)' ---~~~~ ,'
8
- /. . . . '~~ ,-'
9
- /. . . . . ,-'
10
- ; . . . . - . ,'
11
- : . . . . _ /
12
- . . . . . `-.:
13
- . . . ./ - . )
14
- . . . | _____..---.._/ _____
15
- ~---~~~~----~~~~ ~~
16
- </pre>
17
-
18
- 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.
19
-
20
- 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.
1
+ [![Flipper Mark](docs/images/banner.jpg)](https://www.flippercloud.io)
2
+
3
+ # Flipper
4
+
5
+ > Beautiful, performant feature flags for Ruby.
6
+
7
+ Flipper gives you control over who has access to features in your app.
8
+
9
+ * Enable or disable features for everyone, specific actors, groups of actors, a percentage of actors, or a percentage of time.
10
+ * Configure your feature flags from the console or a web UI.
11
+ * Regardless of what data store you are using, Flipper can performantly store your feature flags.
12
+ * Use [Flipper Cloud](#flipper-cloud) to cascade features from multiple environments, share settings with your team, control permissions, keep an audit history, and rollback.
13
+
14
+ Control your software &mdash; don't let it control you.
21
15
 
22
16
  ## Installation
23
17
 
@@ -25,6 +19,10 @@ Add this line to your application's Gemfile:
25
19
 
26
20
  gem 'flipper'
27
21
 
22
+ You'll also want to pick a storage [adapter](#adapters), for example:
23
+
24
+ gem 'flipper-active_record'
25
+
28
26
  And then execute:
29
27
 
30
28
  $ bundle
@@ -33,50 +31,108 @@ Or install it yourself with:
33
31
 
34
32
  $ gem install flipper
35
33
 
36
- ## Examples
34
+ ## Getting Started
37
35
 
38
- The goal of the API for flipper was to have everything revolve around features and what ways they can be enabled. Start with top level and dig into a feature, then dig in further and enable that feature for a given type of access, as opposed to thinking about how the feature will be accessed first (ie: `stats.enable` vs `activate_group(:stats, ...)`).
36
+ Use `Flipper#enabled?` in your app to check if a feature is enabled.
39
37
 
40
38
  ```ruby
41
- require 'flipper'
42
-
43
- Flipper.configure do |config|
44
- config.default do
45
- # pick an adapter, this uses memory, any will do
46
- adapter = Flipper::Adapters::Memory.new
47
-
48
- # pass adapter to handy DSL instance
49
- Flipper.new(adapter)
50
- end
51
- end
52
-
53
39
  # check if search is enabled
54
- if Flipper.enabled?(:search)
40
+ if Flipper.enabled? :search, current_user
55
41
  puts 'Search away!'
56
42
  else
57
43
  puts 'No search for you!'
58
44
  end
45
+ ```
59
46
 
60
- puts 'Enabling Search...'
61
- Flipper.enable(:search)
47
+ All features are disabled by default, so you'll need to explicitly enable them.
62
48
 
63
- # check if search is enabled
64
- if Flipper.enabled?(:search)
65
- puts 'Search away!'
66
- else
67
- puts 'No search for you!'
49
+ #### Enable a feature for everyone
50
+
51
+ ```ruby
52
+ Flipper.enable :search
53
+ ```
54
+
55
+ #### Enable a feature for a specific actor
56
+
57
+ ```ruby
58
+ Flipper.enable_actor :search, current_user
59
+ ```
60
+
61
+ #### Enable a feature for a group of actors
62
+
63
+ First tell Flipper about your groups:
64
+
65
+ ```ruby
66
+ # config/initializers/flipper.rb
67
+ Flipper.register(:admin) do |actor|
68
+ actor.respond_to?(:admin?) && actor.admin?
68
69
  end
69
70
  ```
70
71
 
71
- Of course there are more [examples for you to peruse](examples/). You could also check out the [DSL](lib/flipper/dsl.rb) and [Feature](lib/flipper/feature.rb) classes for code/docs.
72
+ Then enable the feature for that group:
73
+
74
+ ```ruby
75
+ Flipper.enable_group :search, :admin
76
+ ```
77
+
78
+ #### Enable a feature for a percentage of actors
79
+
80
+ ```ruby
81
+ Flipper.enable_percentage_of_actors :search, 2
82
+ ```
83
+
84
+
85
+ Read more about enabling and disabling features with [Gates](docs/Gates.md). Check out the [examples directory](examples/) for more, and take a peek at the [DSL](lib/flipper/dsl.rb) and [Feature](lib/flipper/feature.rb) classes for code/docs.
86
+
87
+ ## Adapters
88
+
89
+ Flipper is built on adapters for maximum flexibility. Regardless of what data store you are using, Flipper can performantly store data in it.
90
+
91
+ Pick one of our [supported adapters](docs/Adapters.md#officially-supported) and follow the installation instructions:
92
+
93
+ * [Active Record](docs/active_record/README.md)
94
+ * [Sequel](docs/sequel/README.md)
95
+ * [Redis](docs/redis/README.md)
96
+ * [Mongo](docs/mongo/README.md)
97
+ * [Moneta](docs/moneta/README.md)
98
+ * [Rollout](docs/rollout/README.md)
99
+
100
+ Or [roll your own](docs/Adapters.md#roll-your-own). We even provide automatic (rspec and minitest) tests for you, so you know you've built your custom adapter correctly.
101
+
102
+ Read more about [Adapters](docs/Adapters.md).
103
+
104
+ ## Flipper UI
105
+
106
+ If you prefer a web UI to an IRB console, you can setup the [Flipper UI](docs/ui/README.md).
107
+
108
+ It's simple and pretty.
109
+
110
+ ![Flipper UI Screenshot](docs/ui/images/feature.png)
111
+
112
+
113
+
114
+ ## Flipper Cloud
115
+
116
+ Or, (even better than OSS + UI) use [Flipper Cloud](https://www.flippercloud.io) which comes with:
117
+
118
+ * **everything in one place** &mdash; no need to bounce around from different application UIs or IRB consoles.
119
+ * **permissions** &mdash; grant access to everyone in your organization or lockdown each project to particular people.
120
+ * **multiple environments** &mdash; production, staging, enterprise, by continent, whatever you need.
121
+ * **personal environments** &mdash; no more rake scripts or manual enable/disable to get your laptop to look like production. Every developer gets a personal environment that inherits from production that they can override as they please ([read more](https://www.johnnunemaker.com/flipper-cloud-environments/)).
122
+ * **no maintenance** &mdash; we'll keep the lights on for you. We also have handy webhooks for keeping your app in sync with Cloud, so **our availability won't affect yours**. All your feature flag reads are local to your app.
123
+ * **audit history** &mdash; every feature change and who made it.
124
+ * **rollbacks** &mdash; enable or disable a feature accidentally? No problem. You can roll back to any point in the audit history with a single click.
125
+
126
+ [![Flipper Cloud Screenshot](docs/images/flipper_cloud.png)](https://www.flippercloud.io)
127
+
128
+ Cloud is super simple to integrate with Rails ([demo app](https://github.com/fewerandfaster/flipper-rails-demo)), Sinatra or any other framework.
129
+
130
+ ## Advanced
72
131
 
73
- ## Docs
132
+ A few miscellaneous docs with more info for the hungry.
74
133
 
75
- * [Gates](docs/Gates.md) - Boolean, Groups, Actors, % of Actors, and % of Time
76
- * [Adapters](docs/Adapters.md) - Mongo, Redis, Cassandra, Active Record...
77
134
  * [Instrumentation](docs/Instrumentation.md) - ActiveSupport::Notifications and Statsd
78
135
  * [Optimization](docs/Optimization.md) - Memoization middleware and Cache adapters
79
- * [Web Interface](docs/ui/README.md) - Point and click...
80
136
  * [API](docs/api/README.md) - HTTP API interface
81
137
  * [Caveats](docs/Caveats.md) - Flipper beware! (see what I did there)
82
138
  * [Docker-Compose](docs/DockerCompose.md) - Using docker-compose in contributing
data/docs/Adapters.md CHANGED
@@ -4,17 +4,17 @@ I plan on supporting the adapters in the flipper repo. Other adapters are welcom
4
4
 
5
5
  ## Officially Supported
6
6
 
7
- * [ActiveRecord adapter](https://github.com/jnunemaker/flipper/blob/master/docs/active_record) - Rails 3, 4, 5, and 6.
8
- * [ActiveSupportCacheStore adapter](https://github.com/jnunemaker/flipper/blob/master/docs/active_support_cache_store) - ActiveSupport::Cache::Store
9
- * [Cassanity adapter](https://github.com/jnunemaker/flipper-cassanity)
10
- * [Http adapter](https://github.com/jnunemaker/flipper/blob/master/docs/http)
11
- * [memory adapter](https://github.com/jnunemaker/flipper/blob/master/lib/flipper/adapters/memory.rb) – great for tests
12
- * [Moneta adapter](https://github.com/jnunemaker/flipper/blob/master/docs/moneta)
7
+ * [ActiveRecord adapter](https://github.com/jnunemaker/flipper/blob/master/docs/active_record) - Rails 5 and 6.
8
+ * [Sequel adapter](https://github.com/jnunemaker/flipper/blob/master/docs/sequel)
9
+ * [Redis adapter](https://github.com/jnunemaker/flipper/blob/master/docs/redis)
13
10
  * [Mongo adapter](https://github.com/jnunemaker/flipper/blob/master/docs/mongo)
14
11
  * [PStore adapter](https://github.com/jnunemaker/flipper/blob/master/lib/flipper/adapters/pstore.rb) – great for when a local file is enough
15
- * [read-only adapter](https://github.com/jnunemaker/flipper/blob/master/docs/read-only)
16
- * [Redis adapter](https://github.com/jnunemaker/flipper/blob/master/docs/redis)
17
- * [Sequel adapter](https://github.com/jnunemaker/flipper/blob/master/docs/sequel)
12
+ * [Http adapter](https://github.com/jnunemaker/flipper/blob/master/docs/http) - great for using with `Flipper::Api`
13
+ * [Moneta adapter](https://github.com/jnunemaker/flipper/blob/master/docs/moneta) - great for a variety of data stores
14
+ * [ActiveSupportCacheStore adapter](https://github.com/jnunemaker/flipper/blob/master/docs/active_support_cache_store) - great for Rails caching
15
+ * [Memory adapter](https://github.com/jnunemaker/flipper/blob/master/lib/flipper/adapters/memory.rb) – great for tests
16
+ * [Read-only adapter](https://github.com/jnunemaker/flipper/blob/master/docs/read-only) - great for preventing writes from production console
17
+ * [Rollout adapter](rollout/README.md) - great for switching from rollout to flipper
18
18
 
19
19
  ## Community Supported
20
20
 
data/docs/Caveats.md CHANGED
@@ -1,4 +1,4 @@
1
1
  # Caveats
2
2
 
3
- 1. The [individual actor gate](https://github.com/jnunemaker/flipper/blob/master/docs/Gates.md#2-individual-actor) is typically not designed for hundreds or thousands of actors to be enabled. This is an explicit choice to make it easier to batch load data from the adapters instead of performing individual checks for actors over and over. If you need to enable something for more than 20 individual people, I would recommend using a [group](https://github.com/jnunemaker/flipper/blob/master/docs/Gates.md#5-group).
4
- 2. The disable method exists only to clear something that is enabled. If the thing you are disabling is not enabled, the disable is pointless. This means that if you enable one group an actor is in and disable another group, the feature will be enabled for the actor. ([related issue](https://github.com/jnunemaker/flipper/issues/71))
3
+ 1. The [individual actor gate](https://github.com/jnunemaker/flipper/blob/master/docs/Gates.md#2-individual-actor) is typically not designed for hundreds or thousands of actors to be enabled. This is an explicit choice to make it easier to batch load data from the adapters instead of performing individual checks for actors over and over. If you need to enable something for more than 100 individual actors, I would recommend using a [group](https://github.com/jnunemaker/flipper/blob/master/docs/Gates.md#5-group).
4
+ 2. The `disable` method exists only to clear something that is enabled. If the thing you are disabling is not enabled, the disable is pointless. This means that if you enable one group an actor is in and disable another group, the feature will be enabled for the actor. ([related issue](https://github.com/jnunemaker/flipper/issues/71))
data/docs/Gates.md CHANGED
@@ -7,76 +7,82 @@ Out of the box several types of enabling are supported. They are checked in this
7
7
  All on or all off. Think top level things like `:stats`, `:search`, `:logging`, etc. Also, an easy way to release a new feature as once a feature is boolean enabled it is on for every situation.
8
8
 
9
9
  ```ruby
10
- flipper = Flipper.new(adapter)
11
- flipper[:stats].enable # turn on
12
- flipper[:stats].disable # turn off
13
- flipper[:stats].enabled? # check
10
+ Flipper.enable :stats # turn on
11
+ Flipper.disable :stats # turn off
12
+ Flipper.enabled? :stats # check
14
13
  ```
15
14
 
16
15
  ## 2. Individual Actor
17
16
 
18
- 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`.
17
+ Turn feature on for individual thing. Think enable feature for someone to test or for a buddy.
19
18
 
20
19
  ```ruby
21
- flipper = Flipper.new(adapter)
20
+ Flipper.enable_actor :stats, user
21
+ Flipper.enabled? :stats, user # true
22
22
 
23
- flipper[:stats].enable user
24
- flipper[:stats].enabled? user # true
25
-
26
- flipper[:stats].disable user
27
- flipper[:stats].enabled? user # false
23
+ Flipper.disable_actor :stats, user
24
+ Flipper.enabled? :stats, user # false
28
25
 
29
26
  # you can enable anything, does not need to be user or person
30
- flipper[:search].enable group
31
- flipper[:search].enabled? group
32
-
33
- # you can also use shortcut methods
34
- flipper.enable_actor :search, user
35
- flipper.disable_actor :search, user
36
- flipper[:search].enable_actor user
37
- flipper[:search].disable_actor user
38
- ```
27
+ Flipper.enable_actor :search, organization
28
+ Flipper.enabled? :search, organization
39
29
 
40
- 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.
30
+ # you can also save a reference to a specific feature
31
+ feature = Flipper[:search]
32
+
33
+ feature.enable_actor user
34
+ feature.enabled? user # true
35
+ feature.disable_actor user
36
+ ```
41
37
 
42
- The one exception to this rule is if you have globally unique `flipper_ids`, such as UUIDs. 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:
38
+ The only requirement for an individual actor is that it must have a unique `flipper_id`. Include the `Flipper::Identifier` module for a default implementation which combines the class name and `id` (e.g. `User;6`).
43
39
 
44
40
  ```ruby
45
- class User
46
- def flipper_id
47
- "User;#{id}"
48
- end
41
+ class User < Struct.new(:id)
42
+ include Flipper::Identifier
49
43
  end
50
44
 
51
- class Group
45
+ User.new(5).flipper_id # => "User;5"
46
+ ```
47
+
48
+ You can also define your own implementation:
49
+
50
+ ```
51
+ class Organization < Struct.new(:uuid)
52
52
  def flipper_id
53
- "Group;#{id}"
53
+ uuid
54
54
  end
55
55
  end
56
+
57
+ Organization.new("DEB3D850-39FB-444B-A1E9-404A990FDBE0").flipper_id
58
+ # => "DEB3D850-39FB-444B-A1E9-404A990FDBE0"
56
59
  ```
57
60
 
61
+ Just make sure each type of object has a unique `flipper_id`.
62
+
58
63
  ## 3. Percentage of Actors
59
64
 
60
65
  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.
61
66
 
62
67
  ```ruby
63
- flipper = Flipper.new(adapter)
64
-
65
- # returns a percentage of actors instance set to 10
66
- percentage = flipper.actors(10)
67
-
68
68
  # turn stats on for 10 percent of users in the system
69
- flipper[:stats].enable percentage
69
+ Flipper.enable :stats, Flipper.actors(10)
70
+ # or
71
+ Flipper.enable_percentage_of_actors :stats, 10
70
72
 
71
73
  # checks if actor's flipper_id is in the enabled percentage by hashing
72
74
  # user.flipper_id.to_s to ensure enabled distribution is smooth
73
- flipper[:stats].enabled? user
75
+ Flipper.enabled? :stats, user
74
76
 
75
- # you can also use shortcut methods
76
- flipper.enable_percentage_of_actors :search, 10
77
- flipper.disable_percentage_of_actors :search # sets to 0
78
- flipper[:search].enable_percentage_of_actors 10
79
- flipper[:search].disable_percentage_of_actors # sets to 0
77
+ Flipper.disable_percentage_of_actors :search # sets to 0
78
+ # or
79
+ Flipper.disable :stats, Flipper.actors(0)
80
+
81
+ # you can also save a reference to a specific feature
82
+ feature = Flipper[:search]
83
+ feature.enable_percentage_of_actors 10
84
+ feature.enabled? user
85
+ feature.disable_percentage_of_actors # sets to 0
80
86
  ```
81
87
 
82
88
  ## 4. Percentage of Time
@@ -84,22 +90,20 @@ flipper[:search].disable_percentage_of_actors # sets to 0
84
90
  Turn this on for a percentage of time. Think load testing new features behind the scenes and such.
85
91
 
86
92
  ```ruby
87
- flipper = Flipper.new(adapter)
88
-
89
- # get percentage of time instance set to 5
90
- percentage = flipper.time(5)
91
-
92
93
  # Register a feature called logging and turn it on for 5 percent of the time.
93
94
  # This could be on during one request and off the next
94
95
  # could even be on first time in request and off second time
95
- flipper[:logging].enable percentage
96
- flipper[:logging].enabled? # this will return true 5% of the time.
97
-
98
- # you can also use shortcut methods
99
- flipper.enable_percentage_of_time :search, 5 # registers a feature called "search" and enables it 5% of the time
100
- flipper.disable_percentage_of_time :search # sets to 0
101
- flipper[:search].enable_percentage_of_time 5
102
- flipper[:search].disable_percentage_of_time # sets to 0
96
+ Flipper.enable_percentage_of_time :logging, 5
97
+
98
+ Flipper.enabled? :logging # this will return true 5% of the time.
99
+
100
+ Flipper.disable_percentage_of_time :logging # sets to 0
101
+
102
+ # you can also save a reference to a specific feature
103
+ feature = Flipper[:search]
104
+ feature.enable_percentage_of_time, 5
105
+ feature.enabled?
106
+ feature.disable_percentage_of_time
103
107
  ```
104
108
 
105
109
  Timeness 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 time to be very useful.
@@ -114,52 +118,48 @@ Flipper.register(:admins) do |actor|
114
118
  actor.respond_to?(:admin?) && actor.admin?
115
119
  end
116
120
 
117
- flipper = Flipper.new(adapter)
118
-
119
- flipper[:stats].enable flipper.group(:admins) # This registers a stats feature and turns it on for admins (which is anything that returns true from the registered block).
120
- flipper[:stats].disable flipper.group(:admins) # turn off the stats feature for admins
121
+ Flipper.enable_group :stats, :admins # This registers a stats feature and turns it on for admins (which is anything that returns true from the registered block).
122
+ Flipper.disable_group :stats, :admins # turn off the stats feature for admins
121
123
 
122
124
  person = Person.find(params[:id])
123
- flipper[:stats].enabled? person # check if enabled, returns true if person.admin? is true
125
+ Flipper.enabled? :stats, person # check if enabled, returns true if person.admin? is true
124
126
 
125
- # you can also use shortcut methods. This also registers a stats feature and turns it on for admins.
126
- flipper.enable_group :stats, :admins
127
- person = Person.find(params[:id])
128
- flipper[:stats].enabled? person # same as above. check if enabled, returns true if person.admin? is true
129
127
 
130
- flipper.disable_group :stats, :admins
131
- flipper[:stats].enable_group :admins
132
- flipper[:stats].disable_group :admins
128
+ # you can also use shortcut methods. This also registers a stats feature and turns it on for admins.
129
+ feature = Flipper[:search]
130
+ feature.enable_group :admins
131
+ feature.enabled? person
132
+ feature.disable_group :admins
133
133
  ```
134
134
 
135
135
  Here's a quick explanation of the above code block:
136
136
 
137
- ```
137
+ ```ruby
138
138
  Flipper.register(:admins) do |actor|
139
139
  actor.respond_to?(:admin?) && actor.admin?
140
140
  end
141
141
  ```
142
- - The above first registers a group called `admins` which essentially saves a [Proc](http://www.eriktrautman.com/posts/ruby-explained-blocks-procs-and-lambdas-aka-closures) to be called later. The `actor` is an instance of the `Flipper::Types::Actor` that wraps the thing being checked against and `actor.thing` is the original object being checked.
142
+ - The above first registers a group called `admins` which essentially saves a [Proc](http://www.eriktrautman.com/posts/ruby-explained-blocks-procs-and-lambdas-aka-closures) to be called later. The `actor` is an instance of the `Flipper::Types::Actor` that wraps the thing being checked against and `actor.thing` is the original object being checked.
143
143
 
144
- ```
145
- flipper[:stats].enable flipper.group(:admins)
144
+ ```ruby
145
+ Flipper.enable_group :stats, :admins
146
146
  ```
147
147
 
148
148
  - The above enables the stats feature to any object that returns true from the `:admins` proc.
149
149
 
150
- ```
150
+ ```ruby
151
151
  person = Person.find(params[:id])
152
- flipper[:stats].enabled? person # check if person is enabled, returns true if person.admin? is true
152
+ Flipper.enabled? :stats, person # check if person is enabled, returns true if person.admin? is true
153
153
  ```
154
154
 
155
- When the `person` object is passed to the `enabled?` method, it is then passed into the proc. If the proc returns true, the entire statement returns true and so `flipper[:stats].enabled? person` returns true. Whatever logic follows this conditional check is then executed.
155
+ When the `person` object is passed to the `enabled?` method, it is then passed into the proc. If the proc returns true, the entire statement returns true and so `Flipper[:stats].enabled? person` returns true. Whatever logic follows this conditional check is then executed.
156
156
 
157
157
  There is no requirement that the thing yielded to the block be a user model or whatever. It can be anything you want, therefore it is a good idea to check that the thing passed into the group block actually responds to what you are trying to do in the `register` proc.
158
158
 
159
159
  In your application code, you can do something like this now:
160
160
 
161
- ```
162
- if flipper[:stats].enabled?(some_admin)
161
+ ```ruby
162
+ if Flipper.enabled? :stats, some_admin
163
163
  # do thing...
164
164
  else
165
165
  # do not do thing