class Sitepress::Formats

Manages collections of resources that share the same Node. Given the files `/a.html` and `/a.gif`, both of these assets would be stored in the `Node#name = “a”` under `Node#formats` with the extensions `.gif`, and `.html`.

Public Class Methods

new(node: ) click to toggle source
# File lib/sitepress/formats.rb, line 14
def initialize(node: )
  @node = node
  @formats = Hash.new
end

Public Instance Methods

add(asset:, format: nil) click to toggle source
# File lib/sitepress/formats.rb, line 39
def add(asset:, format: nil)
  format = symbolize(format || default_format)

  resource = Resource.new(asset: asset, node: @node, format: format)
  if @formats.has_key? format
    raise Sitepress::ExistingRequestPathError, "Resource at #{resource.request_path} already set with format #{format.inspect}"
  else
    @formats[format] = resource
  end
end
each(&block) click to toggle source
# File lib/sitepress/formats.rb, line 19
def each(&block)
  @formats.values.each(&block)
end
extensions() click to toggle source
# File lib/sitepress/formats.rb, line 31
def extensions
  @formats.keys
end
get(extension) click to toggle source
# File lib/sitepress/formats.rb, line 27
def get(extension)
  @formats[symbolize(extension || default_format)]
end
inspect() click to toggle source
# File lib/sitepress/formats.rb, line 50
def inspect
  "<#{self.class}: resources=#{map(&:request_path)}>"
end
mime_type(mime_type) click to toggle source
# File lib/sitepress/formats.rb, line 35
def mime_type(mime_type)
  find { |f| f.mime_type == mime_type }
end
remove(extension) click to toggle source
# File lib/sitepress/formats.rb, line 23
def remove(extension)
  @formats.delete symbolize(extension)
end

Private Instance Methods

symbolize(format) click to toggle source
# File lib/sitepress/formats.rb, line 55
def symbolize(format)
  format&.to_sym
end