Created
December 14, 2010 04:54
-
-
Save mojombo/740018 to your computer and use it in GitHub Desktop.
config options mini parser thing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convert an array of nested key parts into a nested Hash and convert the | |
# value to a proper Ruby object. | |
# | |
# keyparts - An Array of String parts of the nested Hash. For instance, the | |
# list ['a', 'b', 'c'] would lead to a nested hash that looks like | |
# {'a' => {'b' => {'c' => X}}}. | |
# val - The String value that will be converted into the proper Boolean, | |
# Integer, or String. | |
# | |
# Returns the nested Hash. | |
def config_options(keyparts, val) | |
if keyparts.empty? | |
case val | |
when 'true' then true | |
when 'false' then false | |
when /^\d+$/ then val.to_i | |
else val | |
end | |
else | |
part = keyparts.shift | |
hash = {} | |
hash[part] = config_options(keyparts, val) | |
hash | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment