Module: Slideshow::CaptureHelper

Defined in:
lib/slideshow/helpers/capture_helper.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) _erb_buffer(the_binding)

Provides direct acccess to the ERB buffer in the conext of the binding.

Parameters

the_binding<Binding>

The binding to pass to the buffer.

Returns

The current ERB output buffer.



129
130
131
# File 'lib/slideshow/helpers/capture_helper.rb', line 129

def _erb_buffer( the_binding )
  eval('_erbout', the_binding, __FILE__, __LINE__)
end

- (Object) capture_erb(*args, &block)

This method is used to capture content from an ERB filter evaluation. It is useful to helpers that need to process chunks of data during ERB filter processing.

Parameters

*args

Arguments to pass to the block.

&block

The ERB block to call.

Returns

String

The output of the block.

Examples

Capture being used in an ERB page:

<% @foo = capture_erb do %>
  <p>Some Foo content!</p> 
<% end %>


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/slideshow/helpers/capture_helper.rb', line 87

def capture_erb( *args, &block )
  # get the buffer from the block's binding
  buffer = _erb_buffer(block.binding) rescue nil
 
  # If there is no buffer, just call the block and get the contents
  if buffer.nil?
    block.call(*args)
  # If there is a buffer, execute the block, then extract its contents
  else
    pos = buffer.length
    block.call(*args)
 
    # extract the block
    data = buffer[pos..-1]
 
    # replace it in the original with empty string
    buffer[pos..-1] = ""
 
    data
  end
end

- (Object) concat_erb(string, the_binding)

This method is used to concatenate content into the ERB output buffer. It is usefule to helpers that need to insert transformed text back into the ERB output buffer.

Parameters

string<String>

The string to insert into the ERB output.

the_binding<Binding>

The binding to pass to the buffer.



117
118
119
# File 'lib/slideshow/helpers/capture_helper.rb', line 117

def concat_erb( string, the_binding )
  _erb_buffer(the_binding) << string
end

- (Object) content_for(obj, string = nil, &block)

Called in pages and partials to store up content for later use. Takes a string and/or a block. First, the string is evaluated, and then the block is captured using the capture() helper provided by the template languages. The two are concatenated together.

Content is retrieved by calling the method without a string or a block.

Parameters

obj<Object>

The key in the content_for hash.

string<String>

Textual content. Defaults to nil.

&block

A block to be evaluated and concatenated to string.

Returns

Any content associated with the key (or nil).

Example

content_for(:foo, "Foo")
content_for(:foo)             #=> "Foo"
content_for(:foo, "Bar")
content_for(:foo)             #=> "FooBar"


27
28
29
30
31
32
33
# File 'lib/slideshow/helpers/capture_helper.rb', line 27

def content_for( obj, string = nil, &block )
  return @content_for[obj] unless string || block_given?
 
  cur = @content_for[obj].to_s
  new = string.to_s + (block_given? ? capture_erb(&block) : "")
  @content_for[obj] = cur + new
end

- (Boolean) content_for?(obj)

Returns true if there is content for the given key. Otherwise returns false.

Parameters

obj<Object>

The key in the conetnt_for hash.

Example

content_for(:foo, "Foo")
content_for?(:foo)            #=> true
content_for?(:bar)            #=> false


46
47
48
# File 'lib/slideshow/helpers/capture_helper.rb', line 46

def content_for?( obj )
  @content_for.key?(obj)
end

- (Object) delete_content_for(obj)

Deletes any content associated with the given object in the content_for hash.

Parameters

obj<Object>

The key in the conetnt_for hash.

Returns

Any content associated with the key (or nil).

Example

content_for(:foo, "Foo")
content_for?(:foo)            #=> true
delete_content_for(:foo)
content_for?(:foo)            #=> false


65
66
67
# File 'lib/slideshow/helpers/capture_helper.rb', line 65

def delete_content_for( obj )
  @content_for.delete(obj)
end