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

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).



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

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

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

Create a node under parent for the path. The source_alcn specified 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 return a string (or a Webgen::Path::SourceIO object).



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

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