Skip to content

Commit

Permalink
chore: update docs, add/improve tests, fix console
Browse files Browse the repository at this point in the history
  • Loading branch information
rarruda committed Jan 9, 2023
1 parent 470a14f commit 67b1c06
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
44 changes: 30 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,6 @@ 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 @@ -321,18 +318,33 @@ 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"
```

If you don't want to check a feature is disabled with `unless`, you can also use `is_disabled?`:

```ruby
# so instead of:
unless UNLEASH.is_enabled? "AwesomeFeature", @unleash_context
puts "AwesomeFeature is disabled"
end

# it might be more intelligible:
if UNLEASH.is_disabled? "AwesomeFeature", @unleash_context
puts "AwesomeFeature is disabled"
end
```

If the feature is not found in the server, it will by default return false.
However you can override that by setting the default return value to `true`:
However, you can override that by setting the default return value to `true`:

```ruby
if UNLEASH.is_enabled? "AwesomeFeature", @unleash_context, true
puts "AwesomeFeature is enabled by default"
end
# or
if UNLEASH.is_disabled? "AwesomeFeature", @unleash_context, true
puts "AwesomeFeature is disabled by default"
end
```

Another possibility is to send a block, [Lambda](https://ruby-doc.org/core-3.0.1/Kernel.html#method-i-lambda) or [Proc](https://ruby-doc.org/core-3.0.1/Proc.html#method-i-yield)
Expand All @@ -355,7 +367,7 @@ awesomeness = 10
@unleash_context.properties[:coolness] = 10

if UNLEASH.is_enabled?("AwesomeFeature", @unleash_context) { |feat, ctx| awesomeness >= 6 && ctx.properties[:coolness] >= 8 }
puts "AwesomeFeature is enabled by default if both the user has a high enought coolness and the application has a high enough awesomeness"
puts "AwesomeFeature is enabled by default if both the user has a high enough coolness and the application has a high enough awesomeness"
end
```

Expand All @@ -364,20 +376,20 @@ Note:
- The client will evaluate the fallback function once per call of `is_enabled()`.
Please keep this in mind when creating your fallback function!
- The returned value of the block should be a boolean.
However the client will coerce the result to boolean via `!!`.
However, the client will coerce the result to boolean via `!!`.
- If both a `default_value` and `fallback_function` are supplied,
the client will define the default value by `OR`ing the default value and the output of the fallback function.


Alternatively by using `if_enabled` you can send a code block to be executed as a parameter:
Alternatively by using `if_enabled` (or `if_disabled`) you can send a code block to be executed as a parameter:

```ruby
UNLEASH.if_enabled "AwesomeFeature", @unleash_context, true do
puts "AwesomeFeature is enabled by default"
end
```

Note: `if_enabled` only supports `default_value`, but not `fallback_function`.
Note: `if_enabled` (and `if_disabled`) only support `default_value`, but not `fallback_function`.

##### Variations

Expand Down Expand Up @@ -498,7 +510,7 @@ In order for strategy to work correctly it should support two methods `name` and
```ruby
class MyCustomStrategy
def name
'muCustomStrategy'
'myCustomStrategy'
end

def is_enabled?(params = {}, context = nil)
Expand All @@ -513,15 +525,19 @@ end

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
After checking out the repo, run `bin/setup` to install dependencies.
Then, run `rake spec` to run the tests.
You can also run `bin/console` for an interactive prompt that will allow you to experiment.

This SDK is also built against the Unleash Client Specification tests. To run the Ruby SDK against this test suite, you'll need to have a copy on your machine, you can clone the repository directly using:
This SDK is also built against the Unleash Client Specification tests.
To run the Ruby SDK against this test suite, you'll need to have a copy on your machine, you can clone the repository directly using:

`git clone --depth 5 --branch v4.2.2 https://github.com/Unleash/client-specification.git client-specification`

After doing this, `rake spec` will also run the client specification tests.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
To install this gem onto your local machine, run `bundle exec rake install`.
To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).


## Contributing
Expand Down
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "unleash"
require "unleash/client"

# You can add fixtures and/or initialization code here to make experimenting
Expand Down
33 changes: 30 additions & 3 deletions spec/unleash/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@
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 Expand Up @@ -412,6 +409,23 @@
expect(WebMock).not_to have_requested(:post, 'http://test-url/client/metrics')
end

it "should return correct default values" do
unleash_client = Unleash::Client.new
expect(unleash_client.is_enabled?('any_feature')).to eq(false)
expect(unleash_client.is_enabled?('any_feature', {}, false)).to eq(false)
expect(unleash_client.is_enabled?('any_feature', {}, true)).to eq(true)

expect(unleash_client.enabled?('any_feature', {}, true)).to eq(true)
expect(unleash_client.enabled?('any_feature', {}, false)).to eq(false)

expect(unleash_client.is_disabled?('any_feature')).to eq(true)
expect(unleash_client.is_disabled?('any_feature', {}, true)).to eq(true)
expect(unleash_client.is_disabled?('any_feature', {}, false)).to eq(false)

expect(unleash_client.disabled?('any_feature', {}, true)).to eq(true)
expect(unleash_client.disabled?('any_feature', {}, false)).to eq(false)
end

it "should yield correctly to block when using if_enabled" do
unleash_client = Unleash::Client.new
cont = Unleash::Context.new(user_id: 1)
Expand All @@ -421,6 +435,19 @@
expect{ |b| unleash_client.if_enabled('any_feature', {}, false, &b).not_to yield_with_no_args }
end

it "should yield correctly to block when using if_disabled" do
unleash_client = Unleash::Client.new
cont = Unleash::Context.new(user_id: 1)

expect{ |b| unleash_client.if_disabled('any_feature', {}, true, &b).not_to yield_with_no_args }
expect{ |b| unleash_client.if_disabled('any_feature', cont, true, &b).not_to yield_with_no_args }

expect{ |b| unleash_client.if_disabled('any_feature', {}, false, &b).to yield_with_no_args }
expect{ |b| unleash_client.if_disabled('any_feature', cont, false, &b).to yield_with_no_args }
expect{ |b| unleash_client.if_disabled('any_feature', {}, &b).to yield_with_no_args }
expect{ |b| unleash_client.if_disabled('any_feature', &b).to yield_with_no_args }
end

describe 'get_variant' do
let(:disable_client) { false }
let(:client) { Unleash::Client.new }
Expand Down

0 comments on commit 67b1c06

Please sign in to comment.