Sha256: e720a1b14b8c147d042f920de416413fe77ee991f2bfc9cbdedf8841846fb77c

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 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
      "#<Node @root=#{@root.inspect} @children=#{children.inspect}>"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bunch-0.0.1 lib/bunch/directory_node.rb