Sha256: 40f276685ae2f63bbba4bd020ede1d365bccc5bbe503a233f375221e95cdd4b2

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require 'rom/memory/dataset'
require 'rom/files/constants'

module ROM
  module Files
    class Dataset < Memory::Dataset
      module Paths
        def self.included(other)
          super(other)
          other.extend ClassInterface
        end

        module ClassInterface
          # @return [Proc]
          def row_proc
            ->(path) { Hash[ID => path] }
          end
        end

        # @!attribute [r] row_proc
        #   @return [Proc]

        # @return [Array<Pathname>]
        def paths
          connection.search(search_patterns, exclude_patterns: exclude_patterns, sorting: sorting, path: path)
        end

        # Pluck values from a pathname property
        #
        # @overload pluck(field)
        #
        # @example Usage with Symbol
        #   users.pluck(:extname).uniq
        #   # %w[.rb .rbw]
        #
        # @param [#to_proc, nil] field A name of the property for extracting values from pathname
        #
        # @overload pluck { |pathname| ... }
        #
        # @example Usage with block
        #   users.pluck { |pathname| pathname.basename.to_s }
        #   # [1, 2, 3]
        #
        # @return [Array]
        #
        # @api public
        def pluck(field = nil, &block)
          block ||= field&.to_proc || row_proc
          paths.map(&block)
        end

        # @return [Array<Hash{Symbol => Pathname, String}>]
        def data
          pluck(row_proc)
        end

        alias to_a data
        alias to_ary to_a

        # Iterate over data using row_proc
        #
        # @return [Enumerator, Array] if block is not given
        #
        # @api private
        def each
          return to_enum unless block_given?
          paths.each { |tuple| yield(row_proc[tuple]) }
        end

        # @return [Integer]
        def count
          to_a.size
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-files-0.2.0 lib/rom/files/dataset/paths.rb