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

Store in hash #2

Merged
merged 4 commits into from
Jan 29, 2015
Merged
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
2 changes: 1 addition & 1 deletion lib/feature_guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def redis
@redis ||= Redis.new
end

[:allow?, :bump_ramp, :disable, :enable, :toggle, :enabled?, :ramp_val, :set_ramp].each do |mname|
[:allow?, :bump_ramp, :disable, :enable, :flags, :toggle, :enabled?, :ramps, :ramp_val, :set_ramp].each do |mname|
define_method(mname) do |key, *args|
Guard.new(key).send(mname, *args)
end
Expand Down
34 changes: 19 additions & 15 deletions lib/feature_guard/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ def initialize(_feature_name)

# binary flag methods (enabled/disabled)
def disable
redis.set(flag_key, 0)
redis.hset(flags_hkey, feature_name, 0)
end

def enable
redis.set(flag_key, 1)
redis.hset(flags_hkey, feature_name, 1)
end

def enabled?
redis.get(flag_key).tap { |v| return (!v.nil? && v.to_i > 0) }
redis.hget(flags_hkey, feature_name).tap { |v| return (!v.nil? && v.to_i > 0) }
rescue
false
end
Expand All @@ -35,34 +35,30 @@ def bump_ramp(amount = 10.0)
end

def ramp_val
redis.get(ramp_key).to_f
redis.hget(ramps_hkey, feature_name).to_f
end

def set_ramp(new_val)
new_val = new_val.to_f
new_val = 100.0 if new_val > 100.0
new_val = 0.0 if new_val < 0.0

redis.set(ramp_key, new_val)
redis.hset(ramps_hkey, feature_name, new_val)
new_val
end

private

def feature_key
@feature_key ||= feature_name.to_s.split.join('_')
def flags
redis.hgetall(flags_hkey)
end

def flag_key
@flag_key ||= "fgf_#{feature_key}"
def ramps
redis.hgetall(ramps_hkey)
end

def ramp_key
@ramp_key ||= "fgr_#{feature_key}"
end
private

def hashed_val(s)
(Digest::MD5.hexdigest("#{ramp_key}_#{s}").to_i(16) % 10000).to_f / 100.0
(Digest::MD5.hexdigest("#{feature_name}_#{s}").to_i(16) % 10000).to_f / 100.0
end

def random_val
Expand All @@ -72,4 +68,12 @@ def random_val
def redis
FeatureGuard.redis
end

def flags_hkey
"featureguard_flags"
end

def ramps_hkey
"featureguard_ramps"
end
end; end
34 changes: 30 additions & 4 deletions spec/feature_guard/guard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

it 'uses a random value' do
expect(guard).to receive(:random_val).and_return(29.9)
expect(subject).to be_true
expect(subject).to eq(true)
end
end

Expand All @@ -21,7 +21,7 @@

it 'hashes the value together with the feature name' do
expect(guard).to receive(:hashed_val).and_return(30.1)
expect(subject).to be_false
expect(subject).to eq(false)
end
end
end
Expand All @@ -42,13 +42,13 @@
subject { guard.enabled? }

context 'for a non-existent flag' do
it { should be_false }
it { is_expected.to eq(false) }
end

context 'for an enabled flag' do
before { guard.enable }

it { should be_true }
it { is_expected.to eq(true) }
end
end

Expand Down Expand Up @@ -80,4 +80,30 @@
expect { guard.toggle }.to change { guard.enabled? }.from(true).to(false)
end
end

describe '#flags' do
before { guard.enable }

subject { guard.flags }

it 'returns all the flags' do
expect(subject.keys).to include(feature.to_s)
end
end

describe '#ramps' do
let(:val) { 10.0 }

before { guard.set_ramp val }

subject { guard.ramps }

it 'returns all the flags' do
expect(subject.keys).to include(feature.to_s)
end

it 'returns all the ramp values' do
expect(subject.values).to include(val.to_s)
end
end
end
4 changes: 2 additions & 2 deletions spec/feature_guard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
subject { FeatureGuard.enabled? feature }

context 'for a non-existent flag' do
it { should be_false }
it { is_expected.to eq(false) }
end

context 'when the Redis client blows up or is non-existent' do
before { FeatureGuard.stub(redis: nil) }

it { should be_false }
it { is_expected.to eq(false) }
end
end
end