Sha256: 1359b2bbee6b615239a84e488823ea06ba5a1e01e8bc478b3bcdfe6ba0b7f26a

Contents?: true

Size: 1.26 KB

Versions: 14

Compression:

Stored size: 1.26 KB

Contents

require 'chef_fs/file_system/base_fs_dir'
require 'chef_fs/file_system/nonexistent_fs_object'
require 'chef_fs/file_system/memory_file'

module ChefFS
  module FileSystem
    class MemoryDir < ChefFS::FileSystem::BaseFSDir
      def initialize(name, parent)
        super(name, parent)
        @children = []
      end

      attr_reader :children

      def child(name)
        @children.select { |child| child.name == name }.first || ChefFS::FileSystem::NonexistentFSObject.new(name, self)
      end

      def add_child(child)
        @children.push(child)
      end

      def can_have_child?(name, is_dir)
        root.cannot_be_in_regex ? (name !~ root.cannot_be_in_regex) : true
      end

      def add_file(path, value)
        path_parts = path.split('/')
        dir = add_dir(path_parts[0..-2].join('/'))
        file = MemoryFile.new(path_parts[-1], dir, value)
        dir.add_child(file)
        file
      end

      def add_dir(path)
        path_parts = path.split('/')
        dir = self
        path_parts.each do |path_part|
          subdir = dir.child(path_part)
          if !subdir.exists?
            subdir = MemoryDir.new(path_part, dir)
            dir.add_child(subdir)
          end
          dir = subdir
        end
        dir
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
knife-essentials-1.5.6 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.5.5 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.5.4 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.5.3 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.5.2 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.5.1 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.5 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.4 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.3.2 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.3 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.2.1 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.2 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.1.1 lib/chef_fs/file_system/memory_dir.rb
knife-essentials-1.1 lib/chef_fs/file_system/memory_dir.rb