Sha256: ee99cb67e6cf98b862958f5f253d483779b7c3d268c78c057d56d10a605f60ea

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 KB

Contents

require 'erb'
require 'json'
require 'forwardable'

class Lurker::BasePresenter

  attr_reader :options

  def initialize(options = {})
    @options = options
  end

  def render_erb(erb_name, binding = get_binding)
    template_path = path_for_template(erb_name)
    template = ERB.new(File.read(template_path), nil, '-')
    template.result(binding)
  end

  def get_binding
    binding
  end

  def html_directory
    options[:url_base_path] || options[:html_directory] || ""
  end

  def url_base_path
    options[:url_base_path] || '/'
  end

  def css_path
    File.join(html_directory, "styles.css")
  end

  def index_path(subdirectory = "")
    html_path = File.join(html_directory, subdirectory)
    if options[:static_html]
      File.join(html_path, 'index.html')
    else
      html_path
    end
  end

  def tag_with_anchor(tag, content, anchor_slug = nil)
    anchor_slug ||= content.downcase.gsub(' ', '_')
    <<-EOS
    <#{tag} id="#{anchor_slug}">
      <a href="##{anchor_slug}" class="anchor">
        #{content}
      </a>
    </#{tag}>
    EOS
  end

  def render(*args)
    rendering_controller.render_to_string(*args)
  end

  protected

  def path_for_template(filename)
    template_dir  = options[:template_directory]
    template_path = File.join(template_dir, filename) if template_dir
    if template_path.nil? || !File.exists?(template_path)
      template_path = File.join(File.dirname(__FILE__), "../templates", filename)
    end
    template_path
  end

  def rendering_controller
    return @rendering_controller if @rendering_controller
    @rendering_controller = Lurker::RenderingController.new
    instance_variables.each do |var|
      @rendering_controller.instance_variable_set(var, instance_variable_get(var))
    end
    @rendering_controller
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lurker-0.6.1 lib/lurker/presenters/base_presenter.rb
lurker-0.5.7 lib/lurker/presenters/base_presenter.rb
lurker-0.6.0 lib/lurker/presenters/base_presenter.rb
lurker-0.5.6 lib/lurker/presenters/base_presenter.rb
lurker-0.5.5 lib/lurker/presenters/base_presenter.rb
lurker-0.5.4 lib/lurker/presenters/base_presenter.rb