Class: AutoData::Parse
- Inherits:
-
Object
- Object
- AutoData::Parse
- Defined in:
- lib/auto_data.rb
Instance Method Summary collapse
-
#initialize ⇒ Parse
constructor
TODO: Make all methods private.
- #load(file) ⇒ Object
- #method_missing(filename, *args) ⇒ Object
Constructor Details
#initialize ⇒ Parse
TODO: Make all methods private
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/auto_data.rb', line 8 def initialize() @gvar=ENV['AUTO_DATA_PATH'].to_s if @gvar.length == 0 raise Exception.new("Variable is not defined : ENV[\'AUTO_DATA_PATH\']") end @files = Hash.new @file_count=0 Dir[ @gvar + '/**/*.yml'].each { |file| begin @files['fullpath_' + @file_count.to_s]=file @files['filename_' + @file_count.to_s]=file.split('/').last @files['name_' + @file_count.to_s]=file.split('/').last.to_s.split('.').first @file_count +=1 end} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(filename, *args) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/auto_data.rb', line 51 def method_missing (filename, *args) fileinfo= args[0].to_s.split('.') if fileinfo.size !=2 raise Exception.new('Malformed structure, must be <AutoData.filename(\'key.subkey\')> ') end key= fileinfo[0] subkey= fileinfo[1] load(filename) result= begin @file["#{key}"]["#{subkey}"].nil? ? 'No Value Found' : @file["#{key}"]["#{subkey}"] rescue NoMethodError => e puts "Couldn't find key #{subkey}.#{subkey} #{e.message}" end result end |
Instance Method Details
#load(file) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/auto_data.rb', line 28 def load(file) if @files.value?(file).nil? raise Exception.new("File name is not found #{file}") end key_value= @files.key(file.to_s).to_s key_2find = 'fullpath_' + key_value.split('_').last local_file_path =nil @files.each_pair {|key,value| begin if key == key_2find local_file_path = value break end end} @file = begin YAML.load_file(local_file_path) rescue Exception => e puts "Could not parse auto objects files: #{e.message}" raise Exception.new('Could not parse auto objects files: #{e.message}') end end |