Sha256: a30fa02907c778084133910febb8ef495e14fec645843b3050b0ce4ac97dd875

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

require 'chef_fs/file_system/base_fs_object'
require 'chef_fs/file_system/nonexistent_fs_object'

module ChefFS
  module FileSystem
    class MultiplexedDir < BaseFSDir
      def initialize(*multiplexed_dirs)
        @multiplexed_dirs = multiplexed_dirs.flatten
        super(@multiplexed_dirs[0].name, @multiplexed_dirs[0].parent)
      end

      attr_reader :multiplexed_dirs

      def write_dir
        multiplexed_dirs[0]
      end

      def children
        @children ||= begin
          result = []
          seen = {}
          # If multiple things have the same name, the first one wins.
          multiplexed_dirs.each do |dir|
            dir.children.each do |child|
              if seen[child.name]
                Chef::Log.warn("Child with name '#{child.name}' found in multiple directories: #{seen[child.name].path_for_printing} and #{child.path_for_printing}")
              else
                result << child
                seen[child.name] = child
              end
            end
          end
          result
        end
      end

      def can_have_child?(name, is_dir)
        write_dir.can_have_child?(name, is_dir)
      end

      def create_child(name, file_contents = nil)
        @children = nil
        write_dir.create_child(name, file_contents)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
knife-essentials-1.5.6 lib/chef_fs/file_system/multiplexed_dir.rb
knife-essentials-1.5.5 lib/chef_fs/file_system/multiplexed_dir.rb
knife-essentials-1.5.4 lib/chef_fs/file_system/multiplexed_dir.rb