Class: Webgen::Context

Inherits:
Object
  • Object
show all
Includes:
WebsiteAccess
Defined in:
lib/webgen/context.rb,
lib/webgen/context/nodes.rb

Overview

This class represents the context object that is passed, for example, to the call method of a content processor.

The needed context variables are stored in the options hash. You can set any options you like, however, there are three noteworthy options:

:content

The content string that should be processed.

:processors

Normally an AccessHash object providing access to all available content processors.

:chain

The chain of nodes that is processed. There are some utiltity methods for getting special nodes of the chain (see #ref_node, #content_node and #dest_node).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WebsiteAccess

included, website

Constructor Details

#initialize(options = {}) ⇒ Context

Create a new Context object. You can use the options hash to set needed options.

The following options are set by default and can be overridden via the options hash:

:content

Is set to an empty string.

:processors

Is set to a new AccessHash.



38
39
40
41
42
43
# File 'lib/webgen/context.rb', line 38

def initialize(options = {})
  @options = {
    :content => '',
    :processors => Webgen::ContentProcessor::AccessHash.new
  }.merge(options)
end

Instance Attribute Details

#optionsObject

Processing options



27
28
29
# File 'lib/webgen/context.rb', line 27

def options
  @options
end

Instance Method Details

#[](name) ⇒ Object

Return the value of the option name.



52
53
54
# File 'lib/webgen/context.rb', line 52

def [](name)
  @options[name]
end

#[]=(name, value) ⇒ Object

Set the option name to the given +value.



57
58
59
# File 'lib/webgen/context.rb', line 57

def []=(name, value)
  @options[name] = value
end

#clone(options = {}) ⇒ Object

Create a copy of the current object. You can use the options parameter to override options of the current Context object in the newly created Context object.



47
48
49
# File 'lib/webgen/context.rb', line 47

def clone(options = {})
  self.class.new(@options.merge(options))
end

#contentObject

Return the :content option.



62
63
64
# File 'lib/webgen/context.rb', line 62

def content
  @options[:content]
end

#content=(value) ⇒ Object

Set the :content option to the given value.



67
68
69
# File 'lib/webgen/context.rb', line 67

def content=(value)
  @options[:content] = value
end

#content_nodeObject Also known as: node

Return the node that is ultimately rendered.

This node should be used, for example, for retrieving meta information.



29
30
31
# File 'lib/webgen/context/nodes.rb', line 29

def content_node
  @options[:chain] && @options[:chain].last
end

#dest_nodeObject

Return the node which represents the file into which everything gets rendered. This is normally the same node as #content_node but can differ in special cases. For example, when rendering the content of node called my.page into the output of the node this.page, this.page would be the dest_node and my.page would be the content_node.

The dest_node is not included in the chain but can be set via the option :dest_node!

The returned node should be used as source node for calculating relative paths to other nodes.



14
15
16
# File 'lib/webgen/context/nodes.rb', line 14

def dest_node
  @options[:dest_node] || self.content_node
end

#ref_nodeObject

Return the reference node, ie. the node which provided the original content for this context object.

The returned node should be used, for example, for resolving relative paths.



22
23
24
# File 'lib/webgen/context/nodes.rb', line 22

def ref_node
  @options[:chain] && @options[:chain].first
end