Skip to content

Commit

Permalink
Stringify keys when reading from Hash source
Browse files Browse the repository at this point in the history
  • Loading branch information
bolshakov committed Jun 9, 2024
1 parent 6b62872 commit 36ff1b7
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 19 deletions.
17 changes: 16 additions & 1 deletion lib/config_x/hash_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,26 @@ class HashSource < Source
attr_reader :source
protected :source

class << self
def deep_stringify_keys(hash)
hash.each_with_object({}) do |(key, value), acc|
acc[key.to_s] =
if Hash === value
deep_stringify_keys(value)
else
value
end
end
end
end

def initialize(source)
@source = source
end

def load = source
def load
self.class.deep_stringify_keys(source)
end

def ==(other)
other.is_a?(self.class) && source == other.source
Expand Down
2 changes: 2 additions & 0 deletions sig/config_x/hash_source.rbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

module ConfigX
class HashSource < Source
def self.deep_stringify_keys: [T] (Hash[(String | Symbol), T])-> Hash[String, T]

def initialize: (Source::config_hash) -> void

attr_reader source: Source::config_hash
Expand Down
40 changes: 34 additions & 6 deletions spec/config_x/config_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,50 @@
end

around do |example|
settings__three = ENV["SETTINGS__THREE"]
ENV["SETTINGS__THREE"] = "environment variable"
settings__three = ENV["SETTINGS__ONE__THREE"]
ENV["SETTINGS__ONE__THREE"] = "environment variable"
example.run
ENV["SETTINGS__THREE"] = settings__three
ENV["SETTINGS__ONE__THREE"] = settings__three
end

it "loads config files in order" do
is_expected.to have_attributes(
one: "settings/development.local.yml",
two: "settings/development.local.yml",
three: "environment variable",
one: have_attributes(
two: "settings/development.local.yml",
three: "environment variable"
),
four: "settings.local.yml",
five: "settings/development.yml",
six: "settings.yml"
)
end
end
end

describe '#load' do
subject(:config_factory) do
described_class.new(
"development",
config_root: "spec/support/config"
).load(*additional_sources)
end

let(:additional_sources) do
[
{one: { two: "additional hash source"} },
]
end

it "loads config files in order" do
is_expected.to have_attributes(
one: have_attributes(
two: "additional hash source",
three: "settings/development.local.yml",
),
four: "settings.local.yml",
five: "settings/development.yml",
six: "settings.yml"
)
end
end
end
8 changes: 8 additions & 0 deletions spec/config_x/hash_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@
end

it { expect(config).to eq(hash) }

context "when keys are symbols" do
let(:hash) { { four: { bar: 42 } } }

it "deep stringifies keys" do
expect(config).to eq("four" => {"bar" => 42})
end
end
end
6 changes: 3 additions & 3 deletions spec/support/config/settings.local.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
one: settings.local.yml
two: settings.local.yml
three: settings.local.yml
one:
two: settings.local.yml
three: settings.local.yml
four: settings.local.yml
6 changes: 3 additions & 3 deletions spec/support/config/settings.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
one: settings.yml
two: settings.yml
three: settings.yml
one:
two: settings.yml
three: settings.yml
four: settings.yml
five: settings.yml
six: settings.yml
6 changes: 3 additions & 3 deletions spec/support/config/settings/development.local.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
one: settings/development.local.yml
two: settings/development.local.yml
three: settings/development.local.yml
one:
two: settings/development.local.yml
three: settings/development.local.yml
6 changes: 3 additions & 3 deletions spec/support/config/settings/development.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
one: settings/development.yml
two: settings/development.yml
three: settings/development.yml
one:
two: settings/development.yml
three: settings/development.yml
four: settings/development.yml
five: settings/development.yml

0 comments on commit 36ff1b7

Please sign in to comment.