Class: Webgen::Context

Inherits:
Object
  • Object
show all
Includes:
HtmlHead, Nodes, Rendering, WebgenTags
Defined in:
lib/webgen/context.rb,
lib/webgen/context/nodes.rb,
lib/webgen/context/html_head.rb,
lib/webgen/context/rendering.rb,
lib/webgen/context/webgen_tags.rb

Overview

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

About

A context object provides information about the render context as well as access to the website that is rendered. The needed context variables are stored in the options hash. You can set any options you like, however, there are two noteworthy options:

:content

The content string that should be processed. This option is always set.

:chain

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

The persistent options hash is shared by all cloned Context objects.

Adding custom methods

If you want to add custom methods to each context object of your website that is created, you just need to define one or more modules in which your custom methods are defined and then add the modules to the ‘website.ext.context_modules’ array.

Here is a simple example:

module MyContextMethods

  def my_method
    # do something useful here
  end

end

website.ext.context_modules << MyContextMethods

Defined Under Namespace

Modules: HtmlHead, Nodes, Rendering, WebgenTags

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HtmlHead

#html_head

Methods included from Rendering

#render_block

Methods included from WebgenTags

#tag

Methods included from Nodes

#content_node, #dest_node, #ref_node

Constructor Details

#initialize(website, options = {}, persistent = {}) ⇒ Context

Create a new Context object belonging to the website object website.

All modules listed in the array ‘website.ext.context_modules’ are automatically used to extend the Context object.

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

:content

Is set to an empty string.



73
74
75
76
77
78
# File 'lib/webgen/context.rb', line 73

def initialize(website, options = {}, persistent = {})
  @website = website
  (website.ext.context_modules || []).each {|m| self.extend(m)}
  @options = {:content => '', :chain => []}.merge(options)
  @persistent = persistent
end

Instance Attribute Details

#optionsObject (readonly)

Processing options.



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

def options
  @options
end

#persistentObject (readonly)

The persistent options. Once initialized, all cloned objects refer to the same hash.



55
56
57
# File 'lib/webgen/context.rb', line 55

def persistent
  @persistent
end

#websiteObject (readonly)

The website object to which the render context belongs.



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

def website
  @website
end

Instance Method Details

#[](name) ⇒ Object

Return the value of the option name.



89
90
91
# File 'lib/webgen/context.rb', line 89

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

#[]=(name, value) ⇒ Object

Set the option name to the given +value.



94
95
96
# File 'lib/webgen/context.rb', line 94

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.



84
85
86
# File 'lib/webgen/context.rb', line 84

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

#contentObject

Return the :content option.



99
100
101
# File 'lib/webgen/context.rb', line 99

def content
  @options[:content]
end

#content=(value) ⇒ Object

Set the :content option to the given value.



104
105
106
# File 'lib/webgen/context.rb', line 104

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