Sha256: d302d447f75f6d54bef953dbf663494a8eed1e78bc5a61f4afbafcc958e52525

Contents?: true

Size: 1.18 KB

Versions: 3

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)
        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 contents
      @contents ||= children.map(&:contents).join
    end

    def name
      File.basename(@root)
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bunch-0.0.4 lib/bunch/directory_node.rb
bunch-0.0.3 lib/bunch/directory_node.rb
bunch-0.0.2 lib/bunch/directory_node.rb