Class: AutoData::Parse
- Inherits:
-
Object
show all
- Defined in:
- lib/auto_data.rb
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Parse
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/auto_data.rb', line 7
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
@name_def_key =file.split('/').last.to_s.split('.').first
@files['name_' + @file_count.to_s]=@name_def_key
@info = begin
YAML.load_file(file)
rescue Exception => e
puts "Could not parse auto objects files: #{e.message}"
raise Exception.new('Could not parse auto objects files: #{e.message}')
end
@files["#{@name_def_key}_default_key"]= begin
@info["default_key"].nil? ? 'NotFound' : @info["default_key"]
rescue NoMethodError => e
puts "Couldn't find key #{default_key} - #{e.message}"
end
@file_count +=1
end }
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(filename, *args) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/auto_data.rb', line 46
def method_missing (filename, *args)
use_default_key= false
if args[0].count('.') == 0
use_default_key=true
subkey=args[0].to_s
else
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]
end
load(filename)
if use_default_key
if @files.key?(filename.to_s + '_default_key')
default_key = @files["#{filename}_default_key"]
else
raise Exception.new("Couldn't find key default_key. ")
end
result= begin
@file["#{default_key}"]["#{subkey}"].nil? ? 'No Value Found' : @file["#{default_key}"]["#{subkey}"]
rescue NoMethodError => e
puts "Couldn't find key #{subkey} using 'default_key' #{e.message}"
raise Exeption.new("Couldn't find key #{subkey} using 'default_key' #{e.message}")
end
else
result= begin
@file["#{key}"]["#{subkey}"].nil? ? 'No Value Found' : @file["#{key}"]["#{subkey}"]
rescue NoMethodError => e
puts "Couldn't find key #{key}.#{subkey} #{e.message}"
raise Exception.new("Couldn't find key #{key}.#{subkey} #{e.message}" )
end
end
result
end
|
Instance Method Details
#change_scope(filename, new_value) ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/auto_data.rb', line 37
def change_scope(filename, new_value)
if !@files.value?(filename)
raise Exception.new("File name '#{filename}' is not found")
else
@files["#{filename}_default_key"] = new_value
end
end
|