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`.


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sprockets/asset_attributes.rb', line 99

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"]

76
77
78
79
80
81
82
83
84
# File 'lib/sprockets/asset_attributes.rb', line 76

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.


87
88
89
# File 'lib/sprockets/asset_attributes.rb', line 87

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

#extensionsObject

Returns `Array` of extension `String`s.

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

56
57
58
# File 'lib/sprockets/asset_attributes.rb', line 56

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

#format_extensionObject

Returns the format extension.

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

65
66
67
68
69
# File 'lib/sprockets/asset_attributes.rb', line 65

def format_extension
  extensions.reverse.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.


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sprockets/asset_attributes.rb', line 39

def logical_path
  if root_path = environment.paths.detect { |path| pathname.to_s[path] }
    path = pathname.to_s.sub("#{root_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
  else
    raise FileOutsidePaths, "#{pathname} isn't in paths: #{environment.paths.join(', ')}"
  end
end

#processorsObject

Returns all processors to run on the path.


92
93
94
95
96
# File 'lib/sprockets/asset_attributes.rb', line 92

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

#search_pathsObject

Returns paths search the load path for.


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sprockets/asset_attributes.rb', line 17

def search_paths
  paths = [pathname.to_s]

  path_without_extensions = extensions.inject(pathname) { |p, ext| p.sub(ext, '') }

  # optimization: component.json can only be nested one level deep
  if !path_without_extensions.to_s.index('/')
    paths << path_without_extensions.join("component.json").to_s
  end

  if pathname.basename(extensions.join).to_s != 'index'
    paths << path_without_extensions.join("index#{extensions.join}").to_s
  end

  paths
end