6
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
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/configer/json.rb', line 6
def mount_json_files opts= {}
raise(ArgumentError) unless opts.class <= Hash
require 'json'
[:r,:folder,:dir,:directory].each do |sym|
opts[:root] ||= opts.delete(sym)
end
opts[:root] ||= Dir.pwd
opts[:out] ||= opts.delete(:o) || opts.delete(:to) || Configer::Object
raise(ArgumentError,"out/to must point to hashlike object") unless opts[:out].class <= ::Hash
opts[:out].__send__ :extend, HashExtension unless opts[:out].respond_to?(:deep_merge!)
Dir.glob( File.join( File.absolute_path(opts[:root]), "**","*.{json}" ) ).each do |file_path|
var= file_path.sub(opts[:root],"").split('.')
var.pop
var= var.join('.')
path_elements= var.split(File::Separator)
path_elements.delete('')
tmp_hsh= {}
current_obj= nil
path_elements.count.times { |index|
key_str= path_elements[index]
(current_obj ||= tmp_hsh)
current_obj[key_str]= {}
current_obj= current_obj[key_str] unless index == (path_elements.count-1)
}
current_obj[ path_elements.last ]= JSON.parse File.read file_path
opts[:out].deep_merge!(tmp_hsh)
return nil
end
end
|