Forum: Ruby arbitrary depth access yml with method_missing help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tim T. (Guest)
on 2008-10-30 04:51
(Received via mailing list)
Hi All,
  Say I have a class named *TrackCompany ,  *I want to recursively get
some
value from yml, like  *TrackCompany.instance.company01.log_file*,
I use the methoding missing to implement it, so currently it will not
recursively call method_missing, can anybody help?

  the yml defined :

 ========= yml begin ========
  tracks:
     track01: #track name
         company01: #company name
            default_run: true
            log_file: D:\logfile.log
 ========= yml end ===========

 and I code the class to get value from it, like :

======== code begin ===

 require 'singleton'
 require 'yaml'

class TrackCompanyConfig
 include Singleton

 def initialize

   @file_path = "./track_company_config.yml"
   if File.exists?(@file_path)
     @configs = YAML.load_file(@file_path)["tracks"]
   else
     raise "Default config file missing exception,
./track_company_config.yml missing!"
   end

 end

 def method_missing method_sym,*args

     method_sym = method_sym.to_s

     if @configs[method_sym].is_a?(Hash)
        p "then come to next level"
        @configs = @configs[method_sym]  # it just run once, how to make
it
run recursively?
     else
         value = @configs[method_sym]
         @configs = YAML.load_file(@file_path)["tracks"]
         value
     end

   end
 end
end

===== code end =====

Thanks.
Tim
This topic is locked and can not be replied to.