Class: Webgen::SourceHandler::Memory

Inherits:
Object
  • Object
show all
Includes:
Base, WebsiteAccess
Defined in:
lib/webgen/sourcehandler/memory.rb

Overview

This source handler should be used for handling nodes that are created during the write phase.

Instance Method Summary collapse

Methods included from Base

#node_exists?, #output_path, #page_from_path, #parent_node

Methods included from Base::OutputPathHelpers

#standard_output_path

Methods included from Loggable

#log, #puts

Methods included from WebsiteAccess

included, website

Constructor Details

#initializeMemory

:nodoc:



12
13
14
15
16
# File 'lib/webgen/sourcehandler/memory.rb', line 12

def initialize #:nodoc:
  website.blackboard.add_listener(:node_flagged) do |node, *flags|
    node.tree[node.node_info[:memory_source_alcn]].flag(:dirty) if node.node_info[:memory_source_alcn]
  end
end

Instance Method Details

#content(node) ⇒ Object

Return the content of the memory node. If the memory node was not created in this webgen run, it will be flagged for reinitialization (and therefore recreation).



31
32
33
34
35
36
37
38
# File 'lib/webgen/sourcehandler/memory.rb', line 31

def content(node)
  if @data && @data[node.alcn]
    @data[node.alcn].call
  else
    node.flag(:reinit)
    nil
  end
end

#create_node(path, source_alcn, data = nil) ⇒ Object

Create a node for the path. The source_alcn specifies the node that creates this memory node when written. You have two options for providing the content for this node: either you set data to a string (or a Webgen::Path::SourceIO object) or you provide a block which takes the created node as argument and returns a string (or a Webgen::Path::SourceIO object).



22
23
24
25
26
27
# File 'lib/webgen/sourcehandler/memory.rb', line 22

def create_node(path, source_alcn, data = nil)
  super(path) do |node|
    node.node_info[:memory_source_alcn] = source_alcn
    (@data ||= {})[node.alcn] = lambda { data || yield(node) }
  end
end