Class: Webgen::Page::Block

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

Overview

A single block within a Page object. The content of the block can be rendered using the #render method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, content, options) ⇒ Block

Create a new block with the name name and the given content and options.



24
25
26
# File 'lib/webgen/page.rb', line 24

def initialize(name, content, options)
  @name, @content, @options = name, content, options
end

Instance Attribute Details

#contentObject (readonly)

The content of the block.



18
19
20
# File 'lib/webgen/page.rb', line 18

def content
  @content
end

#nameObject (readonly)

The name of the block.



15
16
17
# File 'lib/webgen/page.rb', line 15

def name
  @name
end

#optionsObject (readonly)

The options set specifically for this block.



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

def options
  @options
end

Instance Method Details

#render(context) ⇒ Object

Render the block using the provided context object.

The context object needs to respond to #[] and #[]= (e.g. a Hash is a valid context object) and the key :processors needs to contain a Hash which maps processor names to processor objects that respond to #call.

Uses the content processors specified in the pipeline key of the options attribute to do the actual rendering.

Returns the given context with the rendered content.



38
39
40
41
42
43
44
45
46
# File 'lib/webgen/page.rb', line 38

def render(context)
  context[:content] = @content.dup
  context[:block] = self
  @options['pipeline'].to_s.split(/,/).each do |processor|
    raise "No such content processor available: #{processor}" unless context[:processors].has_key?(processor)
    context[:processors][processor].call(context)
  end
  context
end