Class: Sitepress::Formats

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/sitepress/formats.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(node:) ⇒ Formats

Returns a new instance of Formats.



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

def initialize(node: )
  @node = node
  @formats = Hash.new
end

Instance Method Details

#add(asset:, ext:) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/sitepress/formats.rb', line 36

def add(asset: , ext: )
  resource = Resource.new(asset: asset, node: @node, ext: ext)
  if @formats.has_key? ext
    raise Sitepress::ExistingRequestPathError, "Resource at #{resource.request_path} already set"
  else
    @formats[ext] = resource
  end
end

#each(&block) ⇒ Object



16
17
18
# File 'lib/sitepress/formats.rb', line 16

def each(&block)
  @formats.values.each(&block)
end

#ext(ext) ⇒ Object



24
25
26
# File 'lib/sitepress/formats.rb', line 24

def ext(ext)
  @formats[ext]
end

#extensionsObject



28
29
30
# File 'lib/sitepress/formats.rb', line 28

def extensions
  @formats.keys
end

#inspectObject



45
46
47
# File 'lib/sitepress/formats.rb', line 45

def inspect
  "<#{self.class}: resources=#{map(&:request_path)}>"
end

#mime_type(mime_type) ⇒ Object



32
33
34
# File 'lib/sitepress/formats.rb', line 32

def mime_type(mime_type)
  find { |f| f.mime_type == mime_type }
end

#remove(ext) ⇒ Object



20
21
22
# File 'lib/sitepress/formats.rb', line 20

def remove(ext)
  @formats.delete(ext)
end