Sha256: 9a836877f1cc406b42fb00ef4fa568fc63366dc075d4700bceaa1b6c5e247f91

Contents?: true

Size: 1.14 KB

Versions: 39

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Bridgetown
  # A singleton class that caches frozen instances of path strings returned from its methods.
  #
  # NOTE:
  #   This class exists because `File.join` allocates an Array and returns a new String on every
  #   call using **the same arguments**. Caching the result means reduced memory usage.
  #   However, the caches are never flushed so that they can be used even when a site is
  #   regenerating. The results are frozen to deter mutation of the cached string.
  #
  #   Therefore, employ this class only for situations where caching the result is necessary
  #   for performance reasons.
  #
  class PathManager
    # This class cannot be initialized from outside
    private_class_method :new

    # Wraps `File.join` to cache the frozen result.
    # Reassigns `nil`, empty strings and empty arrays to a frozen empty string beforehand.
    #
    # Returns a frozen string.
    def self.join(base, item)
      base = "" if base.nil? || base.empty?
      item = "" if item.nil? || item.empty?
      @join ||= {}
      @join[base] ||= {}
      @join[base][item] ||= File.join(base, item).freeze
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
bridgetown-core-0.21.0.beta1 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.20.0 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.19.3 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.19.2 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.19.1 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.19.0 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.18.6 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.18.5 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.18.4 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.18.3 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.18.2 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.18.1 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.18.0 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.17.1 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.17.0 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.16.0 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.16.0.beta2 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.16.0.beta1 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.15.0 lib/bridgetown-core/path_manager.rb
bridgetown-core-0.15.0.beta4 lib/bridgetown-core/path_manager.rb