Sha256: 5ec37837f4ce520e4f90ed1bc3ffd65851679ceb7632987d601a465761d26fe7

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true
require 'pathname'
require_relative 'path_utils'

module Decant
  class Collection
    # @return [Pathname]
    attr_reader :dir

    # @return [String, nil]
    attr_reader :ext

    # @param dir [Pathname, String]
    # @param ext [String, nil]
    def initialize(dir:, ext: nil)
      self.dir = dir
      self.ext = ext
    end

    # @return [Array<Pathname>]
    def entries
      glob('**/*')
    end

    # If {#ext} is defined then +pattern+ MUST NOT include the file's extension
    # as it will automatically be added, if {#ext} is +nil+ then +pattern+ MUST
    # include the file's extension - essentially becoming the file's full
    # relative path within {#dir}.
    #
    # Technically +pattern+ can be any pattern supported by +Dir.glob+ though
    # it's more likely to simply be a file name.
    #
    # @param pattern [String]
    #
    # @return [Pathname, nil]
    def find(pattern)
      glob(pattern).first
    end

    # @param pattern [String]
    #
    # @return [Array<Pathname>]
    def glob(pattern)
      dir.glob("#{pattern}#{ext}").select { |path| path.file? }
    end

    # The extension-less relative path of +path+ within {#dir}.
    #
    # @param path [Pathname]
    #
    # @return [String]
    def slug_for(path)
      relative_path = path.relative_path_from(dir).to_s

      # The collection has no configured extension, files are identified by
      # their full (relative) path so there's no extension to remove.
      return relative_path if @delete_ext_regexp.nil?

      relative_path.sub(@delete_ext_regexp, '')
    end

    private
      def dir=(value)
        @dir = Pathname.new(value)
      end

      def ext=(value)
        if value
          @ext = value.start_with?('.') ? value : ".#{value}"
          @delete_ext_regexp = PathUtils.delete_ext_regexp(ext)
        else
          @ext = nil
          @delete_ext_regexp = nil
        end
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decant-0.2.0 lib/decant/collection.rb