Class: Sitepress::Asset
- Inherits:
-
Object
- Object
- Sitepress::Asset
- 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
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#==(asset) ⇒ Object
Treat resources with the same request path as equal.
-
#basename ⇒ Object
TODO: This is really a “key” or “leafname”.
- #exists? ⇒ Boolean
-
#extensions ⇒ Object
List of all file extensions.
- #format_basename ⇒ Object
-
#format_extension ⇒ Object
Returns the format extension.
-
#format_name ⇒ Object
The base name with the format extension.
-
#initialize(path:, mime_type: nil) ⇒ Asset
constructor
A new instance of Asset.
- #mime_type ⇒ Object
-
#template_extensions ⇒ Object
Returns a list of the rendering extensions.
-
#to_request_path ⇒ Object
Spits out a reasonable default request path.
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
#path ⇒ Object (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 |
#basename ⇒ Object
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
66 67 68 |
# File 'lib/sitepress/asset.rb', line 66 def exists? File.exists? path end |
#extensions ⇒ Object
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_basename ⇒ Object
38 39 40 |
# File 'lib/sitepress/asset.rb', line 38 def format_basename [basename, format_extension].join(".") end |
#format_extension ⇒ Object
Returns the format extension.
43 44 45 |
# File 'lib/sitepress/asset.rb', line 43 def format_extension extensions.first end |
#format_name ⇒ Object
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_type ⇒ Object
62 63 64 |
# File 'lib/sitepress/asset.rb', line 62 def mime_type @mime_type ||= inferred_mime_type || DEFAULT_MIME_TYPE end |
#template_extensions ⇒ Object
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_path ⇒ Object
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 |