Class: TreeConfig::DirectoryTree

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_config/vendor/directory_tree.rb

Class Method Summary collapse

Class Method Details

.load(dir, struct_obj) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tree_config/vendor/directory_tree.rb', line 6

def self.load(dir, struct_obj)
  Dir.foreach(dir) do |entry|
    next if (entry == '..' || entry == '.')
    full_path = File.join(dir, entry)
    if File.directory? (full_path)
      struct_obj.send("#{entry}=", DeepStruct.new)
      struct_obj.send("#{entry}=", self.load(full_path, struct_obj.send("#{entry}")))
    else
      next if entry.empty? || (entry[-3,3] != "yml" && entry[-4,4] != "yaml") 
      hash = ActiveSupport::HashWithIndifferentAccess.new(
        begin 
          YAML.load(File.open(full_path))
        rescue
        end
      )
      name = File.basename(entry, '.*')
      struct_obj.send("#{name}=", DeepStruct.new(hash))
    end
  end
  return struct_obj
end