-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
to_hash method would be useful #4
Comments
to_hash
method would be useful
Hi! Sorry for late response. The idea make sense, but currently it's not so easy to implement, 'cause we do not store config parameters internally in a Hash (or smth similar) but in instance variables. So, maybe, it would be better to refactor the internal representation. Another question: do you suppose that |
My initial thought was being able to write a method which accepts double splat kwargs ( I would say that the hash returned should definitely be read-only, so that you don't accidentally edit the values thinking you're editing the configuration. Currently the fastest way I found to achieve this without refactoring anything is with module MyCoolGem
def self.config_hash
self.config.instance_variables.map do |attribute|
{ attribute.to_s.tr('@', '').to_sym => self.config.instance_variable_get(attribute) }
end
end
end It's intricate, needs to drop the |
We can use def to_h
self.class.config_attributes.each_with_object({}) do |key, obj|
obj[key.to_sym] = send(key)
end.freeze
end The above solution has one caveat: we can have complex values (Hash, Array), so we should def to_h
self.class.config_attributes.each_with_object({}) do |key, obj|
...
end.deep_dup.deep_freeze
end That should work. |
Looks nice and maybe worth implementing. Though |
We have our own simplified |
Awesome! 👍 |
Released as v1.1.0 |
A
to_hash
orto_h
method callable onMyCoolGem.config
would be useful to pass the values around in bulk, without having to call the specific attribute name.MyCoolGem.config.load
kinda return an hash but it callsload_from_sources
which may not be the behavior expected.The text was updated successfully, but these errors were encountered: