Sha256: d4b075abeb7f1d08040899a242d88fb45bf3e2dd499a014853d9d175502d8f03

Contents?: true

Size: 1.17 KB

Versions: 6

Compression:

Stored size: 1.17 KB

Contents

# frozen-string-literal: true

#
class Roda
  module RodaPlugins
    # The strip_path_prefix plugin makes Roda strip a given prefix off internal absolute paths,
    # turning them to relative paths.  Roda by default stores internal paths as absolute paths.
    # The main reason to use this plugin is when the internal absolute path could change at
    # runtime, either due to a symlink change or chroot call, or you really want to use
    # relative links instead of absolute links.
    # 
    # Examples:
    #
    #   plugin :strip_path_prefix # Defaults to Dir.pwd
    #   plugin :strip_path_prefix, File.dirname(Dir.pwd)
    module StripPathPrefix
      # Set the regexp to use when stripping prefixes from internal paths.
      def self.configure(app, prefix=Dir.pwd)
        prefix += '/' unless prefix=~ /\/\z/
        app.opts[:strip_path_prefix] = /\A#{Regexp.escape(prefix)}/
      end

      module ClassMethods
        # Strip the path prefix from the gien path if it starts with the prefix.
        def expand_path(path, root=opts[:root])
          super.sub(opts[:strip_path_prefix], '')
        end
      end
    end

    register_plugin(:strip_path_prefix, StripPathPrefix)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
roda-2.29.0 lib/roda/plugins/strip_path_prefix.rb
roda-2.28.0 lib/roda/plugins/strip_path_prefix.rb
roda-2.27.0 lib/roda/plugins/strip_path_prefix.rb
roda-2.26.0 lib/roda/plugins/strip_path_prefix.rb
roda-2.25.0 lib/roda/plugins/strip_path_prefix.rb
roda-2.24.0 lib/roda/plugins/strip_path_prefix.rb