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.



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

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

#bodyObject



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

def body
  @body ||= exists? ? parse_error { parser.body } : nil
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#==(asset) ⇒ Object

Treat resources with the same request path as equal.



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

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

#created_atObject



73
74
75
# File 'lib/sitepress/asset.rb', line 73

def created_at
  File.ctime path
end

#dataObject



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

def data
  @data ||= Data.manage(exists? ? parse_error { parser.data } : {})
end

#data=(data) ⇒ Object



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

def data=(data)
  @data = Data.manage(data)
end

#destroyObject



77
78
79
# File 'lib/sitepress/asset.rb', line 77

def destroy
  FileUtils.rm path
end

#mime_typeObject



52
53
54
# File 'lib/sitepress/asset.rb', line 52

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

#parser=(parser_klass) ⇒ Object

Mmm.… that’s the smell of cache busting, which means the hiearchy of this is wrong.



64
65
66
67
# File 'lib/sitepress/asset.rb', line 64

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

#renderable?Boolean

Certain files, like binary file types, aren’t something that we should try to parse. When this returns true in some cases, a reference to the file will be passed and skip all the overhead of trying to parse and render.

Returns:

  • (Boolean)


59
60
61
# File 'lib/sitepress/asset.rb', line 59

def renderable?
  !!handler
end

#rendererObject



85
86
87
# File 'lib/sitepress/asset.rb', line 85

def renderer
  @parser_klass::Renderer.new(data: data, body: body)
end

#saveObject



81
82
83
# File 'lib/sitepress/asset.rb', line 81

def save
  File.write path, render
end

#updated_atObject



69
70
71
# File 'lib/sitepress/asset.rb', line 69

def updated_at
  File.mtime path
end