Skip to content

Commit

Permalink
Adding disabled? method (#130)
Browse files Browse the repository at this point in the history
is_disabled?, disabled? if_disabled
  • Loading branch information
serhii-bodnaruk authored Jan 5, 2023
1 parent c4f6b49 commit f28859b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ AllCops:
Naming/PredicateName:
AllowedMethods:
- is_enabled?
- is_disabled?


Metrics/ClassLength:
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`:
Expand All @@ -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.
Expand Down Expand Up @@ -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 |
Expand Down
11 changes: 11 additions & 0 deletions lib/unleash/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
3 changes: 3 additions & 0 deletions spec/unleash/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit f28859b

Please sign in to comment.