Sha256: 736aa7836f6ba32cb86cf8ca17b6638a3062a739f3bf7f74900bfcc904a31daa

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

module Bunch
  class DirectoryNode
    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
        else
          raise "Directory contains non-homogeneous nodes: #{exts.inspect}"
        end
      end
    end

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

    def name
      File.basename(@root)
    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.0.6 lib/bunch/directory_node.rb
bunch-0.0.5 lib/bunch/directory_node.rb