Skip to content

Commit

Permalink
Proxy optional keyword arguments to parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
hasghari committed Jun 25, 2022
1 parent 176af87 commit 93fcd12
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby 3.1.0
ruby 3.1.2
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
conifer (1.3.0)
conifer (2.0.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -56,6 +56,7 @@ GEM
PLATFORMS
x86_64-darwin-19
x86_64-darwin-20
x86_64-darwin-21
x86_64-linux

DEPENDENCIES
Expand Down
4 changes: 2 additions & 2 deletions lib/conifer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ def self.included(base)
# rubocop:disable Metrics/ParameterLists
module ClassMethods
def conifer(name, prefix: nil, dir: nil, format: :yml, method: ::File.basename(name.to_s, ".#{format}"),
singleton: false, allowed_classes: [])
singleton: false, **options)
dir ||= ::File.expand_path(::File.dirname(caller_locations.first.path))

body = proc do
return instance_variable_get("@conifer_#{method}") if instance_variable_defined?("@conifer_#{method}")

instance_variable_set "@conifer_#{method}",
Conifer::File.new(name, prefix: prefix, format: format,
dir: dir, allowed_classes: allowed_classes).tap(&:validate!)
dir: dir, **options).tap(&:validate!)
end

singleton ? define_singleton_method(method, &body) : define_method(method, &body)
Expand Down
10 changes: 5 additions & 5 deletions lib/conifer/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class File
NotFoundError = Class.new(StandardError)
UnsupportedFormatError = Class.new(StandardError)

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

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

def [](key)
Expand All @@ -26,9 +26,9 @@ def [](key)
def parsed
@parsed ||= case format
when :yml, :yaml
YAML.safe_load(ERB.new(::File.read(path)).result, permitted_classes: allowed_classes)
YAML.safe_load(ERB.new(::File.read(path)).result, **options)
when :json
JSON.parse(ERB.new(::File.read(path)).result)
JSON.parse(ERB.new(::File.read(path)).result, **options)
else
raise UnsupportedFormatError, "Format '#{format}' is not supported"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/conifer/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Conifer
VERSION = '1.3.0'
VERSION = '2.0.0'
end
6 changes: 3 additions & 3 deletions spec/conifer/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

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

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

describe '#path' do
context 'when file is in current directory' do
Expand Down Expand Up @@ -120,7 +120,7 @@
end

context 'when type is whitelisted' do
let(:allowed_classes) { [Date] }
let(:permitted_classes) { [Date] }

it 'does not raise error' do
expect { file.parsed }.not_to raise_error
Expand Down

0 comments on commit 93fcd12

Please sign in to comment.