Class: Sitepress::Resource

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

Class Attribute Summary collapse

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.



18
19
20
21
22
23
24
# File 'lib/sitepress/resource.rb', line 18

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

Class Attribute Details

.path_suffix_hack_that_you_should_not_useObject

Returns the value of attribute path_suffix_hack_that_you_should_not_use.



107
108
109
# File 'lib/sitepress/resource.rb', line 107

def path_suffix_hack_that_you_should_not_use
  @path_suffix_hack_that_you_should_not_use
end

Instance Attribute Details

#assetObject (readonly)

Returns the value of attribute asset.



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

def asset
  @asset
end

#formatObject

Returns the value of attribute format.



13
14
15
# File 'lib/sitepress/resource.rb', line 13

def format
  @format
end

#handlerObject

Returns the value of attribute handler.



13
14
15
# File 'lib/sitepress/resource.rb', line 13

def handler
  @handler
end

#mime_typeObject

Returns the value of attribute mime_type.



13
14
15
# File 'lib/sitepress/resource.rb', line 13

def mime_type
  @mime_type
end

#nodeObject

Returns the value of attribute node.



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

def node
  @node
end

Instance Method Details

#==(resource) ⇒ Object



97
98
99
# File 'lib/sitepress/resource.rb', line 97

def ==(resource)
  resource.request_path == request_path
end

#children(**args) ⇒ Object



93
94
95
# File 'lib/sitepress/resource.rb', line 93

def children(**args)
  filter_resources(**args){ node.children }.compact
end

#cloneObject

Clones should be initialized with a nil node. Initializing with a node would mean that multiple resources are pointing to the same node, which shouldn’t be possible.



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

def clone
  self.class.new(asset: @asset, node: nil, format: @format, mime_type: @mime_type, handler: @handler)
end

#copy_to(destination) ⇒ Object

Creates a duplicate of the resource and moves it to the destination.

Raises:



59
60
61
62
63
64
# File 'lib/sitepress/resource.rb', line 59

def copy_to(destination)
  raise Sitepress::Error, "#{destination.inspect} is not a Sitepress::Node" unless destination.is_a? Sitepress::Node
  self.clone.tap do |resource|
    resource.node = destination.child(node.name)
  end
end

#inspectObject



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

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.



102
103
104
# File 'lib/sitepress/resource.rb', line 102

def lineage
  node.parents.reject(&:root?).reverse.map(&:name)
end

#move_to(destination) ⇒ Object

Moves the resource to a destination node. Moving a resource to a Sitepress::Node is a little weird for people who are accustomed to working with files, which is pretty much everybody (including myself). A child node has to be created on the destination node with the name of the resource node.

Or just ignore all of that and use the ‘move_to` method so you can feel like you’re moving files around.

Raises:



51
52
53
54
55
56
# File 'lib/sitepress/resource.rb', line 51

def move_to(destination)
  raise Sitepress::Error, "#{destination.inspect} is not a Sitepress::Node" unless destination.is_a? Sitepress::Node
  self.tap do |resource|
    resource.node = destination.child(node.name)
  end
end

#parent(**args) ⇒ Object



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

def parent(**args)
  parents(**args).first
end

#parents(**args) ⇒ Object



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

def parents(**args)
  filter_resources(**args){ node.parents }
end

#removeObject

Removes the resource from the node’s resources list.



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

def remove
  node.resources.remove format if node
end

#request_pathObject Also known as: url



26
27
28
# File 'lib/sitepress/resource.rb', line 26

def request_path
  File.join("/", *lineage, request_filename)
end

#siblings(**args) ⇒ Object



89
90
91
# File 'lib/sitepress/resource.rb', line 89

def siblings(**args)
  filter_resources(**args){ node.siblings }.compact
end