Sha256: de51c763e0887e1d222a3d8ca09666972aaa7be62b0b25a7f2e5fbc708f4b7fb

Contents?: true

Size: 888 Bytes

Versions: 1

Compression:

Stored size: 888 Bytes

Contents

require "digest/sha1"
require "action_dispatch/http/mime_type"

class Propshaft::Asset
  attr_reader :path, :logical_path

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

  def content
    File.binread(path)
  end

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

  def length
    content.size
  end

  def digest
    @digest ||= Digest::SHA1.hexdigest(content)
  end

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

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

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

  private
    def already_digested?
      logical_path.to_s =~ /-([0-9a-zA-Z]{7,128})\.digested/
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
propshaft-0.5.0 lib/propshaft/asset.rb