# = yamlstruct.rb
#
#   Copyright (c) 2005 Thomas Sawyer
#
#   Ruby License
#
#   This module is free software. You may use, modify, and/or redistribute this
#   software under the same terms as Ruby.
#
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#   FOR A PARTICULAR PURPOSE.
#
# == Author(s)
#
# * Thomas Sawyer
#
# == Developer Notes
#
#   TODO Currently only supports YAML documents with a map at the top-level.
#        Conceivably it could support any object.
#
#   TODO Is this the best name for this class? Maybe YAMLObject would be better?

# Author::    Thomas Sawyer
# Copyright:: Copyright (c) 2006 Thomas Sawyer
# License::   Ruby License

require 'yaml'

# = YAMLStruct
#
# Takes a YAML document and makes it accessable via a delgate object.
#
# Currently this only supports YAML documents with a map at the top-level.
# But this should be easy enough to adjust in the future if it proves
# useful.

class YAMLStruct

  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
      #raise ?
    end
  end

  def to_s
    @yaml.to_yaml
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#

=begin test

  require 'test/unit'

  class TC_YAMLStruct < Test::Unit::TestCase

    def setup
      y = %{
        a: 1
        b: 2
        c: 3
      }
      @y = YAMLStruct.new(y)
    end

    def test_001
      assert_equal( 1, @y.a )
      assert_equal( 2, @y.b )
      assert_equal( 3, @y.c )
    end
    
  end

=end
