Sha256: beee2698266bb4c96e9941d18dae66e6dfe5cc81bd9dea3d438ed0f98f2a40d5

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

module Bunch
  class DirectoryNode < AbstractNode
    attr_reader :root

    def initialize(fn)
      @root = Pathname.new(fn)
    end

    def filenames
      Dir[@root.join("*")].select { |f| f !~ /_\.yml$/ }
    end

    def children
      @children ||= begin
        children = filenames.map do |filename|
          Bunch.tree_for(filename, @options)
        end

        ordering_file = @root.join('_.yml')

        if File.exist?(ordering_file)
          ordering = YAML.load_file(ordering_file)
          ordered, unordered = children.partition { |c| ordering.include?(c.name) }

          (ordering - ordered.map(&:name)).each do |f|
            $stderr.puts "WARNING: directory #{ full_name } has no object #{ f }"
          end

          ordered.sort_by { |c| ordering.index(c.name) } + unordered.sort_by(&:name)
        else
          children.sort_by(&:name)
        end
      end
    end

    def target_extension
      @target_extension ||= begin
        exts = children.map(&:target_extension).compact.uniq
        if exts.count == 1
          exts.first
        elsif exts.count > 1
          raise "Directory #{name} contains non-homogeneous nodes: #{exts.inspect}"
        else
          nil
        end
      end
    end

    def content
      @content ||= children.map(&:content).join
    end

    def name
      File.basename(@root)
    end

    def full_name
      @root
    end

    def write_to_file(dir)
      return if filenames.count == 0
      super
    end

    def write_to_dir(dir)
      super

      if @options[:recurse]
        directory_name = File.join(dir, name)
        FileUtils.mkdir(directory_name)
        children.each do |child|
          child.write_to_dir(directory_name)
        end
      end
    end

    def inspect
      "#<DirectoryNode @root=#{@root.inspect} @children=#{children.inspect}>"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bunch-0.2.2 lib/bunch/directory_node.rb
bunch-0.2.1 lib/bunch/directory_node.rb