arbitrary depth access yml with method_missing help

  • Follow


[Note:  parts of this message were removed to make it a legal post.]

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

0
Reply tengxianggong (7) 10/30/2008 2:50:42 AM


0 Replies
33 Views

(page loaded in 0.012 seconds)


Reply: