Skip to content

Commit

Permalink
Add support for JSON config files
Browse files Browse the repository at this point in the history
  • Loading branch information
hasghari committed Mar 9, 2020
1 parent 1dfc9eb commit 09fc372
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/conifer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def self.included(base)

# rubocop:disable Metrics/ParameterLists
module ClassMethods
def conifer(name, prefix: nil, dir: nil, method: ::File.basename(name.to_s, '.yml'), singleton: false,
allowed_classes: [])
def conifer(name, prefix: nil, dir: nil, format: :yml, method: ::File.basename(name.to_s, ".#{format}"),
singleton: false, allowed_classes: [])
dir ||= ::File.expand_path(::File.dirname(caller_locations.first.path))

body = proc do
Expand Down
19 changes: 14 additions & 5 deletions lib/conifer/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
module Conifer
class File
NotFoundError = Class.new(StandardError)
UnsupportedFormatError = Class.new(StandardError)

attr_reader :name, :prefix, :dir, :allowed_classes
attr_reader :name, :dir, :prefix, :format, :allowed_classes

def initialize(name, dir:, prefix: nil, allowed_classes: [])
def initialize(name, dir:, prefix: nil, format: :yml, allowed_classes: [])
@name = name
@prefix = prefix
@dir = dir
@prefix = prefix
@format = format
@allowed_classes = allowed_classes
end

Expand All @@ -22,7 +24,14 @@ def [](key)
end

def parsed
@parsed ||= YAML.safe_load(ERB.new(::File.read(path)).result, allowed_classes)
@parsed ||= case format
when :yml, :yaml
YAML.safe_load(ERB.new(::File.read(path)).result, allowed_classes)
when :json
JSON.parse(ERB.new(::File.read(path)).result)
else
raise UnsupportedFormatError, "Format '#{format}' is not supported"
end
end

def path
Expand All @@ -36,7 +45,7 @@ def exists?
end

def filename
"#{::File.basename(name.to_s, '.yml')}.yml"
"#{::File.basename(name.to_s, ".#{format}")}.#{format}"
end

def validate!
Expand Down
25 changes: 24 additions & 1 deletion spec/conifer/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
require 'conifer/file'

RSpec.describe Conifer::File do
subject(:file) { described_class.new(name, dir: dir, prefix: prefix, allowed_classes: allowed_classes) }
subject(:file) do
described_class.new(name, dir: dir, format: format, prefix: prefix, allowed_classes: allowed_classes)
end

let(:name) { :foo }
let(:dir) { File.expand_path(__dir__) }
let(:format) { :yml }
let(:prefix) { nil }
let(:allowed_classes) { [] }

Expand Down Expand Up @@ -87,6 +90,26 @@
end
end

context 'with json format' do
let(:format) { :json }

it 'returns parsed content of file' do
ClimateControl.modify AWS_SECRET_KEY: 'secret' do
expect(file.parsed).to eq('foo' => 'bar',
'development' => { 'aws' => { 'secret_key' => 'secret' }, 'log_level' => 'debug' },
'test' => { 'log_level' => 'fatal' })
end
end
end

context 'with unsupported format' do
let(:format) { :ruby }

it 'raises error' do
expect { file.parsed }.to raise_error Conifer::File::UnsupportedFormatError
end
end

context 'with non-primitive values' do
let(:name) { :dates }

Expand Down
12 changes: 12 additions & 0 deletions spec/conifer/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"foo": "bar",
"development": {
"aws": {
"secret_key": "<%= ENV.fetch('AWS_SECRET_KEY', 12345) %>"
},
"log_level": "debug"
},
"test": {
"log_level": "fatal"
}
}

0 comments on commit 09fc372

Please sign in to comment.