Class: Ditz::ErbHtml

Inherits:
Object show all
Defined in:
lib/html.rb

Overview

pass through any variables needed for template generation, and add a bunch of HTML formatting utility methods.

Instance Method Summary collapse

Constructor Details

#initialize(template_dir, links, binding = {}) ⇒ ErbHtml

Returns a new instance of ErbHtml.



8
9
10
11
12
# File 'lib/html.rb', line 8

def initialize template_dir, links, binding={}
  @template_dir = template_dir
  @links = links
  @binding = binding
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *a) ⇒ Object



64
65
66
# File 'lib/html.rb', line 64

def method_missing meth, *a
  @binding.member?(meth) ? @binding[meth] : super
end

Instance Method Details

#clone_for_binding(extra_binding = {}) ⇒ Object

return an ErbHtml object that has the current binding plus extra_binding merged in



15
16
17
# File 'lib/html.rb', line 15

def clone_for_binding extra_binding={}
  extra_binding.empty? ? self : ErbHtml.new(@template_dir, @links, @binding.merge(extra_binding))
end


51
52
53
# File 'lib/html.rb', line 51

def fancy_issue_link_for i
  "<span class=\"issuestatus_#{i.status}\">" + link_to(i, "[#{i.title}]") + "</span>"
end

#h(o) ⇒ Object

the following methods are meant to be called from the ERB itself



41
# File 'lib/html.rb', line 41

def h o; o.to_s.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;") end


55
56
57
58
59
# File 'lib/html.rb', line 55

def link_issue_names project, s
  project.issues.inject(s) do |s, i|
    s.gsub(/\b#{i.name}\b/, fancy_issue_link_for(i))
  end
end

Raises:

  • (ArgumentError)


45
46
47
48
49
50
# File 'lib/html.rb', line 45

def link_to o, name
  dest = @links[o]
  dest = o if dest.nil? && o.is_a?(String)
  raise ArgumentError, "no link for #{o.inspect}" unless dest
  "<a href=\"#{dest}\">#{name}</a>"
end

#obscured_email(e) ⇒ Object



44
# File 'lib/html.rb', line 44

def obscured_email e; h e.gsub(/@.*?(>|$)/, "@...\\1") end

#p(o) ⇒ Object



43
# File 'lib/html.rb', line 43

def p o; "<p>" + h(o.to_s).gsub("\n\n", "</p><p>") + "</p>" end

#render_string(s, extra_binding = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/html.rb', line 29

def render_string s, extra_binding={}
  if extra_binding.empty?
    ERB.new(s).result binding
  else
    clone_for_binding(extra_binding).render_string s
  end
end

#render_template(template_name, extra_binding = {}) ⇒ Object Also known as: render



19
20
21
22
23
24
25
26
27
# File 'lib/html.rb', line 19

def render_template template_name, extra_binding={}
  if extra_binding.empty?
    @@erbs ||= {}
    @@erbs[template_name] ||= ERB.new IO.read(File.join(@template_dir, "#{template_name}.rhtml"))
    @@erbs[template_name].result binding
  else
    clone_for_binding(extra_binding).render_template template_name
  end
end

#t(o) ⇒ Object



42
# File 'lib/html.rb', line 42

def t o; o.strftime "%Y-%m-%d %H:%M %Z" end