Sha256: 904baab2d3f1a17406eb10fdc2b3c5e872852e92dd67a7a1217dc9402b97e46e

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 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 &Bunch.method(:tree_for)
        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) }
          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 write_to_file(dir)
      return if filenames.count == 0
      super
    end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bunch-0.0.11 lib/bunch/directory_node.rb
bunch-0.0.10 lib/bunch/directory_node.rb
bunch-0.0.9 lib/bunch/directory_node.rb
bunch-0.0.8 lib/bunch/directory_node.rb
bunch-0.0.7 lib/bunch/directory_node.rb