Class: Propshaft::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/propshaft/asset.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, logical_path:, load_path:) ⇒ Asset

Returns a new instance of Asset.



16
17
18
# File 'lib/propshaft/asset.rb', line 16

def initialize(path, logical_path:, load_path:)
  @path, @logical_path, @load_path = path, Pathname.new(logical_path), load_path
end

Instance Attribute Details

#load_pathObject (readonly)

Returns the value of attribute load_path.



5
6
7
# File 'lib/propshaft/asset.rb', line 5

def load_path
  @load_path
end

#logical_pathObject (readonly)

Returns the value of attribute logical_path.



5
6
7
# File 'lib/propshaft/asset.rb', line 5

def logical_path
  @logical_path
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/propshaft/asset.rb', line 5

def path
  @path
end

Class Method Details

.extract_path_and_digest(digested_path) ⇒ Object



8
9
10
11
12
13
# File 'lib/propshaft/asset.rb', line 8

def extract_path_and_digest(digested_path)
  digest = digested_path[/-([0-9a-zA-Z]{7,128})\.(?!digested)([^.]|.map)+\z/, 1]
  path   = digest ? digested_path.sub("-#{digest}", "") : digested_path

  [path, digest]
end

Instance Method Details

#==(other_asset) ⇒ Object



48
49
50
# File 'lib/propshaft/asset.rb', line 48

def ==(other_asset)
  logical_path.hash == other_asset.logical_path.hash
end

#content(encoding: "ASCII-8BIT") ⇒ Object



20
21
22
# File 'lib/propshaft/asset.rb', line 20

def content(encoding: "ASCII-8BIT")
  File.read(path, encoding: encoding)
end

#content_typeObject



24
25
26
# File 'lib/propshaft/asset.rb', line 24

def content_type
  Mime::Type.lookup_by_extension(logical_path.extname.from(1))
end

#digestObject



32
33
34
# File 'lib/propshaft/asset.rb', line 32

def digest
  @digest ||= Digest::SHA1.hexdigest("#{content_with_compile_references}#{load_path.version}").first(8)
end

#digested_pathObject



36
37
38
39
40
41
42
# File 'lib/propshaft/asset.rb', line 36

def digested_path
  if already_digested?
    logical_path
  else
    logical_path.sub(/\.(\w+(\.map)?)$/) { |ext| "-#{digest}#{ext}" }
  end
end

#fresh?(digest) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/propshaft/asset.rb', line 44

def fresh?(digest)
  self.digest == digest || already_digested?
end

#lengthObject



28
29
30
# File 'lib/propshaft/asset.rb', line 28

def length
  content.size
end