diff --git a/.rubocop.yml b/.rubocop.yml index 91428927..7469dd65 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -6,6 +6,7 @@ AllCops: Naming/PredicateName: AllowedMethods: - is_enabled? + - is_disabled? Metrics/ClassLength: diff --git a/README.md b/README.md index 7c5d29d0..2add3ac2 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,12 @@ if @unleash.is_enabled?(feature_name, unleash_context) else puts " #{feature_name} is disabled according to unleash" end + +if @unleash.is_disabled?(feature_name, unleash_context) + puts " #{feature_name} is disabled according to unleash" +else + puts " #{feature_name} is enabled according to unleash" +end ``` ## Usage in a Rails Application @@ -304,6 +310,9 @@ Then wherever in your application that you need a feature toggle, you can use: if UNLEASH.is_enabled? "AwesomeFeature", @unleash_context puts "AwesomeFeature is enabled" end +if UNLEASH.is_disabled? "AwesomeFeature", @unleash_context + puts "AwesomeFeature is disabled" +end ``` or if client is set in `Rails.configuration.unleash`: @@ -312,6 +321,9 @@ or if client is set in `Rails.configuration.unleash`: if Rails.configuration.unleash.is_enabled? "AwesomeFeature", @unleash_context puts "AwesomeFeature is enabled" end +if Rails.configuration.unleash.is_disabled? "AwesomeFeature", @unleash_context + puts "AwesomeFeature is enabled" +end ``` If the feature is not found in the server, it will by default return false. @@ -445,6 +457,9 @@ Method Name | Description | Return Type | `is_enabled?` | Check if feature toggle is to be enabled or not. | Boolean | `enabled?` | Alias to the `is_enabled?` method. But more ruby idiomatic. | Boolean | `if_enabled` | Run a code block, if a feature is enabled. | `yield` | +`is_disabled?` | Check if feature toggle is to be enabled or not. | Boolean | +`disabled?` | Alias to the `is_disabled?` method. But more ruby idiomatic. | Boolean | +`if_disabled` | Run a code block, if a feature is disabled. | `yield` | `get_variant` | Get variant for a given feature | `Unleash::Variant` | `shutdown` | Save metrics to disk, flush metrics to server, and then kill ToggleFetcher and MetricsReporter threads. A safe shutdown. Not really useful in long running applications, like web applications. | nil | `shutdown!` | Kill ToggleFetcher and MetricsReporter threads immediately. | nil | diff --git a/lib/unleash/client.rb b/lib/unleash/client.rb index 7e4496b1..62b75f35 100644 --- a/lib/unleash/client.rb +++ b/lib/unleash/client.rb @@ -50,14 +50,25 @@ def is_enabled?(feature, context = nil, default_value_param = false, &fallback_b toggle.is_enabled?(context) end + def is_disabled?(feature, context = nil, default_value_param = true, &fallback_blk) + !is_enabled?(feature, context, !default_value_param, &fallback_blk) + end + # enabled? is a more ruby idiomatic method name than is_enabled? alias enabled? is_enabled? + # disabled? is a more ruby idiomatic method name than is_disabled? + alias disabled? is_disabled? # execute a code block (passed as a parameter), if is_enabled? is true. def if_enabled(feature, context = nil, default_value = false, &blk) yield(blk) if is_enabled?(feature, context, default_value) end + # execute a code block (passed as a parameter), if is_disabled? is true. + def if_disabled(feature, context = nil, default_value = true, &blk) + yield(blk) if is_disabled?(feature, context, default_value) + end + def get_variant(feature, context = Unleash::Context.new, fallback_variant = disabled_variant) Unleash.logger.debug "Unleash::Client.get_variant for feature: #{feature} with context #{context}" diff --git a/spec/unleash/client_spec.rb b/spec/unleash/client_spec.rb index ecac0bd4..4233130a 100644 --- a/spec/unleash/client_spec.rb +++ b/spec/unleash/client_spec.rb @@ -173,6 +173,9 @@ expect( unleash_client.is_enabled?('toggleName', {}, true) ).to eq(true) + expect( + unleash_client.disabled?('toggleName', {}, false) + ).to eq(false) expect(WebMock).not_to have_requested(:get, 'http://test-url/') expect(WebMock).to have_requested(:post, 'http://test-url/client/register')