Sha256: b2773f34e2be9a6bbc498a56bf168a0df961b677897f29b06611ab88fac145c6

Contents?: true

Size: 921 Bytes

Versions: 2

Compression:

Stored size: 921 Bytes

Contents

# -*- coding: utf-8 -*-
module TreeRb

  #
  # Visit a directory tree
  # not TreeNode related
  #
  class DirProcessor

    def initialize( &action )
      @processors = {}
      @default_processor = action
    end

    def add_processor( re, &action )
      @processors[ re ] = action
    end

    def process( dirname )
      @dirname = dirname
      old_dirname = Dir.pwd
      Dir.chdir( @dirname )
      Dir['**/*'].each { |f|
        pn = Pathname.new( f ).expand_path
        # puts "#{self.class.name}#loadfromdir #{f}"
        next if pn.directory?
        process_file( pn )
      }
      Dir.chdir( old_dirname )
      self
    end

    private

    def process_file( pn )
      # puts "file: #{f}"
      pair = @processors.find { |re,action| re =~ pn.to_s }
      unless pair.nil?
        pair[1].call( pn )
      else
        @default_processor.call( pn ) if @default_processor
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tree.rb-0.3.13 lib/tree_rb/input_plugins/file_system/dir_processor.rb
tree.rb-0.3.12 lib/tree_rb/input_plugins/file_system/dir_processor.rb