Created
June 24, 2009 09:22
-
-
Save trans/135115 to your computer and use it in GitHub Desktop.
YAMLStruct
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
# Turn a YAML document into an object via method_missing delegation. | |
# This is a pretty useless idea since you can just use OpenStruct. | |
# | |
# OpenStruct.new(YAML.load(yaml)) | |
# | |
class YAMLStruct < BasicObject | |
def initialize( stream ) | |
@yaml = YAML::load( stream ) | |
end | |
def method_missing( sym, *args, &blk ) | |
sym = sym.to_s | |
if sym.slice(-1,1) == '=' | |
@yaml[ sym ] = args[0] | |
elsif @yaml.key?( sym ) | |
@yaml[ sym ] | |
else | |
super | |
end | |
end | |
def to_s | |
@yaml.to_yaml | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment