Class: Ditz::ErbHtml
Overview
pass through any variables needed for template generation, and add a bunch of HTML formatting utility methods.
Instance Method Summary collapse
-
#clone_for_binding(extra_binding = {}) ⇒ Object
return an ErbHtml object that has the current binding plus extra_binding merged in.
-
#h(o) ⇒ Object
the following methods are meant to be called from the ERB itself.
-
#initialize(template_dir, links, binding = {}) ⇒ ErbHtml
constructor
A new instance of ErbHtml.
- #issue_link_for(i, opts = {}) ⇒ Object
- #issue_status_img_for(i, opts = {}) ⇒ Object
- #link_issue_names(project, s, opts = {}) ⇒ Object
- #link_to(o, name) ⇒ Object
- #method_missing(meth, *a) ⇒ Object
- #obscured_email(e) ⇒ Object
- #p(o) ⇒ Object
- #progress_meter(p, size = 50) ⇒ Object
- #render_string(s, extra_binding = {}) ⇒ Object
- #render_template(template_name, extra_binding = {}) ⇒ Object (also: #render)
- #t(o) ⇒ Object
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
99 100 101 |
# File 'lib/html.rb', line 99 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 |
#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("&", "&").gsub("<", "<").gsub(">", ">") end |
#issue_link_for(i, opts = {}) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/html.rb', line 73 def issue_link_for i, opts={} link = link_to i, "#{i.title}" link = "<span class=\"inline-issue-link\">" + link + "</span>" if opts[:inline] 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/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 |
#link_issue_names(project, s, opts = {}) ⇒ Object
80 81 82 83 84 |
# File 'lib/html.rb', line 80 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 |
#link_to(o, name) ⇒ Object
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 |
#progress_meter(p, size = 50) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/html.rb', line 86 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'>" + (" " * done) + "</span><span class='progress-meter-undone'>" + (" " * undone) + "</span></span>" 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 |