Skip to content

Commit 8c894be

Browse files
authored
Add explicit implementation of Config::Options#as_json (#292)
1 parent 4e3a9ea commit 8c894be

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
* Added alias to_h for to_hash ([#277](https://github.com/railsconfig/config/issues/277))
88
* Prevent unnecessary doubled loading of environment variables ([#291](https://github.com/rubyconfig/config/pull/291))
9+
* Return `Hash` from `Config::Options#as_json` instead of `Array` of pairs when using ActiveSupport Core Extensions ([#292](https://github.com/rubyconfig/config/pull/292))
910

1011
### Changes
1112

lib/config/options.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ def to_json(*args)
129129
to_hash.to_json(*args)
130130
end
131131

132+
def as_json(options = nil)
133+
to_hash.as_json(options)
134+
end
135+
132136
def merge!(hash)
133137
current = to_hash
134138
DeepMerge.deep_merge!(

spec/options_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,19 @@
224224

225225
end
226226

227+
context 'when calling #as_json' do
228+
it 'should return a Hash of the keys and values' do
229+
unless defined?(::Rails)
230+
skip <<~REASON
231+
Config::Options#as_json raises a runtime error unless Active Support
232+
Core Extensions are loaded. We don't expect users to call this method
233+
at all if they're not using AS, so we disable this test except when
234+
running the suite against Rails.
235+
REASON
236+
end
237+
238+
options = Config::Options.new(foo: :bar)
239+
expect(options.as_json).to eq({ 'foo' => 'bar' })
240+
end
241+
end
227242
end

0 commit comments

Comments
 (0)