Class: DerivedImages::ManifestEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/derived_images/manifest_entry.rb

Overview

A ManifestEntry describes how to create one derived image.

Constant Summary collapse

PROCESSORS =
{ mini_magick: ImageProcessing::MiniMagick, vips: ImageProcessing::Vips }.freeze
COMPRESSION_OVERRIDES =
{ 'avif' => 'av1', 'heic' => 'hevc' }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, target) ⇒ ManifestEntry

Returns a new instance of ManifestEntry.



8
9
10
11
12
# File 'lib/derived_images/manifest_entry.rb', line 8

def initialize(source, target)
  @source = source
  @target = target
  @pipeline = default_pipeline(File.extname(target).delete_prefix('.'))
end

Instance Attribute Details

#pipelineObject

Returns the value of attribute pipeline.



6
7
8
# File 'lib/derived_images/manifest_entry.rb', line 6

def pipeline
  @pipeline
end

#sourceObject

Returns the value of attribute source.



6
7
8
# File 'lib/derived_images/manifest_entry.rb', line 6

def source
  @source
end

#targetObject

Returns the value of attribute target.



6
7
8
# File 'lib/derived_images/manifest_entry.rb', line 6

def target
  @target
end

Class Method Details

.empty_pipelineObject



42
43
44
# File 'lib/derived_images/manifest_entry.rb', line 42

def self.empty_pipeline
  PROCESSORS.fetch(DerivedImages.config.processor).dup
end

Instance Method Details

#==(other) ⇒ Object



14
15
16
# File 'lib/derived_images/manifest_entry.rb', line 14

def ==(other)
  source == other.source && target == other.target && options_hash == other.options_hash
end

#cache_keyString?

Returns a cache key for the result of the image transformation. It will vary if the source file’s content varies or if the operations applied to generate the target vary.

Returns:

  • (String, nil)

    A 64 character hexdigest, or nil if the source file can’t be found



34
35
36
37
38
39
40
# File 'lib/derived_images/manifest_entry.rb', line 34

def cache_key
  return nil unless source_present?

  Digest::SHA256.hexdigest({ version: DerivedImages::VERSION,
                             source: source_digest,
                             pipeline: options_hash }.to_json)
end

#source_pathObject



18
19
20
21
22
23
24
# File 'lib/derived_images/manifest_entry.rb', line 18

def source_path
  DerivedImages.config.image_paths.each do |path|
    path = Pathname.new(path).join(source).expand_path
    return path if path.file?
  end
  nil
end

#source_present?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/derived_images/manifest_entry.rb', line 53

def source_present?
  source_path&.file?
end

#target_digestObject



49
50
51
# File 'lib/derived_images/manifest_entry.rb', line 49

def target_digest
  Digest::SHA256.file(target_path).hexdigest
end

#target_pathObject



26
27
28
# File 'lib/derived_images/manifest_entry.rb', line 26

def target_path
  Pathname.new(DerivedImages.config.build_path).join(target).expand_path
end