Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stable #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/toggleable/feature_toggler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def register(key)
features << key
end

def available_features
keys.slice(*features)
def available_features(memoize: Toggleable.configuration.use_memoization)
available_features = memoize ? memoized_keys : keys
available_features.slice(*features)
end

def mass_toggle!(mapping, actor: nil)
Expand All @@ -31,6 +32,16 @@ def keys
Toggleable.configuration.storage.hgetall(NAMESPACE)
end

def memoized_keys
return @_memoized_keys if defined?(@_memoized_keys) && !read_expired?
@_last_read_at = Time.now.localtime
@_memoized_keys = Toggleable.configuration.storage.hgetall(NAMESPACE)
end

def read_expired?
@_last_read_at < Time.now.localtime - Toggleable.configuration.expiration_time
end

def log_changes(mapping, actor)
previous_values = available_features
mapping.each do |key, val|
Expand Down
50 changes: 40 additions & 10 deletions spec/toggleable/feature_toggler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,50 @@
end

describe '#available_features' do
let(:keys) {
{
'active_key' => 'true',
'inactive_key' => 'false',
'unavailable_key' => 'true'
context 'without memoize' do
let(:keys) {
{
'active_key' => 'true',
'inactive_key' => 'false',
'unavailable_key' => 'true'
}
}
}

before do
allow(subject).to receive(:keys).and_return(keys)
allow(subject).to receive(:features).and_return(['active_key', 'inactive_key'])
before do
allow(subject).to receive(:keys).and_return(keys)
allow(subject).to receive(:features).and_return(['active_key', 'inactive_key'])
end

it { expect(subject.available_features).to eq({ 'active_key' => 'true', 'inactive_key' => 'false' }) }
end

it { expect(subject.available_features).to eq({ 'active_key' => 'true', 'inactive_key' => 'false' }) }
context 'with memoize' do
let(:keys) {
{
'active_key' => 'true',
'inactive_key' => 'false',
'unavailable_key' => 'true'
}
}

let(:updated_keys) {
{
'active_key' => 'false',
'inactive_key' => 'true'
}
}

before do
described_class.instance.mass_toggle!(keys)
allow(subject).to receive(:features).and_return(['active_key', 'inactive_key'])
end

it do
expect(subject.available_features(memoize: true)).to eq({ 'active_key' => 'true', 'inactive_key' => 'false' })
described_class.instance.mass_toggle!(updated_keys)
expect(subject.available_features(memoize: true)).to eq({ 'active_key' => 'true', 'inactive_key' => 'false' })
end
end
end

describe '#mass_toggle!' do
Expand Down