Sha256: 18f18e151c43a2549869bf81800416ca78d355e5eecc27329a21f0300da6be4b

Contents?: true

Size: 1.37 KB

Versions: 14

Compression:

Stored size: 1.37 KB

Contents

module Lookbook
  module PathUtils
    class << self
      def to_absolute(path)
        File.absolute_path(path.to_s, Rails.root)
      end

      def to_lookup_path(file_path)
        path = file_path.to_s.downcase

        directory_path = File.dirname(path)
        directory_path = nil if directory_path.start_with?(".")

        file_name = File.basename(path).split(".").first

        segments = [*directory_path&.split("/"), file_name].compact
        stripped_segments = segments.map! do |segment|
          PriorityPrefixParser.call(segment).last.tr("-", "_")
        end

        to_path(stripped_segments)
      end

      def to_path(*args)
        args.flatten.compact.map(&:to_s).join("/")
      end

      def normalize_paths(paths, allow_root: false)
        Array(paths).map do |path|
          full_path = to_absolute(path)
          if File.exist?(full_path)
            full_path if allow_root || !root_path?(full_path)
          end
        end.compact.uniq
      end

      def root_path?(path)
        Rails.application.root.to_s == path.to_s
      end

      def strip_slashes(path)
        path.to_s.gsub(/\A\/|\/\z/, "")
      end

      def determine_full_path(rel_path, search_dirs = [])
        base_path = search_dirs.detect { |p| Dir["#{p}/#{rel_path}"].first }
        path = Dir["#{base_path}/#{rel_path}"].first
        Pathname(path) if path
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
lookbook-2.0.0 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.rc.3 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.rc.2 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.rc.1 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.beta.9 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.beta.8 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.beta.7 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.beta.6 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.beta.5 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.beta.4 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.beta.3 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.beta.2 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.beta.1 lib/lookbook/support/utils/path_utils.rb
lookbook-2.0.0.beta.0 lib/lookbook/support/utils/path_utils.rb