Skip to content

Commit

Permalink
Added templates feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
faizalzakaria committed Jan 8, 2018
1 parent 7a9d30d commit 1ad2cd5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
fflags (0.4.2)
fflags (0.4.3)
redis (>= 3.0.0)

GEM
Expand Down
35 changes: 35 additions & 0 deletions lib/fflags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ def config
api.load_flags
end

# Reset the whole config to the default values
#
# Ex.
# FFlags.reset_config
def reset_config
@configuration = nil
reset
end

# Returns all supported flags.
#
# Ex.
Expand All @@ -28,6 +37,32 @@ def all
api.flags
end

# Returns all templates.
#
# Ex.
# FFlags.templates
def templates
configuration.templates
end

# Sets as given template.
# It will returns false if the template doesn't exists
#
# Ex.
# FFlags.set_as_template(:template_name)
def set_as_template(template)
template = templates[template.to_sym] || templates[template.to_s]

return false unless template

status = true
template.each_pair do |key, value|
status &&= set(key, value)
end

status
end

# Check if the flag is enabled,
# it returns true | false.
#
Expand Down
3 changes: 2 additions & 1 deletion lib/fflags/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module FFlags
# Configuration Class
class Configuration
attr_accessor :key, :redis_url, :debug, :flags
attr_accessor :key, :redis_url, :debug, :flags, :templates

def initialize
set_default_values
Expand All @@ -14,6 +14,7 @@ def set_default_values
@redis_url = 'redis://127.0.0.1:6379'
@debug = false
@flags = {}
@templates = {}
end
end
end
2 changes: 1 addition & 1 deletion lib/fflags/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module FFlags
VERSION = '0.4.2'.freeze
VERSION = '0.4.3'.freeze
end
54 changes: 52 additions & 2 deletions spec/fflags_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
RSpec.describe FFlags do
before do
FFlags.config { |config| config.flags = {} }
FFlags.reset
FFlags.reset_config
end

describe '#version' do
Expand Down Expand Up @@ -33,6 +32,55 @@
end
end

describe '#templates' do
context 'no templates set' do
it 'returns empty templates' do
expect(FFlags.templates).to eq({})
end
end

context '2 templates is set' do
let(:templates) { { production: { test: true } } }
before do
FFlags.config { |config| config.templates = templates }
end

it 'returns all templates' do
expect(FFlags.templates).to eq(templates)
end
end
end

describe '#set_as_template' do
let(:templates) { { production: { read: false } } }
before do
FFlags.config do |config|
config.flags = { read: true }
config.templates = templates
end
end

context 'template exists' do
it 'sets template correctly' do
expect(FFlags.read?).to be true
expect(FFlags.set_as_template(:production)).to be true
expect(FFlags.read?).to be false
end
end

context 'template does not exists' do
it 'doesn set as template' do
expect(FFlags.set_as_template(:experimental)).to be false
end
end

context 'template has wrong key' do
let(:templates) { { production: { new_feature: true } } }

it { expect(FFlags.set_as_template(:production)).to be false }
end
end

describe '#enabled?' do
before { FFlags.config { |config| config.flags = { test: true } } }
it 'returns correct boolean' do
Expand All @@ -57,6 +105,8 @@
expect(FFlags.test?).to be true
expect(FFlags.set('test', false)).to be true
expect(FFlags.test?).to be false
expect(FFlags.set(:test, true)).to be true
expect(FFlags.test?).to be true
end
end

Expand Down

0 comments on commit 1ad2cd5

Please sign in to comment.