Skip to content

Commit

Permalink
refactoring configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomoura committed Jan 12, 2016
1 parent 82ed5ff commit be46d91
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 37 deletions.
14 changes: 2 additions & 12 deletions lib/cnab150.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'cnab150/version'
require 'cnab150/configuration'
require 'cnab150/registry'
require 'cnab150/parser'
require 'cnab150/layout'
require 'cnab150/errors'
require 'cnab150/configuration'
require 'cnab150/version'

# The public interface of gem
module Cnab150
Expand Down Expand Up @@ -43,15 +43,5 @@ def select(type, raw)
def find(registries, type)
registries.find { |r| r.registry_code.eql?(type) }
end

attr_writer :configuration

def configuration
@configuration ||= Configuration.new
end

def configure
yield(configuration)
end
end
end
25 changes: 22 additions & 3 deletions lib/cnab150/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
module Cnab150
class Configuration
module Cnab150 #:nodoc:
class << self
# Configures global settings
# Cnab150.configure do |config|
# config.default_per_page = 10
# end
def configure
yield(@config)
end

# Global settings
def config
@config ||= Cnab150::Configuration.new
end
end

class Configuration #:nodoc:
attr_accessor :layout_file_path

def initialize
@layout_file_path = "#{File.dirname(__dir__)}/../config/layout.yml"
end

# The layouts method load the layouts from the yml file.
def layouts
@_ ||= YAML.load_file(@layout_file_path)['cnab150']
end
end
end

9 changes: 2 additions & 7 deletions lib/cnab150/layout.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
require 'yaml'

# The layouts method load the layouts from the yml file.
module Cnab150
def self.layouts
@_ ||= YAML.load_file(configuration.layout_file_path)['cnab150']
end

# The Layout module is responsible of build the appropriate layout.
class Layout
def self.build(type)
fail Cnab150::Errors::LayoutNotImplementedError unless
Cnab150.layouts.include?(type.downcase)
Cnab150.config.layouts.include?(type.downcase)

new Cnab150.layouts[type.downcase]
new Cnab150.config.layouts[type.downcase]
end

def initialize(layout)
Expand Down
10 changes: 0 additions & 10 deletions spec/cnab150_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,4 @@
it { expect(subject.registry_code).to be_eql('Z') }
end
end

describe "#configure" do
before do
Cnab150.configure do |config|
config.layout_file_path = 'fake.yml'
end
end

it { expect(Cnab150.configuration.layout_file_path).to eq('fake.yml') }
end
end
22 changes: 17 additions & 5 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@
module Cnab150
describe Configuration do
subject { Configuration.new }
describe '#layout_file' do
describe '#layout_file_path' do
context 'default value' do
let(:default) { 'cnab150/lib/../config/layout.yml' }
it { expect(subject.layout_file).to end_with(default) }
it { expect(subject.layout_file_path).to end_with(default) }
end

context "can set value" do
before { subject.layout_file = 7 }
it { expect(subject.layout_file).to eql(7) }
context 'override the default layout file path' do
let(:file_path) { 'fake.yml' }
before { subject.layout_file_path = file_path }
it { expect(subject.layout_file_path).to eql(file_path) }
end
end

describe '#configure' do
let(:file_path) { 'fake.yml' }
before do
Cnab150.configure do |config|
config.layout_file_path = file_path
end
end

it { expect(Cnab150.config.layout_file_path).to eq file_path }
end
end
end

0 comments on commit be46d91

Please sign in to comment.