Module: CoreHelper::Capture

Included in:
CoreHelper
Defined in:
lib/helpers/core_helper.rb

Instance Method Summary collapse

Instance Method Details

#capture(*args, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/helpers/core_helper.rb', line 89

def capture(*args, &block)
  # execute the block
  begin
    buffer = eval('_erbout', block.binding)
  rescue
    buffer = nil
  end

  if buffer.nil?
    capture_block(*args, &block).to_s
  else
    capture_erb_with_buffer(buffer, *args, &block).to_s
  end
end

#capture_block(*args, &block) ⇒ Object



104
105
106
# File 'lib/helpers/core_helper.rb', line 104

def capture_block(*args, &block)
  block.call(*args)
end

#capture_erb(*args, &block) ⇒ Object



108
109
110
111
# File 'lib/helpers/core_helper.rb', line 108

def capture_erb(*args, &block)
  buffer = eval('_erbout', block.binding)
  capture_erb_with_buffer(buffer, *args, &block)
end

#capture_erb_with_buffer(buffer, *args, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/helpers/core_helper.rb', line 113

def capture_erb_with_buffer(buffer, *args, &block)
  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

#concat(string, binding) ⇒ Object



126
127
128
# File 'lib/helpers/core_helper.rb', line 126

def concat(string, binding)
  eval('_erbout', binding) << string
end

#content_for(name, content = nil, &block) ⇒ Object



83
84
85
86
87
# File 'lib/helpers/core_helper.rb', line 83

def content_for(name, content = nil, &block)
  existing_content_for = instance_variable_get("@content_for_#{name}").to_s
  new_content_for      = existing_content_for + (block_given? ? capture(&block) : content)
  instance_variable_set("@content_for_#{name}", new_content_for)
end