Sha256: 34653be5b34cbda3b4e862ea6ddf83dd5ff4847495230442217a83a633ab502a

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module DirModel
  module Import
    class Path # Csv

      attr_reader :path
      attr_reader :index
      attr_reader :current_path
      attr_reader :previous_path

      def initialize(path)
        @path = path
        reset!
      end

      def size
        @size ||= ruby_path.size
      end

      def reset!
        @index = -1
        @current_path = @ruby_path = @previous_path = nil
      end

      def start?
        index == -1
      end

      def end?
        index.nil?
      end

      def next_path
        @next_path ||= _read_path
      end

      def read_path
        @previous_path = @current_path
        unless caching_value?
          @current_path = _read_path { forward! }
        end
        current_path
      end

      protected

      def caching_value?
        if @next_path
          @current_path = @next_path
          @next_path = nil
          forward!
          true
        else
          false
        end
      end

      def ruby_path
        @ruby_path ||= Pathname.glob("#{path}/**/*").each
      end

      def _read_path
        path = ruby_path.next.to_path
        yield if block_given?
        path
      rescue StopIteration
        set_end
       nil
      end

      def forward!
        @index += 1
      end

      def set_end
        @current_path = @index = nil
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dir_model-0.3.0 lib/dir_model/import/path.rb