-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring metadata params and adding config reader
- Loading branch information
1 parent
5141f38
commit 1e5394c
Showing
8 changed files
with
238 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'json' | ||
require 'deep_merge' | ||
|
||
class Formatron | ||
class Configuration | ||
module Config | ||
# reads config directories into config hash | ||
module Reader | ||
def self.read(dir, default_file) | ||
default = File.join(dir, default_file) | ||
config = File.file?(default) ? JSON.parse(File.read(default)) : {} | ||
entries = Dir.glob(File.join(dir, '*'), File::FNM_DOTMATCH) | ||
do_entries entries, default_file, config | ||
config | ||
end | ||
|
||
def self.do_entries(entries, default_file, config) | ||
entries.each do |entry| | ||
basename = File.basename(entry) | ||
next if ['.', '..', default_file].include?(basename) | ||
config[basename] = {} unless config[basename].is_a? Hash | ||
config[basename].deep_merge!( | ||
read(entry, default_file) | ||
) if File.directory?(entry) | ||
config[basename] = File.read(entry) if File.file?(entry) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
require 'spec_helper' | ||
require 'formatron/configuration/config/reader' | ||
|
||
describe Formatron::Configuration::Config::Reader do | ||
include FakeFS::SpecHelpers | ||
|
||
before(:each) do | ||
Dir.mkdir('test') | ||
end | ||
|
||
context 'with just a default json file' do | ||
before(:each) do | ||
File.write( | ||
'test/default.json', <<-EOH.gsub(/\s{10}/, '') | ||
{ | ||
"test": "value" | ||
} | ||
EOH | ||
) | ||
@config = Formatron::Configuration::Config::Reader.read( | ||
'test', | ||
'default.json' | ||
) | ||
end | ||
|
||
it 'should read the json into a hash' do | ||
expect(@config).to eql( | ||
'test' => 'value' | ||
) | ||
end | ||
end | ||
|
||
context 'with files but no default json file' do | ||
before(:each) do | ||
File.write( | ||
'test/test-key', <<-EOH.gsub(/\s{10}/, '') | ||
another value | ||
EOH | ||
) | ||
@config = Formatron::Configuration::Config::Reader.read( | ||
'test', | ||
'default.json' | ||
) | ||
end | ||
|
||
it 'should add the file content as a key to the hash' do | ||
expect(@config).to eql( | ||
'test-key' => "another value\n" | ||
) | ||
end | ||
end | ||
|
||
context 'with files and a default json file' do | ||
before(:each) do | ||
File.write( | ||
'test/test-key', <<-EOH.gsub(/\s{10}/, '') | ||
another value | ||
EOH | ||
) | ||
File.write( | ||
'test/default.json', <<-EOH.gsub(/\s{10}/, '') | ||
{ | ||
"test": "value", | ||
"test-key": "overriden value" | ||
} | ||
EOH | ||
) | ||
@config = Formatron::Configuration::Config::Reader.read( | ||
'test', | ||
'default.json' | ||
) | ||
end | ||
|
||
it 'should merge the keys using the files to override the json' do | ||
expect(@config).to eql( | ||
'test' => 'value', | ||
'test-key' => "another value\n" | ||
) | ||
end | ||
end | ||
|
||
context 'with sub directories' do | ||
before(:each) do | ||
File.write( | ||
'test/test-key', <<-EOH.gsub(/\s{10}/, '') | ||
another value | ||
EOH | ||
) | ||
File.write( | ||
'test/default.json', <<-EOH.gsub(/\s{10}/, '') | ||
{ | ||
"test": "value", | ||
"test-key": "overriden value", | ||
"hello": "something", | ||
"banana": { | ||
"fruit": true | ||
} | ||
} | ||
EOH | ||
) | ||
Dir.mkdir('test/hello') | ||
File.write( | ||
'test/hello/default.json', <<-EOH.gsub(/\s{10}/, '') | ||
{ | ||
"more-test": "hello", | ||
"and-again": "override me" | ||
} | ||
EOH | ||
) | ||
File.write( | ||
'test/hello/and-again', <<-EOH.gsub(/\s{10}/, '') | ||
new value | ||
EOH | ||
) | ||
Dir.mkdir('test/banana') | ||
File.write( | ||
'test/banana/default.json', <<-EOH.gsub(/\s{10}/, '') | ||
{ | ||
"color": "yellow" | ||
} | ||
EOH | ||
) | ||
@config = Formatron::Configuration::Config::Reader.read( | ||
'test', | ||
'default.json' | ||
) | ||
end | ||
|
||
it 'should recurse and merge everything' do | ||
expect(@config).to eql( | ||
'test' => 'value', | ||
'test-key' => "another value\n", | ||
'hello' => { | ||
'more-test' => 'hello', | ||
'and-again' => "new value\n" | ||
}, | ||
'banana' => { | ||
'fruit' => true, | ||
'color' => 'yellow' | ||
} | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters