Class: Skunk::Generator::Html::PathTruncator

Inherits:
Object
  • Object
show all
Defined in:
lib/skunk/generators/html/path_truncator.rb

Overview

Utility class for truncating file paths to show only the relevant project structure

Constant Summary collapse

PROJECT_FOLDERS =

Common project folder names to truncate from

%w[app lib src test spec features db].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ PathTruncator

Returns a new instance of PathTruncator.



25
26
27
# File 'lib/skunk/generators/html/path_truncator.rb', line 25

def initialize(file_path)
  @file_path = file_path.to_s
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



8
9
10
# File 'lib/skunk/generators/html/path_truncator.rb', line 8

def file_path
  @file_path
end

Class Method Details

.truncate(file_path) ⇒ String

Truncates a file path to show only the relevant project structure starting from the first project folder found

:reek:NilCheck

Parameters:

  • file_path (String)

    The full file path to truncate

Returns:

  • (String)

    The truncated path starting from the project folder



19
20
21
22
23
# File 'lib/skunk/generators/html/path_truncator.rb', line 19

def self.truncate(file_path)
  return file_path if file_path.nil?

  new(file_path).truncate
end

Instance Method Details

#truncateObject

:reek:TooManyStatements



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/skunk/generators/html/path_truncator.rb', line 30

def truncate
  return file_path if file_path.empty?

  path_parts = file_path.split("/")
  folder_index = path_parts.find_index do |part|
    PROJECT_FOLDERS.include?(part)
  end

  if folder_index
    # rubocop:disable Style/SlicingWithRange
    path_parts[folder_index..-1].join("/")
    # rubocop:enable Style/SlicingWithRange
  else
    file_path
  end
end