Sha256: d710e4d47b62bb4b8dd6c872353b84ed3f8a322a610078fd27ce8a85cb30633a

Contents?: true

Size: 904 Bytes

Versions: 8

Compression:

Stored size: 904 Bytes

Contents

# frozen_string_literal: true

module JsDependency
  class ReplacePathAlias
    # @param [String] str
    # @param [Hash, nil] alias_paths
    def initialize(str, alias_paths = nil)
      @str = str
      @alias_paths = alias_paths
    end

    # @param [String] str
    # @param [Hash, nil] alias_paths
    # @return [String]
    def self.call(str, alias_paths = nil)
      return str if alias_paths.nil? || alias_paths.empty?

      new(str, alias_paths).call
    end

    # @return [String]
    def call
      return @str if safe_list.include?(@str)

      replace_alias_paths(@str, @alias_paths)
    end

    private

    # @param [String] str
    # @param [nil, Hash] alias_paths
    def replace_alias_paths(str, alias_paths)
      alias_paths.each do |(key, value)|
        str = str.gsub(/^#{key}/, value.to_s)
      end
      str
    end

    def safe_list
      ["@rails/ujs"]
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
js_dependency-0.3.0 lib/js_dependency/replace_path_alias.rb
js_dependency-0.2.3.1 lib/js_dependency/replace_path_alias.rb
js_dependency-0.2.3 lib/js_dependency/replace_path_alias.rb
js_dependency-0.2.2 lib/js_dependency/replace_path_alias.rb
js_dependency-0.2.1 lib/js_dependency/replace_path_alias.rb
js_dependency-0.2.0 lib/js_dependency/replace_path_alias.rb
js_dependency-0.1.1 lib/js_dependency/replace_path_alias.rb
js_dependency-0.1.0 lib/js_dependency/replace_path_alias.rb