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 frontmatter 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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Asset.



21
22
23
24
25
26
# File 'lib/sitepress/asset.rb', line 21

def initialize(path: , mime_type: nil)
  # 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 = Pathname.new path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/sitepress/asset.rb', line 16

def path
  @path
end

Instance Method Details

#==(asset) ⇒ Object

Treat resources with the same request path as equal.



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

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

#basenameObject

TODO: This is really a “key” or “leafname”.



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

def basename
  path.basename.to_s.split(".").first
end

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  File.exists? path
end

#extensionsObject

List of all file extensions.



29
30
31
# File 'lib/sitepress/asset.rb', line 29

def extensions
  path.basename.to_s.split(".").drop(1)
end

#format_basenameObject



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

def format_basename
  [basename, format_extension].join(".")
end

#format_extensionObject

Returns the format extension.



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

def format_extension
  extensions.first
end

#format_nameObject

The base name with the format extension.



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

def format_name
  [basename, format_extension].join(".")
end

#mime_typeObject



62
63
64
# File 'lib/sitepress/asset.rb', line 62

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

#template_extensionsObject

Returns a list of the rendering extensions.



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

def template_extensions
  extensions.drop(1)
end

#to_request_pathObject

Spits out a reasonable default request path. This may be changed via Resource#request_path.



72
73
74
75
76
77
78
# File 'lib/sitepress/asset.rb', line 72

def to_request_path
  if ext = format_extension
    path.dirname.join(basename).sub_ext(".#{ext}").to_s
  else
    path.to_s
  end
end