Class: Ditz::ErbHtml

Inherits:
Object show all
Defined in:
lib/ditz/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/ditz/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



102
103
104
# File 'lib/ditz/html.rb', line 102

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/ditz/html.rb', line 15

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

#h(o) ⇒ Object

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



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

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


73
74
75
76
77
78
79
80
81
# File 'lib/ditz/html.rb', line 73

def issue_link_for i, opts={}
  link = if opts[:inline]
    "<span class=\"inline-issue-link\">" + link_to(i, "issue <span class=\"id\">#{i.id[0,8]}</span>: #{i.title}") + "</span>"
  else
    link_to i, i.title
  end
  link = link + " " + issue_status_img_for(i, :class => "inline-status-image") if opts[:status_image]
  link
end

#issue_status_img_for(i, opts = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ditz/html.rb', line 52

def issue_status_img_for i, opts={}
  fn, title = if i.closed?
    case i.disposition
    when :fixed; ["green-check.png", "fixed"]
    when :wontfix; ["red-check.png", "won't fix"]
    when :reorg; ["blue-check.png", "reorganized"]
    end
  elsif i.in_progress?
    ["green-bar.png", "in progress"]
  elsif i.paused?
    ["yellow-bar.png", "paused"]
  end

  return "" unless fn

  args = {:src => fn, :alt => title, :title => title}
  args[:class] = opts[:class] if opts[:class]

  "<img " + args.map { |k, v| "#{k}=#{v.inspect}" }.join(" ") + "/>"
end


83
84
85
86
87
# File 'lib/ditz/html.rb', line 83

def link_issue_names project, s, opts={}
  project.issues.inject(s) do |s, i|
    s.gsub(/\b#{i.name}\b/, issue_link_for(i, {:inline => true, :status_image => true}.merge(opts)))
  end
end

Raises:

  • (ArgumentError)


45
46
47
48
49
50
# File 'lib/ditz/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/ditz/html.rb', line 44

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

#p(o) ⇒ Object



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

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

#progress_meter(p, size = 50) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/ditz/html.rb', line 89

def progress_meter p, size=50
  done = (p * size).to_i
  undone = [size - done, 0].max
  "<span class='progress-meter'><span class='progress-meter-done'>" +
    ("&nbsp;" * done) +
    "</span><span class='progress-meter-undone'>" +
    ("&nbsp;" * undone) +
    "</span></span>"
end

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



29
30
31
32
33
34
35
# File 'lib/ditz/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/ditz/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/ditz/html.rb', line 42

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