Class: Sitepress::Resources
- Inherits:
-
Object
- Object
- Sitepress::Resources
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/sitepress/resources.rb
Overview
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 formats `.gif`, and `.html`.
Instance Method Summary collapse
- #add(resource) ⇒ Object
- #add_asset(asset, format: nil) ⇒ Object
- #each(&block) ⇒ Object
- #flatten(resources: []) ⇒ Object
- #format(extension) ⇒ Object
- #format?(key) ⇒ Boolean
- #formats ⇒ Object
-
#initialize(node:) ⇒ Resources
constructor
A new instance of Resources.
- #inspect ⇒ Object
- #mime_type(mime_type) ⇒ Object
- #remove(extension) ⇒ Object
Constructor Details
#initialize(node:) ⇒ Resources
Returns a new instance of Resources.
14 15 16 17 |
# File 'lib/sitepress/resources.rb', line 14 def initialize(node:) @node = node @registry = Hash.new end |
Instance Method Details
#add(resource) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/sitepress/resources.rb', line 43 def add(resource) if @registry.key? resource.format raise Sitepress::ExistingRequestPathError, "Resource at #{resource.request_path} already set with format #{resource.format.inspect}" else @registry[resource.format] = resource end end |
#add_asset(asset, format: nil) ⇒ Object
59 60 61 62 |
# File 'lib/sitepress/resources.rb', line 59 def add_asset(asset, format: nil) format = symbolize(format || default_format) add Resource.new(asset: asset, node: @node, format: format) end |
#each(&block) ⇒ Object
19 20 21 |
# File 'lib/sitepress/resources.rb', line 19 def each(&block) @registry.values.each(&block) end |
#flatten(resources: []) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/sitepress/resources.rb', line 51 def flatten(resources: []) each { |resource| resources << resource } @node.children.each do |child| child.resources.flatten.each { |resource| resources << resource } end resources end |
#format(extension) ⇒ Object
27 28 29 |
# File 'lib/sitepress/resources.rb', line 27 def format(extension) @registry[symbolize(extension || default_format)] end |
#format?(key) ⇒ Boolean
35 36 37 |
# File 'lib/sitepress/resources.rb', line 35 def format?(key) @registry.key? key end |
#formats ⇒ Object
31 32 33 |
# File 'lib/sitepress/resources.rb', line 31 def formats @registry.keys end |
#inspect ⇒ Object
64 65 66 |
# File 'lib/sitepress/resources.rb', line 64 def inspect "<#{self.class}: resources=#{map(&:request_path)}>" end |
#mime_type(mime_type) ⇒ Object
39 40 41 |
# File 'lib/sitepress/resources.rb', line 39 def mime_type(mime_type) find { |f| f.mime_type == mime_type } end |
#remove(extension) ⇒ Object
23 24 25 |
# File 'lib/sitepress/resources.rb', line 23 def remove(extension) @registry.delete symbolize(extension) end |