Sha256: 0a550a1ec7db493aa71eaea786e0873ec7b194cc44efeeaa017385827ded23f3

Contents?: true

Size: 856 Bytes

Versions: 2

Compression:

Stored size: 856 Bytes

Contents

require 'ostruct'
require 'yaml'

module TreeConfig
  class DirectoryTree
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tree_config-0.0.3 lib/tree_config/vendor/directory_tree.rb
tree_config-0.0.2 lib/tree_config/vendor/directory_tree.rb