Class: Frails::Manifest

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

Defined Under Namespace

Classes: MissingEntryError, MissingManifestError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Manifest

Returns a new instance of Manifest.



11
12
13
14
15
16
17
# File 'lib/frails/manifest.rb', line 11

def initialize(path)
  @manifest_path = Rails.public_path.join(Frails.public_output_path, path)

  return if @manifest_path.exist?

  raise Frails::Manifest::MissingManifestError, "Frails can't find manifest #{manifest_path}"
end

Instance Attribute Details

#manifest_pathObject (readonly)

Returns the value of attribute manifest_path.



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

def manifest_path
  @manifest_path
end

Instance Method Details

#lookup(name, type: nil) ⇒ Object

Computes the relative path for a given Frails asset using manifest.json. If no asset is found, returns nil.

Example:

Frails.manifest.lookup('calendar.js') # => "/assets/calendar-1016838bab065ae1e122.js"


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/frails/manifest.rb', line 28

def lookup(name, type: nil)
  # When using SplitChunks or RuntimeChunks the manifest hash will contain an extra object called
  # "entrypoints". When the entrypoints key is not present in the manifest, or the name is not
  # found in the entrypoints hash, it will raise a NoMethodError. If this happens, we should try
  # to lookup a single instance of the pack based on the given name.
  manifest_pack_type = manifest_type(type)
  manifest_pack_name = manifest_name(name, manifest_pack_type)

  # Lookup the pack in the entrypoints of the manifest
  find('entrypoints')[manifest_pack_name][manifest_pack_type]
rescue NoMethodError
  # Lookup a single instance of the pack.
  find full_pack_name(name, type)
end

#lookup!(name, type: nil) ⇒ Object

Like lookup, except that if no asset is found, raises a Frails::Manifest::MissingEntryError.



44
45
46
# File 'lib/frails/manifest.rb', line 44

def lookup!(name, type: nil)
  lookup(name, type: type) || handle_missing_entry(name)
end

#read(name, type) ⇒ Object



55
56
57
58
59
60
# File 'lib/frails/manifest.rb', line 55

def read(name, type)
  sources = *lookup(name, type: type)
  sources.map do |path|
    yield path, read_source(path)
  end
end

#read!(name, type) ⇒ Object



48
49
50
51
52
53
# File 'lib/frails/manifest.rb', line 48

def read!(name, type)
  sources = *lookup!(name, type: type)
  sources.map do |path|
    yield path, read_source(path)
  end
end

#refreshObject



19
20
21
# File 'lib/frails/manifest.rb', line 19

def refresh
  @data = load
end