Class: Sitepress::Resource
- Inherits:
-
Object
- Object
- Sitepress::Resource
- 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
-
#asset ⇒ Object
readonly
Returns the value of attribute asset.
-
#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
Returns the value of attribute node.
Instance Method Summary collapse
- #==(resource) ⇒ Object
- #children(**args) ⇒ Object
-
#clone ⇒ Object
Clones should be initialized with a nil node.
-
#copy_to(destination) ⇒ Object
Creates a duplicate of the resource and moves it to the destination.
-
#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.
-
#move_to(destination) ⇒ Object
Moves the resource to a destination node.
- #parent(**args) ⇒ Object
- #parents(**args) ⇒ Object
-
#path ⇒ Object
The ‘page_url|page_path` helper in Rails uses this method to determine the URL of a resource.
-
#remove ⇒ Object
Removes the resource from the node’s resources list.
- #render_in(view_context) ⇒ Object
- #request_path ⇒ Object (also: #url)
- #siblings(**args) ⇒ Object
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
#asset ⇒ Object (readonly)
Returns the value of attribute asset.
11 12 13 |
# File 'lib/sitepress/resource.rb', line 11 def asset @asset end |
#format ⇒ Object
Returns the value of attribute format.
13 14 15 |
# File 'lib/sitepress/resource.rb', line 13 def format @format end |
#handler ⇒ Object
Returns the value of attribute handler.
13 14 15 |
# File 'lib/sitepress/resource.rb', line 13 def handler @handler end |
#mime_type ⇒ Object
Returns the value of attribute mime_type.
13 14 15 |
# File 'lib/sitepress/resource.rb', line 13 def mime_type @mime_type end |
#node ⇒ Object
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 |
#clone ⇒ Object
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.
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 |
#inspect ⇒ Object
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 |
#lineage ⇒ Object
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.
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 |
#path ⇒ Object
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 |
#remove ⇒ Object
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_path ⇒ Object 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 |