Module: CoreHelper::Capture
- Included in:
- CoreHelper
- Defined in:
- lib/helpers/core_helper.rb
Instance Method Summary collapse
- #capture(*args, &block) ⇒ Object
- #capture_block(*args, &block) ⇒ Object
- #capture_erb(*args, &block) ⇒ Object
- #capture_erb_with_buffer(buffer, *args, &block) ⇒ Object
- #concat(string, binding) ⇒ Object
- #content_for(name, content = nil, &block) ⇒ Object
Instance Method Details
#capture(*args, &block) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/helpers/core_helper.rb', line 87 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
102 103 104 |
# File 'lib/helpers/core_helper.rb', line 102 def capture_block(*args, &block) block.call(*args) end |
#capture_erb(*args, &block) ⇒ Object
106 107 108 109 |
# File 'lib/helpers/core_helper.rb', line 106 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
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/helpers/core_helper.rb', line 111 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
124 125 126 |
# File 'lib/helpers/core_helper.rb', line 124 def concat(string, binding) eval('_erbout', binding) << string end |
#content_for(name, content = nil, &block) ⇒ Object
81 82 83 84 85 |
# File 'lib/helpers/core_helper.rb', line 81 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 |