rubyconfig / config

Easiest way to add multi-environment yaml settings to Rails, Sinatra, Padrino and other Ruby projects.
Other
2.11k stars 231 forks source link

Add explicit implementation of Config::Options#as_json #292

Closed cjlarose closed 3 years ago

cjlarose commented 3 years ago

Fixes #287

Before, Config::Options#as_json was not explicitly defined, but the class included the Enumerable module. So, if you were using ActiveSupport Core Extensions, then the definition of #as_json for Enumerable was invoked, which would represent the Config::Options as a Array of key-value pairs instead of a Hash.

This changes brings #as_json in line with the output we get from to_json: essentially, calling these methods on an instance of Config::Options behaves the same as if they were called instead on the hash returned by to_hash.

It's not clear if this is a "bug fix" or a "breaking change" from a semver perspective, but I think it's unlikely that someone was depending on the output from #as_json to be the Array-of-pairs representation.

cjlarose commented 3 years ago

Tests fail when using the Sinatra Gemfiles, which is more-or-less expected: if you don't have the ActiveSupport Core Extensions loaded, then Hash#as_json isn't defined.

I don't know yet what the right fix is. I'm considering skipping the test except when testing against Rails, since the method sort of has an implicit dependency on ActiveSupport. If a user isn't in the Rails ecosystem or otherwise using ActiveSupport, then I wouldn't expect them to call #as_json anyway.

It's possible to, at runtime, check to_hash.repsond_to?(:as_json) and do something differently if it doesn't respond to that method, but it's not 100% clear what we'd return in that case.

cjlarose commented 3 years ago

@pkuczynski Updated to skip the test except when running against Rails. LMK what you think!

pkuczynski commented 3 years ago

Looks very good to me! I will merge it in and release new version :) Thanks for all your hard work on this @cjlarose!