Class: Sprockets::URITar

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/uri_tar.rb

Overview

Internal: used to “expand” and “compress” values for storage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, env) ⇒ URITar

Internal: Initialize object for compression or expansion

uri - A String containing URI that may or may not contain the scheme env - The current “environment” that assets are being loaded into.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sprockets/uri_tar.rb', line 10

def initialize(uri, env)
  @root = env.root
  @env  = env
  if uri.include?("://".freeze)
    uri_array = uri.split("://".freeze)
    @scheme   = uri_array.shift
    @scheme   << "://".freeze
    @path     = uri_array.join("".freeze)
  else
    @scheme = "".freeze
    @path   = uri
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/sprockets/uri_tar.rb', line 4

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



4
5
6
# File 'lib/sprockets/uri_tar.rb', line 4

def root
  @root
end

#schemeObject (readonly)

Returns the value of attribute scheme.



4
5
6
# File 'lib/sprockets/uri_tar.rb', line 4

def scheme
  @scheme
end

Instance Method Details

#compressObject

Internal: Converts full uri to a “compressed” uri

If a uri is inside of an environment’s root it will be shortened to be a relative path.

If a uri is outside of the environment’s root the original uri will be returned.

Returns String



33
34
35
# File 'lib/sprockets/uri_tar.rb', line 33

def compress
  scheme + compressed_path
end

#compressed_pathObject

Internal: Returns “compressed” path

If the input uri is relative to the environment root it will return a path relative to the environment root. Otherwise an absolute path will be returned.

Only path information is returned, and not scheme.

Returns String



68
69
70
71
72
73
74
# File 'lib/sprockets/uri_tar.rb', line 68

def compressed_path
  if compressed_path = @env.split_subpath(root, path)
    compressed_path
  else
    path
  end
end

#expandObject

Internal: Convert a “compressed” uri to an absolute path

If a uri is inside of the environment’s root it will not start with a slash for example:

file://this/is/a/relative/path

If a uri is outside the root, it will start with a slash:

file:///This/is/an/absolute/path

Returns String



49
50
51
52
53
54
55
56
57
# File 'lib/sprockets/uri_tar.rb', line 49

def expand
  if path.start_with?("/".freeze)
    # Stored path was absolute, don't add root
    scheme + path
  else
    # Stored path was relative, add root
    scheme + File.join(root, path)
  end
end