Class: DerivedImages::Manifest

Inherits:
Object
  • Object
show all
Includes:
Dsl
Defined in:
lib/derived_images/manifest.rb

Overview

The Manifest creates and holds a list of ManifestEntry instances, describing every derived image to create.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dsl

#derive, #resize

Constructor Details

#initialize(path = Rails.root.join(DerivedImages.config.manifest_path)) ⇒ Manifest

Returns a new instance of Manifest.



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

def initialize(path = Rails.root.join(DerivedImages.config.manifest_path))
  @path = path
  @entry_map = {}
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



38
39
40
# File 'lib/derived_images/manifest.rb', line 38

def path
  @path
end

Instance Method Details

#add_entry(entry) ⇒ Object



22
23
24
# File 'lib/derived_images/manifest.rb', line 22

def add_entry(entry)
  entry_map[entry.target] = entry
end

#diff_from(former_manifest) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/derived_images/manifest.rb', line 40

def diff_from(former_manifest)
  changed = []
  removed = []
  former_manifest.each do |target, entry|
    if key?(target)
      changed << entry if entry_map[target] != entry
    else
      removed << entry
    end
  end
  added = entry_map.filter_map { |target, entry| former_manifest.key?(target) ? nil : entry }
  [added, changed, removed]
end

#draw(&block) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/derived_images/manifest.rb', line 13

def draw(&block)
  @entry_map.clear
  if block
    instance_eval(&block)
  else
    instance_eval(File.read(path), path.to_s)
  end
end

#produced_from(source_path) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/derived_images/manifest.rb', line 28

def produced_from(source_path)
  source_names = []
  DerivedImages.config.image_paths.each do |path|
    dir = Pathname.new(path).expand_path.realpath
    contains_source_file = source_path.ascend.any? { _1 == dir }
    source_names << source_path.relative_path_from(dir).to_s if contains_source_file
  end
  entry_map.filter_map { |_target, entry| source_names.include?(entry.source) ? entry : nil }
end