class Sitepress::Asset
Represents a file on a web server that may be parsed to extract metadata or be renderable via a template. Multiple resources may point to the same asset. Properties of an asset should be mutable. The Resource
object is immutable and may be modified by the Resources proxy.
Constants
- DEFAULT_MIME_TYPE
If we can't resolve a mime type for the resource, we'll fall back to this binary octet-stream type so the client can download the resource and figure out what to do with it.
Attributes
Public Class Methods
# File lib/sitepress/asset.rb, line 22 def initialize(path:, mime_type: nil, parser: Parsers::Frontmatter) # The MIME::Types gem returns an array when types are looked up. # This grabs the first one, which is likely the intent on these lookups. @mime_type = Array(mime_type).first @path = Path.new path @parser_klass = parser end
Public Instance Methods
Treat resources with the same request path as equal.
# File lib/sitepress/asset.rb, line 31 def ==(asset) path == asset.path end
# File lib/sitepress/asset.rb, line 35 def mime_type @mime_type ||= inferred_mime_type || DEFAULT_MIME_TYPE end
Set the parser equal to a thing.
# File lib/sitepress/asset.rb, line 46 def parser=(parser_klass) @parser = nil @parser_klass = parser_klass end
Used by the Rails controller to short circuit additional processing if the asset is not renderable (e.g. is it erb or haml?)
# File lib/sitepress/asset.rb, line 41 def renderable? !!handler end
Private Instance Methods
Returns the mime type of the file extension. If a type can't be resolved then we'll just grab the first type.
# File lib/sitepress/asset.rb, line 58 def inferred_mime_type format_extension = path.format&.to_s MIME::Types.type_for(format_extension).first if format_extension end
# File lib/sitepress/asset.rb, line 52 def parser @parser ||= @parser_klass.new File.read path end