Sha256: 67abab38f9a39ba981f5d245fe3b4b059eb949642e2b3134c260f191c560643a

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'dumb_down_viewer/version'
require 'fileutils'
require 'find'
require 'nokogiri'

module DumbDownViewer
  class << self
    attr_reader :filesystem_encoding
  end

  @filesystem_encoding = Encoding.find('locale')

  def self.collect_directories_and_files(path)
    entries = Dir.entries(path) - ['.', '..']
    entries.partition do |entry|
      entry_path = File.expand_path(File.join(path, entry))
      File.directory? entry_path
    end
  end

  def self.build_node_tree(dir)
    dirname, filename = File.split(dir)
    DirNode.new(dirname, filename, 0).tap {|dir| dir.collect_entries }
  end

  class Node
    attr_reader :sub_nodes, :directory, :name, :depth
    attr_accessor :summary

    def initialize(pwd, name, depth)
      @directory = pwd
      @name = name.encode(Encoding::UTF_8)
      @depth = depth
      @name_with_path = pwd.empty? ? @name : File.join(pwd, name)
      setup
    end

    def setup
    end

    def accept(visitor, memo)
      visitor.visit(self, memo)
    end

    def to_s
      @name
    end

    def directory?
      kind_of? DirNode
    end

    def file?
      kind_of? FileNode
    end
  end

  class DirNode < Node
    attr_reader :directories, :files

    def sub_nodes
      (@files + @directories).freeze
    end

    def collect_entries
      dirs, files = DumbDownViewer.collect_directories_and_files(@name_with_path)
      depth = @depth + 1
      @directories = entry_nodes(dirs, DirNode, depth)
      @directories.each {|dir| dir.collect_entries }
      @files = entry_nodes(files, FileNode, depth)
    end

    def entry_nodes(nodes, node_class, depth)
      nodes.map {|node| node_class.new(@name_with_path, node, depth) }
        .sort_by {|node| node.name }
    end
  end

  class FileNode < Node
    attr_reader :extention
    def setup
      extract_extention
      @sub_nodes = [].freeze
    end

    private

    def extract_extention
      m = /\.([^.]+)\Z/.match(@name)
      @extention = m ? m[1] : ''
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dumb_down_viewer-0.1.1 lib/dumb_down_viewer.rb