Class: Propshaft::Asset

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, logical_path:, version: nil) ⇒ Asset

Returns a new instance of Asset.



7
8
9
# File 'lib/propshaft/asset.rb', line 7

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

Instance Attribute Details

#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

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#==(other_asset) ⇒ Object



39
40
41
# File 'lib/propshaft/asset.rb', line 39

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

#contentObject



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

def content
  File.binread(path)
end

#content_typeObject



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

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

#digestObject



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

def digest
  @digest ||= Digest::SHA1.hexdigest("#{content}#{version}")
end

#digested_pathObject



27
28
29
30
31
32
33
# File 'lib/propshaft/asset.rb', line 27

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

#fresh?(digest) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/propshaft/asset.rb', line 35

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

#lengthObject



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

def length
  content.size
end