Sha256: c4eb9beada294a7968ce6ac645fd72cd397a9598b724ce762602ff34236be5a9

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

require 'active_model'

module DirModel
  module Import
    class Dir # File
      extend ActiveModel::Callbacks

      define_model_callbacks :next

      delegate :size, :index, :end?, to: :path

      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 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?

        run_callbacks :next do
          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?
        end

        current_dir_model
      end

      private

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

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dir_model-0.7.1 lib/dir_model/import/dir.rb