Sha256: 352238c253a79d2676cbd128176be5bccded288fba799e51de21d1dd892593d8

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

# Redwood::FileNode stores a Directory tree in a Redwood structure. 
# FileNodes respond to methods of the File class.
# eg. FileNode#chmod, FileNode#stat, etc.

module Redwood
  class FileNode < Node
    
    # Takes a path and scans the directory, creating a Redwood tree.
    def self.scandir(dir = Dir.pwd, tree=nil)
      node = tree ? tree : self.new(dir)
      if File.directory?(dir)
        Dir.foreach(dir) do |d|
          if File.directory?("#{dir}/#{d}")
            node << scandir("#{dir}/#{d}",tree) unless (d.eql?('..') || d.eql?('.'))
          else
            node.add_child("#{dir}/#{d}")
          end
        end
      else
        node.add_child(dir)
      end
      node
    end
    
    attr_reader :path
    
    def initialize(name, parent=nil)
      super
      @path = File.expand_path(name)
    end
    
    # Some information to store in the node. Defaults to the basename of the file/directory
    def value
      @value ||= basename
    end
    
    def method_missing(method, *args, &block)
      if File.respond_to?(method)
        File.send method, path
      else
        super
      end
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
redwood-0.1.2 lib/redwood/filenode.rb
redwood-0.1.1 lib/redwood/filenode.rb
redwood-0.1.0 lib/redwood/filenode.rb