lib/cockpit/core/settings.rb in cockpit-0.2.3 vs lib/cockpit/core/settings.rb in cockpit-0.2.4
- old
+ new
@@ -1,5 +1,6 @@
+# http://refactormycode.com/codes/1204-nested-hash-from-delimiter-separated-string
module Cockpit
# settings have one direct definition and many child proxy
class Settings
include Global
@@ -177,9 +178,25 @@
end
def empty?(key)
value = self[key]
value.respond_to?(:empty?) ? value.empty? : !value
+ end
+
+ def to_tree
+ keys.inject({}) do |result, path|
+ curr = result
+ names = path.split(".")
+ names.each do |name|
+ if name == names.last
+ curr[name] = Cockpit::Settings[path]
+ else
+ curr[name] ||= {}
+ curr = curr[name]
+ end
+ end
+ result
+ end
end
protected
def method_missing(method, *args, &block)