class SparkEngine::BlockHelper

Attributes

current_helper[RW]
current_parent_block_helper[RW]
helper[W]
parent[W]

Public Class Methods

inherited(klass) click to toggle source
# File lib/spark_engine/helpers/block_helper.rb, line 10
def inherited(klass)
  # Define the helper method
  # e.g. for a class:
  #   class HelloHelper < BlockHelpers::Base
  #     #.....
  #   end
  #
  # then we define a helper method 'hello_helper'
  #
  method_name = klass.name.split('::').last.underscore
  klass.parent.class_eval %(
    def #{method_name}(*args, &block)
      
      # Get the current helper object which has all the normal helper methods
      if self.is_a?(SparkEngine::BlockHelper)
        top_level_helper = self.helper
        parent_block_helper = self
      else
        top_level_helper = self
        parent_block_helper = nil
      end
      
      # We need to save the current helper and parent block helper in the class so that
      # it's visible to the renderer's 'initialize' method...
      #{klass.name}.current_helper = top_level_helper
      #{klass.name}.current_parent_block_helper = parent_block_helper
      renderer = #{klass.name}.new(*args)

      # ...then set them anyway on the renderer so that renderer methods can use it
      renderer.send(:helper=, top_level_helper)
      renderer.send(:parent=, parent_block_helper)

      body = block ? capture(renderer, &block) : nil

      if processed_body = renderer.display(body)
        return processed_body
      end
    end
  )
end

Public Instance Methods

display(body) click to toggle source
# File lib/spark_engine/helpers/block_helper.rb, line 55
def display(body)
  body
end
respond_to?(method, include_all = false) click to toggle source
Calls superclass method
# File lib/spark_engine/helpers/block_helper.rb, line 59
def respond_to?(method, include_all = false)
  super or helper.respond_to?(method, include_all)
end

Protected Instance Methods

capture(*args) { |*args| ... } click to toggle source
# File lib/spark_engine/helpers/block_helper.rb, line 85
def capture(*args)
  # ActiveSupport 3.1 breaks capture method (defines it on all objects)
  # so we have to resort to rewrite it
  value = nil
  buffer = with_output_buffer { value = yield(*args) }
  if string = buffer.presence || value and string.is_a?(String)
    ERB::Util.html_escape string
  end
end
helper() click to toggle source
# File lib/spark_engine/helpers/block_helper.rb, line 72
def helper
  @helper ||= self.class.current_helper
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/spark_engine/helpers/block_helper.rb, line 76
def method_missing(method, *args, &block)
  if helper.respond_to?(method)
    self.class_eval "def #{method}(*args, &block); helper.send('#{method}', *args, &block); end"
    self.send(method, *args, &block)
  else
    super
  end
end
parent() click to toggle source

For nested block helpers

# File lib/spark_engine/helpers/block_helper.rb, line 68
def parent
  @parent ||= self.class.current_parent_block_helper
end