Class: BrainDamage::Templateable::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/brain_damage/lib/templateable/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, options = {}) ⇒ Base

Returns a new instance of Base.



14
15
16
17
18
19
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



48
49
50
# File 'lib/generators/brain_damage/lib/templateable/base.rb', line 48

def method_missing(method, *args, &block)
  @resource.send method, *args, &block
end

Instance Attribute Details

#inner_htmlObject (readonly)

Returns the value of attribute inner_html.



11
12
13
# File 'lib/generators/brain_damage/lib/templateable/base.rb', line 11

def inner_html
  @inner_html
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/generators/brain_damage/lib/templateable/base.rb', line 10

def options
  @options
end

#template_fileObject

Returns the value of attribute template_file.



12
13
14
# File 'lib/generators/brain_damage/lib/templateable/base.rb', line 12

def template_file
  @template_file
end

Instance Method Details

#indent(level = nil) ⇒ Object



43
44
45
46
# 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

#render(template_file = @template_file) ⇒ Object



39
40
41
# 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) ⇒ Object



21
22
23
24
# 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) ⇒ Object



26
27
28
29
# 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) ⇒ Object



31
32
33
34
35
36
37
# 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