class BrainDamage::Templateable::Base

Attributes

inner_html[R]
options[R]
template_file[RW]

Public Class Methods

new(resource, options = {}) click to toggle source
# File lib/generators/brain_damage/lib/templateable/base.rb, line 14
def initialize(resource, options = {})
  @options = options
  @resource = resource

  @template_file = options[:template_file] || "#{self.class.to_s.split('::').last.underscore}.html.haml" unless @template_file
end

Public Instance Methods

indent(level = nil) click to toggle source
# File lib/generators/brain_damage/lib/templateable/base.rb, line 43
def indent(level = nil)
  level ||= @html_args[:indentation]
  @inner_html.indent! level if level
end
method_missing(method, *args, &block) click to toggle source
# File lib/generators/brain_damage/lib/templateable/base.rb, line 48
def method_missing(method, *args, &block)
  @resource.send method, *args, &block
end
render(template_file = @template_file) click to toggle source
# File lib/generators/brain_damage/lib/templateable/base.rb, line 39
def render(template_file = @template_file)
  render_template_file template_file
end
render_erb_file(file) click to toggle source
# File lib/generators/brain_damage/lib/templateable/base.rb, line 21
def render_erb_file(file)
  raise "Trying to render a file that doesn't exist: #{file}" unless File.file? file
  render_erb_string(File.open(file).read)
end
render_erb_string(string) click to toggle source
# File lib/generators/brain_damage/lib/templateable/base.rb, line 26
def render_erb_string(string)
  return Erubis::Eruby.new(string).result(binding).strip.gsub(/\n\n\n+/, "\n").gsub(/NEW_LINE_\d+/, "").split("\n").map(&:rstrip).join("\n") if string.is_a? String
  ''
end
render_template_file(template_file = @template_file) click to toggle source
# File lib/generators/brain_damage/lib/templateable/base.rb, line 31
def render_template_file(template_file = @template_file)
  if Pathname.new(template_file).absolute?
    render_erb_file template_file
  else
    render_erb_file "#{dir}/templates/#{template_file}"
  end
end