Class: Bridgetown::ImagePipeline::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/bridgetown/image_pipeline/manifest.rb

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir:) ⇒ Manifest

Returns a new instance of Manifest.



9
10
11
12
13
14
# File 'lib/bridgetown/image_pipeline/manifest.rb', line 9

def initialize(cache_dir:)
  @cache_dir = cache_dir
  FileUtils.mkdir_p(@cache_dir)
  @entries = {}        # source_path => entry hash (symbolized)
  @by_src  = {}        # "/images/foo.jpg" => entry hash
end

Instance Method Details

#allObject



48
49
50
# File 'lib/bridgetown/image_pipeline/manifest.rb', line 48

def all
  @entries.dup
end

#fetch_cached(cache_key) ⇒ Object



23
24
25
26
27
28
# File 'lib/bridgetown/image_pipeline/manifest.rb', line 23

def fetch_cached(cache_key)
  path = cache_file(cache_key)
  return nil unless File.exist?(path)

  deep_symbolize(JSON.parse(File.read(path)))
end

#find_by_src(src) ⇒ Object



35
36
37
# File 'lib/bridgetown/image_pipeline/manifest.rb', line 35

def find_by_src(src)
  @by_src[src]
end

#put(source_path, entry, cache_key:) ⇒ Object



16
17
18
19
20
21
# File 'lib/bridgetown/image_pipeline/manifest.rb', line 16

def put(source_path, entry, cache_key:)
  symbolized = deep_symbolize(entry)
  @entries[source_path] = symbolized
  @by_src[public_src_for(source_path)] = symbolized
  File.write(cache_file(cache_key), JSON.generate(symbolized))
end

#register_cached(source_path, entry) ⇒ Object



30
31
32
33
# File 'lib/bridgetown/image_pipeline/manifest.rb', line 30

def register_cached(source_path, entry)
  @entries[source_path] = entry
  @by_src[public_src_for(source_path)] = entry
end

#variants_by_width(src) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/bridgetown/image_pipeline/manifest.rb', line 39

def variants_by_width(src)
  entry = @by_src[src]
  return {} unless entry

  entry[:variants].each_with_object({}) do |v, out|
    (out[v[:width]] ||= {})[v[:format]] = v[:path]
  end
end