Class: Sitepress::Resource
- Inherits:
-
Object
- Object
- Sitepress::Resource
- Defined in:
- lib/sitepress/resource.rb
Overview
Represents the request path of an asset. There may be multiple resources that point to the same asset. Resources are immutable and may be altered by the resource proxy.
Constant Summary collapse
- DEFAULT_FILTER_SCOPE =
Default scope for querying parent/child/sibling resources.
:same
Instance Attribute Summary collapse
-
#asset ⇒ Object
readonly
Returns the value of attribute asset.
- #body ⇒ Object
- #data ⇒ Object
-
#format ⇒ Object
Returns the value of attribute format.
-
#handler ⇒ Object
Returns the value of attribute handler.
-
#mime_type ⇒ Object
Returns the value of attribute mime_type.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
Instance Method Summary collapse
- #==(resource) ⇒ Object
- #children(**args) ⇒ Object
-
#initialize(asset:, node:, format: nil, mime_type: nil, handler: nil) ⇒ Resource
constructor
A new instance of Resource.
- #inspect ⇒ Object
-
#lineage ⇒ Object
Used internally to construct paths from the current node up to the root node.
- #parent(**args) ⇒ Object
- #parents(**args) ⇒ Object
-
#renderable? ⇒ Boolean
Certain files, like binary file types, aren’t something that we should try to parse.
- #request_path ⇒ Object
- #siblings(**args) ⇒ Object
Constructor Details
#initialize(asset:, node:, format: nil, mime_type: nil, handler: nil) ⇒ Resource
Returns a new instance of Resource.
16 17 18 19 20 21 22 |
# File 'lib/sitepress/resource.rb', line 16 def initialize(asset:, node:, format: nil, mime_type: nil, handler: nil) @asset = asset @node = node @format = format || asset.format @mime_type = mime_type || asset.mime_type @handler = handler || asset.handler end |
Instance Attribute Details
#asset ⇒ Object (readonly)
Returns the value of attribute asset.
9 10 11 |
# File 'lib/sitepress/resource.rb', line 9 def asset @asset end |
#body ⇒ Object
32 33 34 |
# File 'lib/sitepress/resource.rb', line 32 def body @body ||= asset.body end |
#data ⇒ Object
28 29 30 |
# File 'lib/sitepress/resource.rb', line 28 def data @data ||= asset.data end |
#format ⇒ Object
Returns the value of attribute format.
11 12 13 |
# File 'lib/sitepress/resource.rb', line 11 def format @format end |
#handler ⇒ Object
Returns the value of attribute handler.
11 12 13 |
# File 'lib/sitepress/resource.rb', line 11 def handler @handler end |
#mime_type ⇒ Object
Returns the value of attribute mime_type.
11 12 13 |
# File 'lib/sitepress/resource.rb', line 11 def mime_type @mime_type end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
9 10 11 |
# File 'lib/sitepress/resource.rb', line 9 def node @node end |
Instance Method Details
#==(resource) ⇒ Object
56 57 58 |
# File 'lib/sitepress/resource.rb', line 56 def ==(resource) resource.request_path == request_path end |
#children(**args) ⇒ Object
52 53 54 |
# File 'lib/sitepress/resource.rb', line 52 def children(**args) filter_resources(**args){ node.children }.compact end |
#inspect ⇒ Object
36 37 38 |
# File 'lib/sitepress/resource.rb', line 36 def inspect "<#{self.class}:#{object_id} request_path=#{request_path.inspect} asset_path=#{asset.path.to_s.inspect}>" end |
#lineage ⇒ Object
Used internally to construct paths from the current node up to the root node.
61 62 63 |
# File 'lib/sitepress/resource.rb', line 61 def lineage @lineage ||= node.parents.reject(&:root?).reverse.map(&:name) end |
#parent(**args) ⇒ Object
40 41 42 |
# File 'lib/sitepress/resource.rb', line 40 def parent(**args) parents(**args).first end |
#parents(**args) ⇒ Object
44 45 46 |
# File 'lib/sitepress/resource.rb', line 44 def parents(**args) filter_resources(**args){ node.parents } 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.
68 69 70 |
# File 'lib/sitepress/resource.rb', line 68 def renderable? asset.renderable? end |
#request_path ⇒ Object
24 25 26 |
# File 'lib/sitepress/resource.rb', line 24 def request_path File.join("/", *lineage, request_filename) end |
#siblings(**args) ⇒ Object
48 49 50 |
# File 'lib/sitepress/resource.rb', line 48 def siblings(**args) filter_resources(**args){ node.siblings }.compact end |