Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a hash with indifferent access rather than a Mash #29

Merged
merged 1 commit into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class NotFound < RuntimeError; end
class FileError < RuntimeError; end
class AlreadyLoaded < RuntimeError; end

class SettingHash < Hash
include Hashie::Extensions::IndifferentAccess
include Hashie::Extensions::KeyConversion
include Hashie::Extensions::DeepMerge
end

include Singleton
NUM_KLASS = if RUBY_VERSION.split(/\./)[0].to_i == 2 && RUBY_VERSION.split(/\./)[1].to_i >= 4
Integer
Expand Down Expand Up @@ -87,7 +93,7 @@ def self.available_settings
#=================================================================

def initialize
@available_settings ||= Hashie::Mash.new
@available_settings ||= SettingHash.new
end

def has_key?(key)
Expand All @@ -105,6 +111,9 @@ def value_for(key, args = [])
end

v = @available_settings[name]
if v.is_a?(Hash)
v = SettingHash[v]
end
if block_given?
v = yield(v, args)
end
Expand Down Expand Up @@ -151,7 +160,7 @@ def loaded?

def load(params)
# reset settings hash
@available_settings = Hashie::Mash.new
@available_settings = SettingHash.new
@loaded = false

files = []
Expand Down
1 change: 1 addition & 0 deletions spec/mc_settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
expect(subject[:two][:three]).to eq(5)
expect(subject['two'][:three]).to eq(5)
expect(subject[:two]['three']).to eq(5)
expect(subject[:two].symbolize_keys[:three]).to eq(5)
end

context "working with arrays" do
Expand Down