Class: Sitepress::Asset

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sitepress/asset.rb

Overview

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.

Constant Summary collapse

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.

MIME::Types["application/octet-stream"].first
DEFAULT_PARSER =

Parsers can be swapped out to deal with different types of resources, like Notion documents, JSON, exif data on images, etc.

Parsers::Frontmatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, mime_type: nil, parser: DEFAULT_PARSER) ⇒ Asset

Returns a new instance of Asset.



25
26
27
28
29
30
31
# File 'lib/sitepress/asset.rb', line 25

def initialize(path:, mime_type: nil, parser: DEFAULT_PARSER)
  # 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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



19
20
21
# File 'lib/sitepress/asset.rb', line 19

def path
  @path
end

Instance Method Details

#==(asset) ⇒ Object

Treat resources with the same request path as equal.



34
35
36
# File 'lib/sitepress/asset.rb', line 34

def ==(asset)
  path == asset.path
end

#mime_typeObject



38
39
40
# File 'lib/sitepress/asset.rb', line 38

def mime_type
  @mime_type ||= inferred_mime_type || DEFAULT_MIME_TYPE
end

#parser=(parser_klass) ⇒ Object

Set the parser equal to a thing.



49
50
51
52
# File 'lib/sitepress/asset.rb', line 49

def parser=(parser_klass)
  @parser = nil
  @parser_klass = parser_klass
end

#renderable?Boolean

Used by the Rails controller to short circuit additional processing if the asset is not renderable (e.g. is it erb or haml?)

Returns:

  • (Boolean)


44
45
46
# File 'lib/sitepress/asset.rb', line 44

def renderable?
  !!handler
end