Sha256: 8b3f3c6da0b666633e9445fcef2d1d8c7c3cfd4c7769c86e9d135949943bda5c

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 KB

Contents

require 'forwardable'

module DirModel
  module Import
    class Dir # File
      extend Forwardable

      attr_reader :index
      attr_reader :path
      attr_reader :import_dir_model_class
      attr_reader :context
      attr_reader :current_dir_model
      attr_reader :previous_dir_model

      def_delegators :@path, :end?

      def initialize(source_path, import_dir_model_class, context={})
        @path, @import_dir_model_class, @context = Path.new(source_path), import_dir_model_class, context.to_h.symbolize_keys
        reset
      end

      def reset
        path.reset!
        @index = -1
        @current_dir_model = nil
      end

      def each(context={})
        return to_enum(__callee__) unless block_given?

        while self.next(context)
          next if skip?
          yield current_dir_model
        end
      end

      def next(context={})
        return if end?

        context = context.to_h.reverse_merge(self.context)
        @previous_dir_model = current_dir_model
        @current_dir_model = import_dir_model_class.next(path, context, previous_dir_model)
        @index += 1
        @current_dir_model = @index = nil if end?

        current_dir_model
      end

      private

      def skip?
        !!current_dir_model.try(:skip?)
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
dir_model-0.7.0 lib/dir_model/import/dir.rb
dir_model-0.6.2 lib/dir_model/import/dir.rb
dir_model-0.6.1 lib/dir_model/import/dir.rb
dir_model-0.6.0 lib/dir_model/import/dir.rb
dir_model-0.5.2 lib/dir_model/import/dir.rb
dir_model-0.5.1 lib/dir_model/import/dir.rb
dir_model-0.5.0 lib/dir_model/import/dir.rb
dir_model-0.4.0 lib/dir_model/import/dir.rb
dir_model-0.3.4 lib/dir_model/import/dir.rb
dir_model-0.3.3 lib/dir_model/import/dir.rb
dir_model-0.3.2 lib/dir_model/import/dir.rb
dir_model-0.3.1 lib/dir_model/import/dir.rb
dir_model-0.3.0 lib/dir_model/import/dir.rb