Class: Sprockets::AssetAttributes

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

Overview

‘AssetAttributes` is a wrapper similar to `Pathname` that provides some helper accessors.

These methods should be considered internalish.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, path) ⇒ AssetAttributes

Returns a new instance of AssetAttributes.



11
12
13
14
# File 'lib/sprockets/asset_attributes.rb', line 11

def initialize(environment, path)
  @environment = environment
  @pathname = path.is_a?(Pathname) ? path : Pathname.new(path.to_s)
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



9
10
11
# File 'lib/sprockets/asset_attributes.rb', line 9

def environment
  @environment
end

#pathnameObject (readonly)

Returns the value of attribute pathname.



9
10
11
# File 'lib/sprockets/asset_attributes.rb', line 9

def pathname
  @pathname
end

Instance Method Details

#content_typeObject

Returns the content type for the pathname. Falls back to ‘application/octet-stream`.



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sprockets/asset_attributes.rb', line 103

def content_type
  @content_type ||= begin
    if format_extension.nil?
      engine_content_type || 'application/octet-stream'
    else
      @environment.mime_types(format_extension) ||
        engine_content_type ||
        'application/octet-stream'
    end
  end
end

#engine_extensionsObject

Returns an ‘Array` of engine extensions.

"foo.js.coffee.erb"
# => [".coffee", ".erb"]


80
81
82
83
84
85
86
87
88
# File 'lib/sprockets/asset_attributes.rb', line 80

def engine_extensions
  exts = extensions

  if offset = extensions.index(format_extension)
    exts = extensions[offset+1..-1]
  end

  exts.select { |ext| @environment.engines(ext) }
end

#enginesObject

Returns engine classes.



91
92
93
# File 'lib/sprockets/asset_attributes.rb', line 91

def engines
  engine_extensions.map { |ext| @environment.engines(ext) }
end

#expand_rootObject

Replaces ‘$root` placeholder with actual environment root.



17
18
19
# File 'lib/sprockets/asset_attributes.rb', line 17

def expand_root
  pathname.to_s.sub(/^\$root/, environment.root)
end

#extensionsObject

Returns ‘Array` of extension `String`s.

"foo.js.coffee"
# => [".js", ".coffee"]


60
61
62
# File 'lib/sprockets/asset_attributes.rb', line 60

def extensions
  @extensions ||= @pathname.basename.to_s.scan(/\.[^.]+/)
end

#format_extensionObject

Returns the format extension.

"foo.js.coffee"
# => ".js"


69
70
71
72
73
# File 'lib/sprockets/asset_attributes.rb', line 69

def format_extension
  extensions.detect { |ext|
    @environment.mime_types(ext) && !@environment.engines(ext)
  }
end

#logical_pathObject

Reverse guess logical path for fully expanded path.

This has some known issues. For an example if a file is shaddowed in the path, but is required relatively, its logical path will be incorrect.

Raises:



44
45
46
47
48
49
50
51
52
53
# File 'lib/sprockets/asset_attributes.rb', line 44

def logical_path
  raise ArgumentError unless pathname.absolute?

  if root_path = environment.paths.detect { |path| pathname.to_s[path] }
    path = pathname.relative_path_from(Pathname.new(root_path)).to_s
    path = engine_extensions.inject(path) { |p, ext| p.sub(ext, '') }
    path = "#{path}#{engine_format_extension}" unless format_extension
    path
  end
end

#path_fingerprintObject

Gets digest fingerprint.

"foo-0aa2105d29558f3eb790d411d7d8fb66.js"
# => "0aa2105d29558f3eb790d411d7d8fb66"


120
121
122
# File 'lib/sprockets/asset_attributes.rb', line 120

def path_fingerprint
  pathname.basename(extensions.join).to_s =~ /-([0-9a-f]{7,40})$/ ? $1 : nil
end

#path_with_fingerprint(digest) ⇒ Object

Injects digest fingerprint into path.

"foo.js"
# => "foo-0aa2105d29558f3eb790d411d7d8fb66.js"


129
130
131
132
133
134
135
136
# File 'lib/sprockets/asset_attributes.rb', line 129

def path_with_fingerprint(digest)
  if old_digest = path_fingerprint
    pathname.sub(old_digest, digest).to_s
  else
    basename = "#{pathname.basename(extensions.join)}-#{digest}#{extensions.join}"
    pathname.dirname.to_s == '.' ? basename : pathname.dirname.join(basename).to_s
  end
end

#processorsObject

Returns all processors to run on the path.



96
97
98
99
100
# File 'lib/sprockets/asset_attributes.rb', line 96

def processors
  environment.preprocessors(content_type) +
    engines.reverse +
    environment.postprocessors(content_type)
end

#relativize_rootObject

Replaces environment root with ‘$root` placeholder.



22
23
24
# File 'lib/sprockets/asset_attributes.rb', line 22

def relativize_root
  pathname.to_s.sub(/^#{Regexp.escape(environment.root)}/, '$root')
end

#search_pathsObject

Returns paths search the load path for.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sprockets/asset_attributes.rb', line 27

def search_paths
  paths = [pathname.to_s]

  if pathname.basename(extensions.join).to_s != 'index'
    path_without_extensions = extensions.inject(pathname) { |p, ext| p.sub(ext, '') }
    index_path = path_without_extensions.join("index#{extensions.join}").to_s
    paths << index_path
  end

  paths
end