Class: Sitepress::Resource

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#assetObject (readonly)

Returns the value of attribute asset.



9
10
11
# File 'lib/sitepress/resource.rb', line 9

def asset
  @asset
end

#bodyObject



32
33
34
# File 'lib/sitepress/resource.rb', line 32

def body
  @body ||= asset.body
end

#dataObject



28
29
30
# File 'lib/sitepress/resource.rb', line 28

def data
  @data ||= asset.data
end

#formatObject

Returns the value of attribute format.



11
12
13
# File 'lib/sitepress/resource.rb', line 11

def format
  @format
end

#handlerObject

Returns the value of attribute handler.



11
12
13
# File 'lib/sitepress/resource.rb', line 11

def handler
  @handler
end

#mime_typeObject

Returns the value of attribute mime_type.



11
12
13
# File 'lib/sitepress/resource.rb', line 11

def mime_type
  @mime_type
end

#nodeObject (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

#inspectObject



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

#lineageObject

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.

Returns:

  • (Boolean)


68
69
70
# File 'lib/sitepress/resource.rb', line 68

def renderable?
  asset.renderable?
end

#request_pathObject



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