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

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

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



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

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

#children(**args) ⇒ Object



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

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.



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

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:



64
65
66
67
68
69
# File 'lib/sitepress/resource.rb', line 64

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



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

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.



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

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:



56
57
58
59
60
61
# File 'lib/sitepress/resource.rb', line 56

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



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

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

#parents(**args) ⇒ Object



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

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

#pathObject

The ‘page_url|page_path` helper in Rails uses this method to determine the URL of a resource.



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

def path
  File.join(*lineage, request_filename)
end

#removeObject

Removes the resource from the node’s resources list.



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

def remove
  node.resources.remove format if node
end

#render_in(view_context) ⇒ Object



111
112
113
# File 'lib/sitepress/resource.rb', line 111

def render_in(view_context)
  view_context.render inline: body, type: handler
end

#request_pathObject Also known as: url



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

def request_path
  File.join("/", path)
end

#siblings(**args) ⇒ Object



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

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