From aa76b73776651d7682df8a00975d7416d9e53b25 Mon Sep 17 00:00:00 2001 From: James Armes Date: Thu, 5 Sep 2024 19:58:38 -0400 Subject: [PATCH] Convert keys to symbols when initializing a new `ConfigSL::Config`. --- CHANGELOG.md | 9 +++++++- lib/configsl/config.rb | 2 +- spec/support/configs.rb | 1 + spec/support/configs/config_spec_config.rb | 7 +++++++ spec/unit/configsl/config_spec.rb | 24 ++++++++++++++++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 spec/support/configs/config_spec_config.rb create mode 100644 spec/unit/configsl/config_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 51da2f8..771c945 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog][changelog], and this project adheres to [Semantic Versioning][versioning]. -ß + +## [Unreleased] + +### Fixed + +- Subclasses of `ConfigSL::Config` initialized with sting keys no longer raise + an error. + ## 1.0.0 Initial release. diff --git a/lib/configsl/config.rb b/lib/configsl/config.rb index 9b2faa1..a0d2543 100644 --- a/lib/configsl/config.rb +++ b/lib/configsl/config.rb @@ -19,7 +19,7 @@ class Config def initialize(params = {}) params.each do |name, value| - set_value(name, value) + set_value(name.to_sym, value) end end end diff --git a/spec/support/configs.rb b/spec/support/configs.rb index 9f415de..c1cb1ed 100644 --- a/spec/support/configs.rb +++ b/spec/support/configs.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require_relative 'configs/config_spec_config' require_relative 'configs/dsl_spec_config' require_relative 'configs/format_spec_config' require_relative 'configs/from_environment_spec_config' diff --git a/spec/support/configs/config_spec_config.rb b/spec/support/configs/config_spec_config.rb new file mode 100644 index 0000000..8af73e7 --- /dev/null +++ b/spec/support/configs/config_spec_config.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class ConfigSpecConfig < ConfigSL::Config + option :default, type: String, default: 'rspec-default' + option :optional, type: String + option :required, type: String, required: true +end diff --git a/spec/unit/configsl/config_spec.rb b/spec/unit/configsl/config_spec.rb new file mode 100644 index 0000000..2ecb05d --- /dev/null +++ b/spec/unit/configsl/config_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +RSpec.describe ConfigSL::Config do + subject(:config) { ConfigSpecConfig } + + describe '#initialize' do + it 'raises an error for unknown options' do + expect { config.new(unknown: 'rspec') }.to \ + raise_error(ConfigSL::InvalidOptionError) + end + + it 'sets values for hash keys' do + instance = config.new(required: 'rspec') + + expect(instance.required).to eq('rspec') + end + + it 'sets values for string keys' do + instance = config.new('required' => 'rspec') + + expect(instance.required).to eq('rspec') + end + end +end