summaryrefslogtreecommitdiff
path: root/lib/chef/chef_fs/file_system/memory/memory_dir.rb
blob: fdef10ff564d2280e6238a50d27a7364bdcbad6f (plain)
1
2
3
4
5
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
48
49
50
51
52
53
require_relative "../base_fs_dir"
require_relative "memory_file"

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

          attr_reader :children

          def make_child_entry(name)
            @children.find { |child| child.name == name }
          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
  end
end