class Sitepress::Resource

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.

Constants

DEFAULT_FILTER_SCOPE

Default scope for querying parent/child/sibling resources.

Attributes

asset[R]
body[W]
data[W]
format[RW]
handler[RW]
mime_type[RW]
node[R]

Public Class Methods

new(asset:, node:, format: nil, mime_type: nil, handler: nil) click to toggle source
# 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

Public Instance Methods

==(resource) click to toggle source
# File lib/sitepress/resource.rb, line 56
def ==(resource)
  resource.request_path == request_path
end
body() click to toggle source
# File lib/sitepress/resource.rb, line 32
def body
  @body ||= asset.body
end
children(**args) click to toggle source
# File lib/sitepress/resource.rb, line 52
def children(**args)
  filter_resources(**args){ node.children }.compact
end
data() click to toggle source
# File lib/sitepress/resource.rb, line 28
def data
  @data ||= asset.data
end
inspect() click to toggle source
# 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() click to toggle source

Used internally to construct paths from the current node up to the root node.

# File lib/sitepress/resource.rb, line 61
def lineage
  @lineage ||= node.parents.reject(&:root?).reverse.map(&:name)
end
parent(**args) click to toggle source
# File lib/sitepress/resource.rb, line 40
def parent(**args)
  parents(**args).first
end
parents(**args) click to toggle source
# File lib/sitepress/resource.rb, line 44
def parents(**args)
  filter_resources(**args){ node.parents }
end
renderable?() click to toggle source

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.

# File lib/sitepress/resource.rb, line 68
def renderable?
  asset.renderable?
end
request_path() click to toggle source
# File lib/sitepress/resource.rb, line 24
def request_path
  File.join("/", *lineage, request_filename)
end
siblings(**args) click to toggle source
# File lib/sitepress/resource.rb, line 48
def siblings(**args)
  filter_resources(**args){ node.siblings }.compact
end

Private Instance Methods

filter_resources(type: DEFAULT_FILTER_SCOPE, &block) click to toggle source

Filters parent/child/sibling resources by a type. The default behavior is to only return resources of the same type. For example given the pages `/a.html`, `/a.gif`, `/a/b.html`, if you query the parent from page `/a/b.html` you'd only get `/a.html` by default. If you query the parents via `parents(type: :all)` you'd get get [`/a.html`, `/a.gif`]

TODO: When `type: :all` is scoped, some queries will mistakenly return single resources. :all should return an array of arrays to accurately represention levels.

TODO: Put a better extension/mime_type handler into resource tree, then instead of faltening below and select, we could call a single map and pull out a resources

# File lib/sitepress/resource.rb, line 83
def filter_resources(type: DEFAULT_FILTER_SCOPE, &block)
  return [] unless node
  nodes = block.call

  case type
  when :all
    nodes.map{ |node| node.formats }
  when :same
    nodes.map{ |n| n.formats.get(format) }.flatten
  when String, Symbol, NilClass
    nodes.map{ |n| n.formats.get(type) }.flatten
  when MIME::Type
    nodes.map{ |n| n.formats.mime_type(type) }.flatten
  else
    raise ArgumentError, "Invalid type argument #{type}. Must be either :same, :all, an extension string, or a Mime::Type"
  end
end
request_filename() click to toggle source

Deals with situations, particularly in the root node and other “index” nodes, for the `request_path`

# File lib/sitepress/resource.rb, line 102
def request_filename
  if node.root? and node.default_format == format
    ""
  elsif node.root? and format
    "#{node.default_name}.#{format}"
  elsif node.root?
    node.default_name
  elsif format.nil? or node.default_format == format
    node.name
  else
    "#{node.name}.#{format}"
  end
end