Skip to content

Commit

Permalink
Merge pull request #17 from ResultadosDigitais/add-configuration-class
Browse files Browse the repository at this point in the history
Uncople Configuration to own class
  • Loading branch information
geisonbiazus authored Sep 23, 2016
2 parents e0e0d43 + 09fd4ad commit 52228ed
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 85 deletions.
41 changes: 4 additions & 37 deletions lib/feature_flagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,20 @@
require 'feature_flagger/control'
require 'feature_flagger/model'
require 'feature_flagger/feature'
require 'feature_flagger/configuration'

module FeatureFlagger
class << self
def configure(&block)
set_config
yield self if block_given?
yield config if block_given?
end

# TODO: rename to just _config_.
def config
@@config
@@configuration ||= Configuration.new
end

def control
@@control ||= Control.new(storage)
end

def storage=(storage)
set_config
@@config[:storage] = storage
end

def storage
set_config
@@config[:storage]
end

private

def set_config
@@config ||= {}
@@config[:storage] ||= default_client

# TODO: Provide a Rake to generate initial YAML file
# for new projects.

if defined?(Rails)
@@config[:yaml_filepath] ||= "#{Rails.root}/config/rollout.yml"
end

if file_path = @@config[:yaml_filepath]
@@config[:info] ||= YAML.load_file(file_path)
end
end

def default_client
Storage::Redis.default_client
@@control ||= Control.new(config.storage)
end
end
end
20 changes: 20 additions & 0 deletions lib/feature_flagger/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module FeatureFlagger
class Configuration
attr_accessor :storage, :yaml_filepath

def initialize
@storage ||= Storage::Redis.default_client
@yaml_filepath ||= default_yaml_filepath
end

def info
@info ||= YAML.load_file(yaml_filepath) if yaml_filepath
end

private

def default_yaml_filepath
"#{Rails.root}/config/rollout.yml" if defined?(Rails)
end
end
end
2 changes: 1 addition & 1 deletion lib/feature_flagger/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module FeatureFlagger
class Feature
def initialize(feature_key, resource_name = nil)
@feature_key = resolve_key(feature_key, resource_name)
@doc = FeatureFlagger.config[:info]
@doc = FeatureFlagger.config.info
fetch_data
end

Expand Down
25 changes: 25 additions & 0 deletions spec/feature_flagger/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

module FeatureFlagger
RSpec.describe Configuration do
describe '.storage' do
let(:configuration) { described_class.new }

context 'no storage set' do
it 'returns a Redis storage by default' do
expect(configuration.storage).to be_a(FeatureFlagger::Storage::Redis)
end
end

context 'storage set' do
let(:storage) { double('storage') }

before { configuration.storage = storage }

it 'returns storage' do
expect(configuration.storage).to eq storage
end
end
end
end
end
3 changes: 1 addition & 2 deletions spec/feature_flagger/feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ module FeatureFlagger

before do
filepath = File.expand_path('../../fixtures/rollout_example.yml', __FILE__)
info = YAML.load_file(filepath)
allow(FeatureFlagger).to receive(:config).and_return(info: info)
FeatureFlagger.config.yaml_filepath = filepath
end

describe '#initialize' do
Expand Down
3 changes: 1 addition & 2 deletions spec/feature_flagger/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def id; 14 end

before do
filepath = File.expand_path('../../fixtures/rollout_example.yml', __FILE__)
info = YAML.load_file(filepath)
allow(FeatureFlagger).to receive(:config).and_return(info: info)
FeatureFlagger.config.yaml_filepath = filepath
end

describe '#release' do
Expand Down
24 changes: 24 additions & 0 deletions spec/feature_flagger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

RSpec.describe FeatureFlagger do
describe '.configure' do
let(:redis) { double('redis') }
let(:storage) { double('storage') }

before do
FeatureFlagger.configure do |config|
config.storage = storage
end
end

it { expect(FeatureFlagger.config.storage).to eq storage }
end

describe '.control' do
let(:control) { FeatureFlagger.control }
it 'initializes a Control with redis storage' do
expect(control).to be_a(FeatureFlagger::Control)
expect(control.storage).to be_a(FeatureFlagger::Storage::Redis)
end
end
end
43 changes: 0 additions & 43 deletions spec/rollout_spec.rb

This file was deleted.

0 comments on commit 52228ed

Please sign in to comment.