Sha256: 894799ca3b2edfaebc6af9d282fa6de0b0aa3b7c5cafb356ad8cc57fde8bfefb

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

require "digest/md5"
require "dassets/cache"
require "dassets/source_file"

module Dassets; end

class Dassets::SourceProxy
  attr_reader :digest_path, :content_cache, :fingerprint_cache
  attr_reader :source_files

  def initialize(digest_path, **options)
    @digest_path       = digest_path
    @content_cache     = options[:content_cache]     || Dassets::NoCache.new
    @fingerprint_cache = options[:fingerprint_cache] || Dassets::NoCache.new
    @source_files      =
      get_source_files(
        @digest_path,
        content_cache:     @content_cache,
        fingerprint_cache: @fingerprint_cache,
      )
  end

  def key
    "#{self.digest_path} -- #{self.mtime}"
  end

  def content
    @content_cache[self.key] ||= source_content
  end

  def fingerprint
    @fingerprint_cache[self.key] ||= source_fingerprint
  end

  def mtime
    @source_files.map{ |f| f.mtime }.compact.max
  end

  def response_headers
    @source_files.inject(Hash.new){ |hash, f| hash.merge!(f.response_headers) }
  end

  def exists?
    @source_files.inject(true){ |res, f| res && f.exists? }
  end

  private

  def source_content
    @source_files.map{ |f| f.compiled }.join("\n")
  end

  def source_fingerprint
    Digest::MD5.new.hexdigest(source_content)
  end

  def get_source_files(digest_path, **options)
    Dassets.config.combinations[digest_path.to_s].map { |source_digest_path|
      Dassets::SourceFile.find_by_digest_path(source_digest_path, **options)
    }
  end
end

class Dassets::NullSourceProxy
  def content
    nil
  end

  def exists?
    false
  end

  def mtime
    nil
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dassets-0.15.0 lib/dassets/source_proxy.rb