class Frails::Manifest
Attributes
manifest_path[R]
Public Class Methods
new(path)
click to toggle source
# 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
Public Instance Methods
lookup(name, type: nil)
click to toggle source
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"
# 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)
click to toggle source
Like lookup, except that if no asset is found, raises a Frails::Manifest::MissingEntryError
.
# File lib/frails/manifest.rb, line 44 def lookup!(name, type: nil) lookup(name, type: type) || handle_missing_entry(name) end
read(name, type) { |path, read_source(path)| ... }
click to toggle source
# 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) { |path, read_source(path)| ... }
click to toggle source
# 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
refresh()
click to toggle source
# File lib/frails/manifest.rb, line 19 def refresh @data = load end
Private Instance Methods
data()
click to toggle source
# File lib/frails/manifest.rb, line 78 def data if Rails.env.production? @data ||= load else refresh end end
find(name)
click to toggle source
# File lib/frails/manifest.rb, line 90 def find(name) data[name.to_s].presence end
full_pack_name(name, type)
click to toggle source
# File lib/frails/manifest.rb, line 94 def full_pack_name(name, type) return name unless File.extname(name.to_s).empty? "#{name}.#{manifest_type(type)}" end
handle_missing_entry(name)
click to toggle source
# File lib/frails/manifest.rb, line 86 def handle_missing_entry(name) raise Frails::Manifest::MissingEntryError, missing_file_from_manifest_error(name) end
load()
click to toggle source
# File lib/frails/manifest.rb, line 74 def load manifest_path.exist? ? JSON.parse(manifest_path.read) : {} end
manifest_name(name, type)
click to toggle source
The `manifest_name` method strips of the file extension of the name, because in the manifest hash the entrypoints are defined by their pack name without the extension. When the user provides a name with a file extension, we want to try to strip it off.
# File lib/frails/manifest.rb, line 103 def manifest_name(name, type) return name if File.extname(name.to_s).empty? File.basename(name, type) end
manifest_type(type)
click to toggle source
# File lib/frails/manifest.rb, line 109 def manifest_type(type) case type when :javascript then 'js' when :stylesheet then 'css' else type.to_s end end
missing_file_from_manifest_error(bundle_name)
click to toggle source
# File lib/frails/manifest.rb, line 117 def missing_file_from_manifest_error(bundle_name) <<~MSG Frails can't find #{bundle_name} in #{manifest_path}. Your manifest contains: #{JSON.pretty_generate(@data)} MSG end
read_source(path)
click to toggle source
# File lib/frails/manifest.rb, line 64 def read_source(path) return Rails.public_path.join(path.gsub(%r{^\/}, '')).read unless Frails.dev_server.running? begin URI.open("http://#{Frails.dev_server.host_with_port}#{path}").read rescue OpenURI::HTTPError handle_missing_entry path end end