Class: Webgen::Source::Stacked

Inherits:
Object
  • Object
show all
Defined in:
lib/webgen/source/stacked.rb

Overview

This source class is used to stack several sources together.

It serves two purposes:

  • First, it can be used to access more than one source. This is useful when your website consists of more than one source directory and you want to use all of them.

  • Second, sources can be mounted on specific directories. For example, a folder with images that you don’t want to copy to the website source directory can be mounted under /images sothat they are available nonetheless.

Also be aware that when a path is returned by a source that has already be returned by a prior source, it is discarded and not used.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map = {}, cache_paths = false) ⇒ Stacked

Create a new stack. The optional map parameter can be used to provide initial mappings of mount points to source objects (see #add for details). You cannot add other maps after a call to #paths if cache_paths is true



30
31
32
33
34
# File 'lib/webgen/source/stacked.rb', line 30

def initialize(map = {}, cache_paths = false)
  @stack = []
  @cache_paths = cache_paths
  add(map)
end

Instance Attribute Details

#cache_pathsObject

Specifies whether the result of #paths calls should be cached (default: false). If caching is activated, new maps cannot be added to the stacked source anymore!



25
26
27
# File 'lib/webgen/source/stacked.rb', line 25

def cache_paths
  @cache_paths
end

#stackObject (readonly)

Return the stack of mount point to Webgen::Source object maps.



21
22
23
# File 'lib/webgen/source/stacked.rb', line 21

def stack
  @stack
end

Instance Method Details

#add(maps) ⇒ Object

Add all mappings found in maps to the stack. The parameter maps should be an array of two-element arrays which contain an absolute directory (ie. starting and ending with a slash) and a source object.



39
40
41
42
43
44
45
# File 'lib/webgen/source/stacked.rb', line 39

def add(maps)
  raise "Cannot add new maps since caching is activated for this source" if defined?(@paths) && @cache_paths
  maps.each do |mp, source|
    raise "Invalid mount point specified: #{mp}" unless mp =~ /^\//
    @stack << [mp, source]
  end
end

#pathsObject

Return all paths returned by the sources in the stack. Since the stack is ordered, paths returned by later source objects are not used if a prior source object has returned the same path.



50
51
52
53
54
55
56
57
58
59
# File 'lib/webgen/source/stacked.rb', line 50

def paths
  return @paths if defined?(@paths) && @cache_paths
  @paths = Set.new
  @stack.each do |mp, source|
    source.paths.each do |path|
      @paths.add?(path.mount_at(mp))
    end
  end
  @paths
end